Jump to content

Help needed for FileCopy


Recommended Posts

I am having trouble with this simple script.

I have a lot of .pdf files that have a generated name. I want to replace them with a number from the document. I can get the number I want to use for the name by opening the .pdf, copying all the text (select all, copy) and converting that to an array. The number I want is always element 35 in that array.

Here is my code

#include <IE.au3>
#include <Excel.au3>
#include <Array.au3>
#include <Date.au3>
#include <File.au3>

dim $aLines, $aColumns,  $avData[1000][6] = [[0]], $aFile[1000]
$FileList = _FileListToArray("Y:\pdf\")
WinActivate("Adobe")
For $i = 4 to $FileList[0]
Sleep(100)
Send("^o")
WinWaitActive("Open")
Sleep(100)
Send($FileList[$i])
Sleep(100)
Send("{Enter}")
Sleep(100)
WinWaitActive($FileList[$i])
Send("^a")
Sleep(500)
Send("^c")
Sleep(500)
$sPage = Clipget()
StringStripCR($sPage)
$aFile = StringSplit($sPage, @LF)
FileCopy("Y:\pdf\" & $FileList[$i] , "Y:\pdf\new\" & $aFile[35] & ".pdf", 9)
Sleep (500)
Sleep (500)
Send("^w")
Sleep(100)
Next

I don't know why the line "FileCopy("Y:\pdf\" & $FileList[$i] , "Y:\pdf\new\" & $aFile[35] & ".pdf", 9)" does not copy the file.

Any help would be appreciated

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for the response, William, but that is not it. I have been out of town for a week, and I am now just getting back to this problem.

I use "FileCopy("Y:pdf" & $FileList[$i] , "Y:pdfnew" & $aFileList[$i], 9)" and it copies the file just fine. I want to change the name of the file when I copy it, however.

Maybe I'm using the wrong command? Can you use FileCopy to change the name of a file when you copy it? In re-reading the help file, it does not actually say you can.

Edited by redpicker
Link to comment
Share on other sites

Check the output of $aFile[35]. What does it look like?

EDIT: And the short answer is Yes, you can change the name of the file with FileCopy()

When you copy a file and you're NOT changing the name, you can specify the output file as ./*

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Thanks, mechaflash213. That helped.

I found if I changed "$aFile[35]" to "StringStripWS($aFile[35],3)" it works just fine. I'm guessing that $aFIle[35] contained some non-printing characters that prevented it from working.

Now, I want to change FileCopy() to FileMove() and it doesn't work anymore. I guess I can claim success, but I really would like to get rid of the old files after renaming them.

(Edit: added the ",3" to the StringStripWS since that is what I ended up using)

Edited by redpicker
Link to comment
Share on other sites

whenever you run into a problem, check the error output first and make adjustments based on the error output. Specifically, with FileMove(), if it fails only returns 0 and doesn't give much more info on why it failed. The only thing the helpfile states is that it fails if the source file cannot be moved, or if the destination already exists (if you didn't specify the correct flags).

With that information, you check your code,

  • "did I enter the source filename correctly?"

  • "is the destination acceptable"

  • "did I specify the correct flags?
If all things seem to check out, but you're still getting errors, post your code and have another pair of eyes take a look at it.

Since you are reading a file, that we don't have access to read, into an array, and feeding that array to the command, we would need to see what the variable outputs as that's probably the culprit.

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

That's pretty much how I've been working at it.

At first (prior to my first post), I displayed message boxes with the array element I was using for the file name. All seemed correct.

I then used clipput() to enter these names in the clipboard and used that to paste them into Adobe's file open or file save windows; those worked just fine.

But, your 1st post got me to thinking about non-printing characters and that Adobe may be stripping these from what gets entered in the Save-As window. That is why I used the StringStripWS functiion.

Actually, I use "StringStripWS($File[35],3)" to get both leading and trainling characters.

That works fine. But, the FileMove doesn't. It returns a value of "0". I tried using a FileDelete after the FileCopy command (using the same array element that worked in the FileCopy Source string) and it does not delete the file. I suspect this is now a security issue; that is, I'm guessing all the FileMove command is a FileCopy with a FileDelete using the same path and when the FIleDelete fails, the command fails. Additional guessing gets me to a security issue (or permissions) is preventing the FileDelete from executing.

The original code works fine with the change made in Post #5, so I'm pretty sure stripping the non-printing characters fixed the original problem. I am not sure I can fix my new problem (that is, neither FIleDelete or FileMove work), because of the way Windows works.

Thanks for the help. I was really wondering what was happening until I read your post. While it didn't give the solution directly, it did get me to think about it differently and got me to find an answer.

Link to comment
Share on other sites

Now that you mention it, it may well be a permissions issue with not being able to delete the file. Check the file permissions and see if this is the case. You may need to run the script with elevated privileges to get it to move/delete the original file.

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...