If/Else/Endif with compare (=)

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
TML15
Newbie
Posts: 4
Joined: Tue Feb 14, 2023 2:42 pm

If/Else/Endif with compare (=)

Post by TML15 » Tue Feb 14, 2023 3:00 pm

As a beginner, I've searched these forums and couldn't find a solution. I'm trying to compare a last file date with the current file date to see if they match. However, even when the match, the code goes to ELSE. (this part: If>%strNewString%=%strNewString2%)

Here is what I created:

Let>FilePath={"‪C:\Users\myname\Desktop\"}

//Find latest file
GetNewestFile>C:\Users\myname\Desktop\NEW*,strFile

//Find latest file date
FileDate>%strFile%,dFileDate

//Find latest file time
FileTime>%strFile%,strFileTime
//Read date & time of last file
ReadFile>C:\Users\myname\Desktop\Counter.txt,lastfile

Let>DateTime=%dFileDate%,%strFileTime%

Let>tmp1=,
StringReplace>lastfile,tmp1,,strNewString

Let>tmp2=,
StringReplace>DateTime,tmp2,,strNewString2

Let>%newfile%=%strNewString2%

If>%strNewString%=%strNewString2%
Goto>TheSame
Else


And these are the results:

1: //Set IGNORESPACES to 1 to force script interpreter to ignore spaces.
5: //Create File Path shortcut
7: Let>FilePath={"?C:\Users\myname\Desktop\"}
9: //Find latest file
10: GetNewestFile>C:\Users\myname\Desktop\NEW*,strFile
12: //Find latest file date
13: FileDate>C:\Users\myname\Desktop\NEW_2023_Feb.xlsx,dFileDate
15: //Find latest file time
16: FileTime>C:\Users\myname\Desktop\NEW_2023_Feb.xlsx,strFileTime
17: //Read date & time of last file
18: ReadFile>C:\Users\myname\Desktop\Counter.txt,lastfile
20: Let>DateTime=20230208,152926
22: Let>tmp1=,
23: StringReplace>lastfile,tmp1,,strNewString
25: Let>tmp2=,
26: StringReplace>DateTime,tmp2,,strNewString2
28: Let>%newfile%=20230208152926
30: If>20230208152926
=20230208152926

Any help would be appreciated!!
Thanks!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1389
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: If/Else/Endif with compare (=)

Post by Dorian (MJT support) » Tue Feb 14, 2023 3:27 pm

Your values match, so without going through the code my first observation is that you're missing the Endif at the very end.
Yes, we have a Custom Scripting Service. Message me or go here

TML15
Newbie
Posts: 4
Joined: Tue Feb 14, 2023 2:42 pm

Re: If/Else/Endif with compare (=)

Post by TML15 » Tue Feb 14, 2023 3:37 pm

Hi Dorian...thanks for the reply! I do have the Endif at the end. Would I need any brackets or quotations? Do I need to Dim any values? Does combining the date & time confuse the comparison? Thanks!!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1389
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: If/Else/Endif with compare (=)

Post by Dorian (MJT support) » Tue Feb 14, 2023 4:30 pm

Looking at it closer it looks like there are control characters in the %strNewString% variable. So it looks like it matches (to us as humans), but doesn't.

Code: Select all

30: If>20230208152926
=20230208152926
As opposed to :

Code: Select all

30: If>20230208152926=20230208152926
So you can either just use ReadLn and only read line 1 :

Code: Select all

ReadLn>C:\Users\myname\Desktop\Counter.txt,1,lastfile
Or you could continue to use ReadFile but then use Trim :

Code: Select all

ReadFile>C:\Users\myname\Desktop\Counter.txt,lastfile
trim>lastfile,lastfile
One other (unrelated) thing to note is that this :

Code: Select all

Let>%newfile%=%strNewString2%
..should be this :

Code: Select all

Let>newfile=%strNewString2%
These may help :

Using Variables and When to use Percent Symbols

User Defined Variables
Yes, we have a Custom Scripting Service. Message me or go here

TML15
Newbie
Posts: 4
Joined: Tue Feb 14, 2023 2:42 pm

Re: If/Else/Endif with compare (=)

Post by TML15 » Tue Feb 14, 2023 4:36 pm

that's great Dorian! Thanks...I give those a try!!

TML15
Newbie
Posts: 4
Joined: Tue Feb 14, 2023 2:42 pm

Re: If/Else/Endif with compare (=)

Post by TML15 » Tue Feb 14, 2023 5:00 pm

This is what worked:

ReadFile>C:\Users\myname\Desktop\Counter.txt,lastfile
trim>lastfile,lastfile

Thanks for all your help!! It was making me frustrated! :D

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1389
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: If/Else/Endif with compare (=)

Post by Dorian (MJT support) » Tue Feb 14, 2023 10:01 pm

You're very welcome.
Yes, we have a Custom Scripting Service. Message me or go here

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