Page 1 of 2
Rename File Names
Posted: 08 Nov 2015 06:51
by armsys
Dear XY Script Gurus,
I seek your help.
How to accomplish the following 9 tasks to all selected files with a script (named "rename.xys"):
[01] Apply regex November (\d{1}), (\d{4}) > $2.11.0$1
[02] Apply regex November (\d{2}), (\d{4}) > $2.11.$1
[03] Apply regex December (\d{1}), (\d{4}) > $2.12.0$1
[04] Apply regex December (\d{2}), (\d{4}) > $2.12.$1
[05] Apply regex January (\d{1}), (\d{4}) > $2.11.0$1
[06] Apply regex January (\d{2}), (\d{4}) > $2.11.$1
[07] Apply regex [ ]-[ ] > [ ]
[08] Apply regex [ ][ ] > [ ]
[09] Set Attribute to non-readonly
How to hotkey the rename.xys?
Thank you for your help.
Re: Rename File Names
Posted: 08 Nov 2015 08:57
by highend
Em,
a foreach loop, regexreplace() + attrstamp()?
I posted dozens of scripts that use the first two of them...
Re: Rename File Names
Posted: 08 Nov 2015 08:59
by armsys
highend wrote:
a foreach loop, regexreplace() + attrstamp()?
I posted dozens of scripts that use the first two of them...
Hi Highend,
Thanks for your tips. I'll search around.
Re: Rename File Names
Posted: 08 Nov 2015 14:58
by armsys
If I knew how to do it, I won't ask in the first place.
Please help. Thanks.
Re: Rename File Names
Posted: 08 Nov 2015 15:27
by highend
Code: Select all
setting "BackgroundFileOps", 0;
foreach($item, "<get SelectedItemsNames |>") {
attrstamp("r", 4, $item);
$newName = regexreplace($item, "^(Book) (November) (\d{2}), (\d{4})(\.[^.]+$)", "$1 $4.11.$3$5");
$newName = regexreplace($item, "^(Book) (November) (\d{1}), (\d{4})(\.[^.]+$)", "$1 $4.11.0$3$5");
// Same procedure for all other months...
$newName = regexreplace($newName, "\s-?\s", " ");
renameitem($newName, $item);
}
Re: Rename File Names
Posted: 08 Nov 2015 16:32
by armsys
Hi Highend,
My script is far simpler.
Code: Select all
global $File, $FileName, $FilePath, $FileExt;
rename r,"November (\d{1}), (\d{4}) > $2.11.0$1";
rename r,"November (\d{2}), (\d{4}) > $2.11.$1";
rename r,"December (\d{1}), (\d{4}) > $2.12.0$1";
rename r,"December (\d{2}), (\d{4}) > $2.12.$1";
rename r,"January (\d{1}), (\d{4}) > $2.11.0$1";
rename r,"January (\d{2}), (\d{4}) > $2.11.$1";
rename r,"November (\d{4}) > $1.11";
rename r,"December (\d{4}) > $1.12";
rename r,"[ ]*-[ ]* > ";
attrstamp("r",4);
}
Re: Rename File Names
Posted: 08 Nov 2015 16:33
by armsys
The script above is named rename.xys.
How to assign a hotkey, say, ALT+R, to rename.xys?
Re: Rename File Names
Posted: 08 Nov 2015 16:49
by highend
Code: Select all
rename r,"January (\d{1}), (\d{4}) > $2.11.0$1";
rename r,"January (\d{2}), (\d{4}) > $2.11.$1";
These are wrong, January != 11
Assign the script to a user defined command and the keyboard shortcut to it is in the same gui
Re: Rename File Names
Posted: 08 Nov 2015 22:51
by armsys
Hi Highend,
highend wrote:These are wrong, January != 11
Thank you for taking time to go thru my script and found a serious error.
Thanks a million.
highend wrote:Assign the script to a user defined command and the keyboard shortcut to it is in the same gui
Got it. Thank you for perfect guidance.

Re: Rename File Names
Posted: 08 Nov 2015 23:36
by armsys
Hi Highend,
I wish to learn more about xyscript...
What are the differences between our scripts?
Is my script anything wrong?
Thank you.
Re: Rename File Names
Posted: 09 Nov 2015 01:56
by FluxTorpedoe
Hi’
To expand on what
highend has already posted, here’s a tiny "Date converter".
It can be run on all files at once and takes care of automatically translating the months into digits, while adding a leading zero to the days or months when need be.
I quickly arranged it for your case
"AnyName Month ##, ####.ext", so obviously it would need to be adjusted/strengthened if the naming scheme differs (e.g. different order, date-like digits in name, "-" instead of "," etc.).
Also there’s no non-date renaming (extra spaces, etc.). Just replace the current
text $filename; line with your renaming actions.
Code: Select all
"Date converter"
$l_months = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
foreach ($filename, <get SelectedItemsNames |>) { $i=1;
foreach ($month, $l_months) {
$filename = regexreplace(regexreplace($filename, "\b".$month."[a-z]+\b (\d\d*), (\d{4})", "$2.".$i++.".$1"), "\b(\d)\b", "0$1");
}
text $filename; // Your action (rename, etc.)
}
Note: A break/continue could be added to spare a few CPU cycles, but XY’s regex is very efficient, so…
Hope this helps,
Flux
Re: Rename File Names
Posted: 09 Nov 2015 03:52
by armsys
Hi Flux,
FluxTorpedoe wrote:It can be run on all files at once and takes care of automatically translating the months into digits, while adding a leading zero to the days or months when need be.
I'm humbled and impressed by your regex tricks.
I see you use $i to get the month number.
I see you use \b(\d)\b to construct a zero-padded 2-digit day number.
Thanks a million.
Re: Rename File Names
Posted: 09 Nov 2015 04:52
by armsys
Hi Flux,
Code: Select all
text $filename; // Your action (rename, etc.)
Yes, it did display the correct filename.
But the filename didn't change. No action has been taken. Why?
Re: Rename File Names
Posted: 09 Nov 2015 08:20
by highend
No action has been taken. Why?
Just replace the current text $filename; line with your renaming actions.
Re: Rename File Names
Posted: 09 Nov 2015 10:18
by armsys
The script fails to rename the selected filenames, except the first one.
Code: Select all
setting "BackgroundFileOps", 0;
$l_months = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
foreach ($filename, <get SelectedItemsNames |>) { $i=1;
foreach ($month, $l_months) {
$NewName = regexreplace(regexreplace($filename, "\b".$month."[a-z]+\b (\d\d*), (\d{4})", "$2.".$i++.".$1"), "\b(\d)\b", "0$1");
}
renameitem($NewName, $filename);
}