rename files with wild cards

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
bbrink
Pro Scripter
Posts: 71
Joined: Thu Dec 31, 2009 2:36 am
Location: Minnesota
Contact:

rename files with wild cards

Post by bbrink » Thu Dec 31, 2009 3:19 am

Hello,

I am evaluating version 11 and my problems are likely self induced...

I have a bunch of files where the names include a date section like this:

FileNameA_20091224.exe
FileNameA_20091220.exe
FileNameB_20091220.exe

I need to find the most recent version of each and move it to a new folder giving a new file name that drops the last part of the fiel name, like this:

FileNameA.exe
FileNameB.exe

Any existing file can be overwitten, since it will be older.

I tried the RenameFile command first, figuring I'd worry about dates and moving files later but rename it does not seem to work with wild cards. Perhaps I mis-used it?

RenameFile>K:\Acme\FileNameA?????????.exe, K:\Acme\FileNameA.exe

I tried debugger but it didn't display anything in the watch window except my machine info / username etc.

Bye the way, how does one clear the watch list without closing the editor?

Thanks in advance,

Bob

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Dec 31, 2009 4:17 am

You are correct, those wildcards will not work with Rename>

Here are two approaches for renaming that you might consider.....

1. You could use RegEx> to extract the "core" filename, everything to the left of the "_" character.

2. Instead of RegEx, you could use Position> to calculate the position of the "_" character and then use MidStr> to extract the "core" filename. You would use the Position value as the string ending point reference in MidStr>.

-----------------
I have no time to provide example now. I would probably use the RegEx command, only one line needed, very fast.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

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:05 am

By most recent I assume you mean the one with the most recent date in the filename, not necessarily the most recently modified file, which could be different.

I would use GetFileList to retrieve a list of matching files (e.g. FileNameA*.exe). Sort the list so you have the one with the most recent date in the filename first (or last) and then rename that one.

This example sorts a file list:
http://www.mjtnet.com/forum/viewtopic.php?t=3414

So if you change the file spec to FileNameA*.exe you'll then have the "most recent" file name at the end of the list, so you just have to rename Files_%Files_Count%:

Let>theFile=Files_%Files_Count%
RenameFile>theFile,wherever/whatever.exe
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

bbrink
Pro Scripter
Posts: 71
Joined: Thu Dec 31, 2009 2:36 am
Location: Minnesota
Contact:

Post by bbrink » Mon Jan 18, 2010 3:50 pm

Marcus,

Perfect!

Thanks for the links, that got me going in the right direction.

Bob

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