Sign in to follow this  
Followers 0
redpicker

Help needed for FileCopy

9 posts in this topic

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

Share this post


Link to post
Share on other sites



Have you forgotten the file extension in the first bit?

FileCopy("Y:pdf" & $FileList[$i]&".pdf" , "Y:pdfnew" & $aFile[35] & ".pdf", 9)

William

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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.”

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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.”

 

Share this post


Link to post
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.

Share this post


Link to post
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.”

 

Share this post


Link to post
Share on other sites

That was it. I moved the files manually to a dirve I have full permissions on and it works just fine.

THANKS!

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • fopetesl
      By fopetesl
      Struggling to get Filecopy() working
      MsgBox(0,"Moving file: ", $fLine) ; C:\AlmeterS3\AI74_No2.csv FileCopy( $fLine, "C:\AlmeterS3\S3_Database\") ; FileCopy( $fLine, $BatchDir & "\S3_Database\") If @error Then MsgBox($MB_SYSTEMMODAL, "ERROR!", "database write error " & @error) EndIf FileDelete( $fLine) doesn't copy only creates a file "C:\AlmeterS3\S3_Database" (actually AI74_No2.csv)
      Again my typo I should have added
      $FC_OVERWRITE + $FC_CREATEPATH since folder didn't actually exist.
    • Simpel
      By Simpel
      Hi,
      I do recognize bad behaviour at this snippet:
      #include <AutoItConstants.au3> Global $g_sSD = @ScriptDir & "\" Global $g_sBurstPath = $g_sSD & "Burst\" DirCreate($g_sBurstPath) Local $sFileName = "Test_mit_3_Seiten.pdf" ; any PDF with more than 1 page inside @ScriptDir FileCopy($g_sSD & $sFileName, $g_sBurstPath & $sFileName, 1) ;~ Sleep(13000) Local $hTimer = TimerInit() ConsoleWrite("Start" & @CRLF) _Burst($g_sBurstPath & $sFileName) ; bursts PDF into single pages ;~ While _FileInUse($g_sBurstPath & $sFileName) ;~ WEnd ConsoleWrite("End: " & Round(TimerDiff($hTimer)) & "ms" & @CRLF) Exit Func _quotePath($sPath) ; because of possible "spaces" inside pathes $sPath = '"' & $sPath & '"' Return $sPath EndFunc Func _Burst($sFile) Local $sPDFtk = $g_sSD & "pdftk.exe" ; path to pdftk.exe (and libiconv2.dll) Local $sCurrWorkingDir = @WorkingDir FileChangeDir($g_sBurstPath) ; it has to change for reasons Local $iPID = Run(_quotePath($sPDFtk) & ' ' & _quotePath($sFile) & ' burst', "", @SW_SHOW, $STDOUT_CHILD + $STDERR_CHILD) ; @SW_SHOW just to see "PDFtk is working" Local $sOutput, $sError While 1 $sOutput &= StdoutRead($iPID) $sError &= StderrRead($iPID) If @error Then ExitLoop Sleep(10) WEnd FileChangeDir($sCurrWorkingDir) ; back to normal EndFunc Func _FileInUse($sFilename) ; by Siao Local $aRet, $hFile $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _ "str", $sFilename, _ ;lpFileName "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ "dword", 0, _ ;dwShareMode = DO NOT SHARE "dword", 0, _ ;lpSecurityAttributes = NULL "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL "hwnd", 0) ;hTemplateFile = NULL $hFile = $aRet[0] If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1 $aRet = DllCall("Kernel32.dll", "int", "GetLastError") SetError($aRet[0]) Return 1 ; file in use (@error contains system error code) Else ;close file handle DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) Return 0 ; file not in use EndIf EndFunc This bursting took about 15000ms. If I activate line 8 "Sleep(13000)" then the bursting itself never needed more than 1000ms. Round about 13000ms delay is needed until bursting is acting very fast, otherwise needing about 15000ms.
      Then I tested _FileInUse() instead of _Burst() w/o Sleep(13000). This runs in 3ms. It seemes to me that FileCopy() and Burst() are interacting in a way I can't see.
      Any ideas? Regards, Conrad
      P.S. You need pdftk.exe and libiconv2.dll at @ScriptDir. This you can get free here: https://www.pdflabs.com/tools/pdftk-server/ (I'm not sure whether I'm allowed to attach it here).
       
    • joseLB
      By joseLB
      Suppose you want from time to time to update a destination folder  with new files that where created at a origin folder.
      It´s a kind of synchronization, where new files in origin must be "added" to destination. No worry about files that changed, just the new ones.
      The natural way:   FileCopy  ($originFolder  &  "\*.*"   ,   $destinationFolder  , $FC_NOOVERWRITE)
      After many tests, where I have 300 .jpg files in origin and no subfolders:
      If there is nothing at destination => OK, copy  is done. Now destination has 300 files. If I erase at destination 10 files in the middle (explorer, shift del)  and then filecopy =>  the 10 deleted files are NOT copied !! If I erase at destination 10 files "at the beginning"* (explorer, shift del)  and then filecopy =>  the 10 deleted files are copied !! = OK !! * "at beginning" = sorted by name, ascending, erase the 10 first ones.
      ** I tried $originFolder  &  "\*.*" , $originFolder&"\"  ,  $originFolder , and many other variants. The same for destination.
      So, what´s seems to me is that  when FileCopy finds the first file from origin  that exists at destination it stops to search.  That´s expected behavior?
      Best Regards
      Jose
    • Xulong
      By Xulong
      I have a script which is copying one file (the latest file among a bunch of structured folders) from one network device to another network device.
      The weird thing is the file copied successfully to the destination folder right after the FileCopy command ran but the script is hanging for another few minutes until it return with exit code 0.
      Below is my script:
      #include <MsgBoxConstants.au3> #include <File.au3> #include <Array.au3> #include <FileConstants.au3> copyfile() Func copyfile() Local $testLogFile = FileOpen(@ScriptDir & "\CopyMSI", 1) Local $BuildArtifacts = "\\vm-build\BuildArtifacts" Local $dServer = "\\VM-DEV\c$\Users\Administrator\Desktop" Send("#r") WinWaitActive("Run") ControlSend("Run", "", 1001, $BuildArtifacts) ControlClick("Run", "OK", 1) _FileWriteLog($testLogFile, "Opened Builds folder") ;trying to get the latest build folder and grab the latest msi file for copying WinWaitActive("BuildArtifacts") Local $FolderList = _FileListToArray($BuildArtifacts, "Grand-4.*") _ArraySort($FolderList, 1) Local $latestSEbuild = $FolderList[0] Sleep (5000) WinClose("BuildArtifacts") Local $sServer = $BuildArtifacts & '\' & $latestSEbuild & '\G4.msi' ; start copying file from build to destination folder FileCopy($sServer, $dServer, $FC_OVERWRITE) _FileWriteLog($testLogFile, "Copied MSI") EndFunc  
    • PINTO1927
      By PINTO1927
      Hello guys,
      I'm working on this script:
      Case $BTN Global $URL = FileOpenDialog("IMPORT FILE", $DESKTOP, "ALL FORMAT (*)", 4) $DIR_DEST = "C:\DIR-WORK\list\IMPORT_DOC\" DirCreate($DIR_DEST) Local $LINE For $t = 1 To $URL[0] _FileReadToArray($URL[$t], $LINE) For $u = 1 To $LINE[0] FileCopy($URL[$u], $DIR_DEST) Next Next the selected files via OpenFileDialog must be copied to the folder $DIR_DEST.