Newest File Name

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
nbell
Newbie
Posts: 11
Joined: Mon Nov 11, 2002 2:51 pm
Location: China
Contact:

Newest File Name

Post by nbell » Wed Mar 05, 2008 5:14 am

I would like to automate a process where by I download one file per day from a remote location.

The files are always named "nathan02292008.doc" with the numbers being the date the file was written/saved/uploaded.

I live in China and the files are stored on a computer in the States so there is almost a day difference between the two locations.

I have been toying with dates and date differences and such but if the file I want happens to be uploaded on the 30th and I am already into the 2nd the time variables associated with the file name are causing me major grief...

As I was sitting thinking about this (into day four on this by the way) it hit me that if I could some how just download the 'newest' file in the directory each day, I would be home free... that would simplify things tremendously...

Is there a way to do such a thing?

Thanks in advance!
May I live my life so the worth of my life
Far outlasts the Years of my life!

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

Post by JRL » Wed Mar 05, 2008 6:12 am

Assuming your date is always newer than the file date by at least one day, would this not work?

Code: Select all


Day>dd
Month>mm
Year>yyyy
Let>today=%mm%/%dd%/%yyyy%

Label>Start
Sub>today,1
Midstr>today,1,2,mm
Midstr>today,4,2,dd
Midstr>today,7,4,yyyy
IfFileExists>[Path]\nathan%mm%%dd%%yyyy%.doc,Continue,Start

Label>Continue

//Etc........
What kind of connection do you have to the server? Can you do a DOS dir command? If yes you could do something like:

Code: Select all

Run>cmd /c dir [drive]\[path]\*.* /o-d /b >%TEMP_DIR%dirlist.txt
ReadLn>%TEMP_DIR%dirlist.txt,1,filename
//filename is the name of the newest file in the folder

nbell
Newbie
Posts: 11
Joined: Mon Nov 11, 2002 2:51 pm
Location: China
Contact:

Post by nbell » Wed Mar 05, 2008 10:09 am

Assuming your date is always newer than the file date by at least one day, would this not work?
My date is ALWAYS newer than the file date by at least one day... and of course up to three days on weekends.

I ran what you gave me through the step by step and one of the problems are the date "/" slash marks... The file name does not have them. So I removed them from the "let>today=%mm%/%dd%/%yyyy%" and I ended up with a good number except the leading zero is gone. So today being 03/05/2008 I get 3052008 and the file names always have the leading zero... or in cases where we are into October, November or December they have the 10, 11 and 12 respectively..

This is basically the same problem I have been having... I used a little different command sequence than you did but I have not been able to get the clear number showing mmddyyyy.

Another problem I have is what to do if today is the 1st of the month. After the Sub>dd,1 routine it shows 0 not the last day of the preceding month which in February this year would have been the 29th. The same thing with months... i.e. January... Sub>mm,1 will result in 0 not 12.

And I thought this was all going to be so easy!

Your loop concept to reduce the date by one day each time through is great however...

Thanks so much for taking the time to help me resolve this! I wish I new VB... I would imagine it's pretty simple with that..
May I live my life so the worth of my life
Far outlasts the Years of my life!

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

Post by Marcus Tettmar » Wed Mar 05, 2008 10:39 am

Sub> can subtract days from a DATE to return a DATE, or a number from a number. DD is a number, not a date, which is why you're left with zero. 1-1 is zero. 1 is not a date.

1. To subtract 1 day from current date and return the day portion:

Code: Select all

//If you use VBScript functions you need these two lines first so put them at the top of your script
VBSTART
VBEND

VBEval>Day(date()-1),dd
2. As you have noticed, you may sometimes end up with 1 digit. So you need to left-pad the result with zeros:

Code: Select all

VBEval>Right("0" & "%dd%",2),dd
This adds a zero to the left of the value in the dd variable, but then takes off only the last two characters. So we end up with any number less than 10 being prefixed with a zero.

Both the above techniques have been discussed here on the forum. There's also a useful article on working with dates and variables here:

http://www.mjtnet.com/blog/2006/01/23/u ... variables/
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
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Mar 05, 2008 2:09 pm

I ran what you gave me through the step by step and one of the problems are the date "/" slash marks... The file name does not have them. So I removed them from the "let>today=%mm%/%dd%/%yyyy%" and I ended up with a good number except the leading zero is gone. So today being 03/05/2008 I get 3052008 and the file names always have the leading zero...
As Marcus pointed out the Sub> function will work very nicely with dates. When you removed the slashes from the assignment of the Today variable you turned the variable into a number rather than a date. The slashes need to be there for Sub> to remove one day from a date. After the Sub> function are three Midstr> lines. Those lines take the new date and remove the slashes because as you mentioned, there are no slashes in your file name. In fact there are no slashes in any file name since they are an illegal file name character.

My concern with using VBScript is, as you found, VBScript is subject to date time regional settings. In some cases you might end up with one digit days one digit months and 2 digit years. A lot of extra fooling around. Macro Scheduler's built in functions Month>, Day>, and Year> always return a 2 digit month and day and a 4 digit year. Very convenient and very reliable.

If you try the script as I posted it (without making alterations before you try it) I think you'll find it does what you want.

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

Post by Marcus Tettmar » Wed Mar 05, 2008 2:19 pm

Um, just for clarity, VBScript is no more sensitive to regional formats than anything else:

VBEVal>date-1,yesterday

VBEval>Year("%yesterday%"),yyyy
VBEval>Month("%yesterday%"),mm
VBEval>Day("%yesterday%"),dd

Now format the date using those parts however you like.

Actually JRL's script is more sensitive to regional date format since the date passed to the Sub command needs to match your regional settings.

You have the choice.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

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

Re: Newest File Name

Post by Me_again » Wed Mar 05, 2008 2:37 pm

nbell wrote:As I was sitting thinking about this (into day four on this by the way) it hit me that if I could some how just download the 'newest' file in the directory each day, I would be home free... that would simplify things tremendously...
Here are two threads with examples of identifying the newest file in a directory (without straying into date arithmetic):

http://www.mjtnet.com/forum/viewtopic.php?t=2240

http://www.mjtnet.com/forum/viewtopic.php?t=3457

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

Post by JRL » Wed Mar 05, 2008 3:27 pm

Actually JRL's script is more sensitive to regional date format since the date passed to the Sub command needs to match your regional settings.
Thanks, I didn't know that.

So the line VBEval>Day(date()-1),dd loses the leading zero (today is 05) because you have subtracted one from the day and VBScript trashes the zero during the subtraction and not because of regional settings. I guess if I'd bothered to try this line I would have known that since my regional settings provide 2 digit days and I get a one digit result.

In any case, nbell, let us know if any of this helps. As you can see, we're here for you :)

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