Rename File Names
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Rename File Names
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.
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.
To see the attached files, you need to log into the forum.
-
highend
- Posts: 14942
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Rename File Names
Em,
a foreach loop, regexreplace() + attrstamp()?
I posted dozens of scripts that use the first two of them...
a foreach loop, regexreplace() + attrstamp()?
I posted dozens of scripts that use the first two of them...
One of my scripts helped you out? Please donate via Paypal
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
Hi Highend,highend wrote: a foreach loop, regexreplace() + attrstamp()?
I posted dozens of scripts that use the first two of them...
Thanks for your tips. I'll search around.
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
If I knew how to do it, I won't ask in the first place.
Please help. Thanks.
Please help. Thanks.
-
highend
- Posts: 14942
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Rename File Names
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);
}One of my scripts helped you out? Please donate via Paypal
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
Hi Highend,
My script is far simpler.
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);
}
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
The script above is named rename.xys.
How to assign a hotkey, say, ALT+R, to rename.xys?
How to assign a hotkey, say, ALT+R, to rename.xys?
-
highend
- Posts: 14942
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Rename File Names
Code: Select all
rename r,"January (\d{1}), (\d{4}) > $2.11.0$1";
rename r,"January (\d{2}), (\d{4}) > $2.11.$1";Assign the script to a user defined command and the keyboard shortcut to it is in the same gui
One of my scripts helped you out? Please donate via Paypal
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
Hi Highend,
Thanks a million.

Thank you for taking time to go thru my script and found a serious error.highend wrote:These are wrong, January != 11
Thanks a million.
Got it. Thank you for perfect guidance.highend wrote:Assign the script to a user defined command and the keyboard shortcut to it is in the same gui
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
Hi Highend,
I wish to learn more about xyscript...
What are the differences between our scripts?
Is my script anything wrong?
Thank you.
I wish to learn more about xyscript...
What are the differences between our scripts?
Is my script anything wrong?
Thank you.
-
FluxTorpedoe
- Posts: 906
- Joined: 05 Oct 2011 13:15
Re: Rename File Names
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.
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
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.)
}Hope this helps,
Flux
• Scripts: Session Manager
| SlideShow | Collection Manager | Power Launcher | Akelpad syntax highlighting | ...
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
Hi Flux,
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.
I'm humbled and impressed by your regex tricks.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 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.
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
Hi Flux,
Yes, it did display the correct filename.
But the filename didn't change. No action has been taken. Why?
Code: Select all
text $filename; // Your action (rename, etc.)But the filename didn't change. No action has been taken. Why?
-
highend
- Posts: 14942
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: Rename File Names
No action has been taken. Why?
Just replace the current text $filename; line with your renaming actions.
One of my scripts helped you out? Please donate via Paypal
-
armsys
- Posts: 557
- Joined: 10 Mar 2012 12:40
- Location: Hong Kong
Re: Rename File Names
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);
}To see the attached files, you need to log into the forum.
XYplorer Beta Club