IfFileExists> Then run a program

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

IfFileExists> Then run a program

Post by kriemer » Tue Jan 12, 2010 4:05 am

I am out of my depth here.

I think the principal command is "IfFileExists>" but I can't get it to do what I want (which is pretty much as below):

Test if file "File1.csv" exists, if FALSE run ABC.exe,
Test if file "File2.csv" exists, if FALSE run DEF.exe,
Test if file "File3.csv" exists, if FALSE run GHI.exe,
Test if file "File4.csv" exists, if FALSE run JKL.exe,

Pause for 1 hour

Run the 4 tests again (3 times)

If on the final run any of the files do not exist then leave a message on the screen identifying that there is a missing file (and if possible which one).

Thanks in advance for any suggestions that point me in the right direction.

Regards

k

gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

IfFileExists>

Post by gdyvig » Tue Jan 12, 2010 5:41 am

Hi Kriemer,

You may also want to consider OnEvent which can tell you the instant the filename comes into existence or is deleted/renamed.

But for now, the IfFileExists method.

Here is some untested script:

Code: Select all


//Loop thru filelist 4 times hourly.
Let>hourcounter=0
Let>Missing=Missing:%CRLF%
Repeat>hourcounter
  Let>hourcounter=hourcounter+1
  IfFileExists>File1.csv
     //Do nothing
  else
     Let>Missing=%Missing%(%hourcounter%)File1.csv %CRLF%,
     Run>ABC.EXE
  end>
  //Same thing for File2.csv
  //Same thing for File3.csv
  //Same thing for File4.csv
  //wait 1 hour (3600 seconds)
  wait>3600
Until>hourcounter>4
//Final message
MessageModal>%Missing%


Hope this gets you started.

Gale

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Tue Jan 12, 2010 4:13 pm

I added the following code to close the message window each time the script is run, i.e., every 24 hours.

Code: Select all

IfWindowOpen>Macro Scheduler Message
CloseWindow>Macro Scheduler Message
 else
//Do nothing
endif>

****YOUR CODE****
I found my original problem in running IfFileExists> was that I enclosed the path and file name in quotes. Doesn't work that way.

Thanks for the help.

k

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Tue Jan 12, 2010 7:12 pm

Is it possible to string IfFileExists> so that the script ends when all File1-4.csv exist?

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

Post by JRL » Tue Jan 12, 2010 8:39 pm

One way is something like this:

Code: Select all

Label>Start

///Whatever code

IfFileExists>File1.csv,,Start
IfFileExists>File2.csv,,Start
IfFileExists>File3.csv,,Start
IfFileExists>File4.csv,,Start
//Close script
Exit>0

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Tue Jan 12, 2010 10:45 pm

JRL, et al;

I am at a loss as to how to integrate your code into what I have at this point. The purpose of the new code would be to end the script if all files are present. No message is necessary as all files exist (scripts have successfully completed).

This is the code as I'm currently working with it:

Code: Select all

DeleteFile>File1.csv - File4.csv
//Delete Message Window
  IfWindowOpen>Macro Scheduler Message
     CloseWindow>Macro Scheduler Message
  else
     //Do nothing
  endif>

Let>hourcounter=0
Let>Missing=Missing:%CRLF%
Repeat>hourcounter
  Let>hourcounter=hourcounter+1
  IfFileExists>File1.csv
     //Do nothing
  else
     Let>Missing=%Missing%(%hourcounter%)File1.csv %CRLF%,
     Run>ABC.EXE
  end>
  //Same thing for File2.csv
  //Same thing for File3.csv
  //Same thing for File4.csv

/////This is what I'd like to do////////////////////////////////// 
IfFileExists>FILE1.csv AND FILE2.csv AND FILE3.csv AND FILE4.csv
EXIT> 
//////////////////////////////////////////////////////////////////

//Wait 1 hour (3600 seconds)
wait>3600
//Loop n times
Until>hourcounter>4
//Final message
MessageModal>%Missing%
Thanks in advance.

k

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

Post by JRL » Tue Jan 12, 2010 11:48 pm

Altered slightly and note the first comment.

Code: Select all

//Do you know that this will not delete the 4 files?
DeleteFile>File1.csv - File4.csv


//Delete Message Window
  IfWindowOpen>Macro Scheduler Message
     CloseWindow>Macro Scheduler Message
  else
     //Do nothing
  endif>

Let>hourcounter=0
Let>Missing=Missing:%CRLF%
Repeat>hourcounter
  Let>hourcounter=hourcounter+1
  IfFileExists>File1.csv
     //Do nothing
  else
     Let>Missing=%Missing%(%hourcounter%)File1.csv %CRLF%,
     Run>ABC.EXE
  end>
  //Same thing for File2.csv
  //Same thing for File3.csv
  //Same thing for File4.csv

/////This is what I'd like to do////////////////////////////////// 
IfFileExists>File1.csv,,Skip
IfFileExists>File2.csv,,Skip
IfFileExists>File3.csv,,Skip
IfFileExists>File4.csv,,Skip
EXIT>0
Label>Skip
//////////////////////////////////////////////////////////////////

//Wait 1 hour (3600 seconds)
wait>3600
//Loop n times
Until>hourcounter>4
//Final message
MessageModal>%Missing%

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Wed Jan 13, 2010 12:27 am

Yes I know it won't work as is, I was trying shorthand but apparently not very effectively (sigh).

Could you explain how the Label>skip code works? I'm confused.

Thanks

k

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

Post by JRL » Wed Jan 13, 2010 3:40 am

Yes I know it won't work as is, I was trying shorthand but apparently not very effectively (sigh).
I suspected that was the case which is why I didn't rewrite it for you but I still had to ask . I don't know what you know or don't know.
Could you explain how the Label>skip code works? I'm confused.
First you need to understand how IfFileExists> works.

From Help for IfFileExists>
IfFileExists>filename[,label_name[,false_label_name]]
statements
[ [Else
else statements]
Endif ]

If filename exists the first statements are executed. Otherwise the else statements are executed.
All Macro Scheduler If> functions have two modes of operation. We either have a one line If> test with one or two labels, the first label is processed when the test is true, the second label (which is optional) is processed when the test is false. Or, If> can have a multi-line test where rather than jumping to labels, we can add any code in the If,Then,Else pattern.

In the example posted for you, we are using the one line method and jumping to labels.

So in the line:
IfFileExists>File1.csv,,Skip
If the file "file1.csv" exists the script jumps to the first label name specified. In the example line there is no first label name so in effect, the script does nothing. If the file "file1.csv" does not exist, the script jumps to the second label name which in the example is the "Skip" label. If the first file does not exist we go to label "Skip" then wait a hour before continuing. There is no need to check for the existance of the other files because we only want to know when all the files exist so as soon as one does not exist we can exit the test. If the first file exists we go to the second line and see if "File2.csv" exists. If it does not we "Skip" and wait an hour if it does we check for the existance of "File3.csv"..... and so on.

Hope this is helpful.

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Wed Jan 13, 2010 4:01 am

Skip is perfectly clear now.

Many thanks for your help through this.

Regards

k

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Is the Syntax OK

Post by kriemer » Sun Jan 17, 2010 5:55 am

I am wondering if the syntax of the following code is correct. While everything appears to work, the editor highlighting makes me suspicious.

Code: Select all

***Do Some Stuff***
Let>loopcounter=0
Repeat>loopcounter
Let>loopcounter=loopcounter+1

  //Exit Script If ALL Files Exist AFTER First Loop (1x)
  if>loopcounter=2
    IfFileExists>C:\Users\File1,,Skip
    IfFileExists>C:\Users\File2,,Skip
    IfFileExists>C:\Users\File3,,Skip
    Exit>0
    Label>Skip
  else
    //Do nothing
  endif

***Do Somemore Stuff***

Until>loopcounter>3
Yes, I only want to run the test on the second loop.

My thanks as always.

k

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Re: Is the Syntax OK

Post by jpuziano » Sun Jan 17, 2010 7:32 am

kriemer wrote: ***Do Some Stuff***
Let>loopcounter=0
Repeat>loopcounter
Let>loopcounter=loopcounter+1

//Exit Script If ALL Files Exist AFTER First Loop (1x)
if>loopcounter=2
IfFileExists>C:\Users\File1,,Skip
IfFileExists>C:\Users\File2,,Skip
IfFileExists>C:\Users\File3,,Skip
Exit>0
Label>Skip
else
//Do nothing
endif
***Do Somemore Stuff***

Until>loopcounter>3

I am wondering if the syntax of the following code is correct. While everything appears to work, the editor highlighting makes me suspicious.
Hi kriemer,

Your code looks OK but you made me curious... so I pasted your code into the editor (MS version 11.1.19 on Windows 7) and the color syntax highlighting has an issue.

The endif and the first If> statement should be highlighted green similar to the above... but instead, it is fooled and pairs up the endif with the last of the three IfFileExists> lines. If the three IfFileExists> lines are deleted, color syntax highlighting works properly. Marcus, can you reproduce this problem?

Thanks for posting your suspicions kriemer.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jan 18, 2010 8:09 am

John,

This has been discussed before - if you use "inline" ifs - i.e. a true/false label in the If command, which are deprecated, then it will confuse the syntax highlighter which expects an Endif.

There's no neat solution to this. Idealistically we'd remove support for inline if statements altogether.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Jan 18, 2010 4:31 pm

mtettmar wrote:There's no neat solution to this. Idealistically we'd remove support for inline if statements altogether.
Thanks Marcus... not looking for removal of support for inline if statements as that would break a lot of old code.

I guess this will just have to remain a known issue.


Hi kriemer,

If you want the syntax highlighting to look right, you could add an EndIf line along with each of your IfFileExists> lines as in the code below. No difference in how it operates... other than the color syntax highlighting.

Take care

Code: Select all

***Do Some Stuff***
Let>loopcounter=0
Repeat>loopcounter
Let>loopcounter=loopcounter+1

  //Exit Script If ALL Files Exist AFTER First Loop (1x)
  if>loopcounter=2
    IfFileExists>C:\Users\File1,,Skip
    EndIf
    IfFileExists>C:\Users\File2,,Skip
    EndIf
    IfFileExists>C:\Users\File3,,Skip
    EndIf
    Exit>0
    Label>Skip
  else
    //Do nothing
  endif

***Do Somemore Stuff***

Until>loopcounter>3
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

kriemer
Pro Scripter
Posts: 57
Joined: Fri Oct 30, 2009 2:59 pm

Post by kriemer » Mon Jan 18, 2010 5:54 pm

jpuziano,

As the situation is know (and now known by me); and so long as the syntax highlighting is just a "curiosity" I'll leave my code as is.

Many thanks for the advice.

k

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