trap an error on movefile

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
meclark
Newbie
Posts: 4
Joined: Tue Jun 12, 2007 11:44 pm

trap an error on movefile

Post by meclark » Tue Jun 12, 2007 11:50 pm

I want to move a file from folder a to folder b. Movefile> does work, but if the file is open, the file doesn't move and the macro doesn't pause or abbend. all well and good but I want to trap the return code from movefile>
If the move was successful, log the file name.
If the move was unsuccessful, do not log the file.

How do I do this?
I tried
If>(Movefile>%filename%,%Dir%)
WriteLn>%logfile%,filename
EndIf

but the WriteLn executes if the file is not moved.
Michael Clark

Aaron
Pro Scripter
Posts: 113
Joined: Mon Apr 09, 2007 1:35 am
Location: Wyoming

try this

Post by Aaron » Wed Jun 13, 2007 12:45 am

Code: Select all

Let>Dir=c:\temp\
Let>NewDir=c:\temp\NewFolder\
Let>LogFile=c:\temp\Log.txt
Let>FileName=myfile.txt
Let>NewFileName=myfile.bak

IfWindowOpen>%Dir%%filename%,END,NEXT

Label>NEXT
Movefile>%Dir%%fileName%,%NewDir%%NewFileName%
WriteLn>%LogFile%,%FileName%

Label>END
Aaron

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Fri Jun 15, 2007 2:57 am

Aaron's method may work for you, but it assumes the file name is in WindowName.

What sort of file are you moving? Text?

If it is text, why not

ReadFile>OldFile,TheFileText
Writeln>NewFile,result,TheFileText

then, possibly try to delete OldFile?

Many possibilities, but it depends on the type of file with which you are dealing.

meclark
Newbie
Posts: 4
Joined: Tue Jun 12, 2007 11:44 pm

Post by meclark » Fri Jun 15, 2007 4:08 pm

Thank you. I'm dealing with PDFs. So I'll try Aaron's method.
Michael Clark

meclark
Newbie
Posts: 4
Joined: Tue Jun 12, 2007 11:44 pm

Post by meclark » Fri Jun 15, 2007 7:13 pm

Unfortunately this doesn't work in this situation. I'm not sure if the cause is the file being opened by another user on a different workstation it's a network file, or the name of the file inside the loop.

I have the process and logging part in a loop

Repeat>k
Let>k=k+1
IfWindowOpen>file_names_%k%,END,NEXT
Label>NEXT
MoveFile>file_names_%k%,%StageDir%
WriteLn>%LogFile%,%file_names_%k%
Label>END
Until>k,file_names_count

file_names_%k% has the full path and file name in it. The open PDF has just the file name in the window header. Any ideas how to strip out the path part of file_named_%k%?
Michael Clark

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jun 15, 2007 7:35 pm

I'd do the move then check to see if the new file exists using IfFileExists>

Something like:

MoveFile>FilePathAndName,NewPathAndName
IfFileExists>NewPathAndName
//Do one thing
Else
//Do something else
EndIf

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Fri Jun 15, 2007 7:39 pm

try this:

let>PathAndFile=file_names_%k%
seperate>%PathAndFile%,\,segment
let>TheFile=Segment_%segment_count%

meclark
Newbie
Posts: 4
Joined: Tue Jun 12, 2007 11:44 pm

Post by meclark » Fri Jun 15, 2007 10:28 pm

let>PathAndFile=file_names_%k%
seperate>%PathAndFile%,\,segment
let>TheFile=Segment_%segment_count%

IfWindowOpen>*%TheFile%*,END,DoIt
Label>DoIt
MoveFile>file_names_%k%,%StageDir%
WriteLn>%LogFile%,%file_names_%k%
Label>END
EndIf
Until>k,file_names_count

Isolates the file name and if the application with the open the file is on the local workstation the above logic works. If it's a file on the network and the file is opened by the application on another workstation, this doesn't work. IfWindowOpen is local. It's not a true OS filesystem handle check.

darn, so close.
Michael Clark

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jun 15, 2007 11:34 pm

Let me state this more clearly. If you are working with networked files that may be opened by other networked computers, no variation of IfWindowOpen> is going to help you detect that a file on the network is open and therefore cannot be moved.

What will work is to move the file using MoveFile> then check the new location for the existance of the supposedly moved file using IfFileExists>. If the move was successful the new file will exist. If the move was unsuccessful because the file is in use by another workstation, the new file will not exist. Going back to your originally posted code sample, you had the right idea, just the wrong syntax.

If>(Movefile>%filename%,%Dir%)
WriteLn>%logfile%,filename
EndIf


It needs to be rewritten like this:

MoveFile>%filename%,%Dir%
IfFileExists>%Dir%
WriteLn>%logfile%,%filename%
Else
//Do something else
EndIf

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Jun 15, 2007 11:58 pm

JRL is correct, IfWindowOpen> has no relevance to this problem.

I think it might be prudent to check that a file with that name doesn't already exist in the destination directory (and maybe delete it) before going ahead with the move.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts