String in Filenames

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
om342
Newbie
Posts: 2
Joined: Wed Jun 13, 2012 4:46 pm

String in Filenames

Post by om342 » Wed Jun 13, 2012 5:15 pm

Hello Community :D

I get many Files from my Company and Friends so i decided to upgrademy personal File renaming Macro.

May Macro:

Code: Select all

Wait>3
Input>responseZaehler,Number of Files,
Input>responseDate,Date,
Input>responseName,Name,
Let>counter=1
Repeat>counter
Press ALT
Wait>0.4
Send>D
Wait>0.4
Send>U
If>counter<10>less
Let>Dateiname=%responseName% - %responseDate%0%counter%
Goto>next
Label>bigger
Let>Dateiname=%responseName% - %responseDate%%counter%
Label>next
Let>counter=%counter%+1
Release ALT
SendText>%Dateiname%
Wait>0.5
Press Enter
Release Enter
Press Down
Wait>0.5
Until>counter==%responseZaehler%+1
MessageModal>Files renamed
Now it just starts at the first File and iterates every File till the last is reached.
I think it is the opposite of optimized but i am trying to make it better.

Now i want to know if anyone can help me to make it better and further i want to analyse the Filename and get an important String plus a number and then merge them to another Layout.
Like:
[sda]randomFileName2131 --> randomFileName - 2131
Further there is a chance that the Files doesnt have the same name like:
[sda]randomFileName443
[fdsa]asrandomFileName9832
[a]randomFileName3123[xxaas]

I hope i could explain my problem and that someone can help me
cheers

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

Post by Marcus Tettmar » Thu Jun 14, 2012 2:29 pm

You can probably use RegEx. But we need to know what the pattern is. How random is the random part of the file. Does it follow some rules - i.e. is it only alphabetic characters or alphanumeric, how many characters long is it?

We can easily pull out the numeric bit at the end:

Let>filename=[sda]randomFileName2131
RegEx>\d*$,filename,0,matches,nm,0

That gets out the numbers at the end.

If we assume that the random part is always between the "]" and those numbers then we can get it with:

RegEx>(?<=]).*?(?=\d),filename,0,randstr_matches,nm,0

But looking at your examples it may be a little more complicated than that.

As I say, to hone this any further we need to understand the rules of the pattern.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

om342
Newbie
Posts: 2
Joined: Wed Jun 13, 2012 4:46 pm

Post by om342 » Thu Jun 14, 2012 9:06 pm

I have to say that it is quite difficult to find a pattern.
Here are some Filenames:
[Ad]_FileName_039_[as2134dfa]
[Ad]_FileName_040-041_[a213sdfa]
[era]Filename - 176 [734s]
[asa]Filename - 244 [734s]
[sdt]_Filename_321_[734s][ffasd34]

The Overall Pattern (most of the Files):
First there is a String in between [].
Then there is the Filename, but thats not the problem cause i wil take an overall filename for everything.
And then the complicated part, the number (extension number) right after the Filename. I need to extract this number to merge it with the overall name. The last random string in the [] is useless.

But there is another Problem, sometimes there are two numbers for one File ... like the example 040-041.

The final name should look like this:
Filename - 39
Filename - 40-41
Filename - 244
Filename - 321

I dont know if its possible to get these information but i hope there is a way.

User avatar
PepsiHog
Automation Wizard
Posts: 517
Joined: Wed Apr 08, 2009 4:19 pm
Location: Florida

Post by PepsiHog » Tue Jul 03, 2012 6:50 am

om342,

Here is what I have figured out so far. Though others may have better.
I'm just learning RegEx.

Code: Select all

let>MyFilename=[Ad]_FileName_039_[as2134dfa];[Ad]_FileName_040-041_[a213sdfa];[era]Filename - 176 [734s];[asa]Filename - 244 [734s];[sdt]_Filename_321_[734s][ffasd34]
Separate>MyFilename,;,MyFilename
let>asd=0
let>Final1=
While>asd<>%MyFilename_count%
let>asd=asd+1
let>Temp=MyFilename_%asd%
// Remove anything enclosed in []
RegEx>\[.*?\],Temp,0,MatchIt,nm,1,,Temp
// Remove all digits (numbers) to end with filename only
RegEx>\d(.*),Temp,0,Num,nm,1,,FN
// Replace hyphen(-) with pound(#). (Later will change to hyphen)
StringReplace>Num_1,-,#,Num_1
// Remove anything that is not a letter from the filename(FN).
RegEx>[^a-zA-Z],FN,0,match,nm,1,,FN
// Replace Hyphen,underscore and space with simi-colon. (in number string)
RegEx>(-)|(_)|(space),Num_1,0,match,nm,1,;,Num_1
// Remove preceeding zeros.
RegEx>0(\d),Num_1,0,match,nm,1,$1,Num_1
// Replace # with hyphen(-).
StringReplace>Num_1,#,-,Num_1
// Store data
let>FN=%FN%-
ConCat>Final1,FN
ConCat>Final1,Num_1
StringReplace>Final1,space,;,Final1
EndWhile

mdl>%Final1%
This assumes your data is in one string separated by a simi.

The output of this macro is:

FileName-39;FileName-40-41;Filename-176;Filename-244;Filename-321;

You can easily separate this string with the separate command.

Someone else my have a better pattern to do more in one line with RegEx.
But me, I'm just learning still.

Hope this helps,
PepsiHog

edit- previously I could not figure out how to remove one simi. After posting I realized how I could do it. The above is an updated script.
Windows 7

PepsiHog. Yep! I drink LOTS of Pepsi (still..in 2024) AND enjoy programming. (That's my little piece of heaven!)

The immensity of the scope of possibilities within Macro Scheduler pushes the user beyond just macros!

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