batch rename with n chars

Features wanted...
Post Reply
autocart
Posts: 1248
Joined: 26 Sep 2013 15:22

batch rename with n chars

Post by autocart »

would like to have, please:
keep n characters from beginning
keep n characters from end
delete n characters from beginning
delete n characters from end

autocart
Posts: 1248
Joined: 26 Sep 2013 15:22

Re: batch rename with n chars

Post by autocart »

obviously this wish is not supported or did I miss a way of how it can be done already????
It is more or less standard in many other proggies capeable or batch renaming, i.e. "FreeCommander" or stand-alone-batch-renamers like "Advanced renamer".

But ok, if this should not be then I would like to ask: Please give me the option to use string-altering methods like substr() on the filename. Thank you.

(I know I could build a whole script myself using renameitem() but then I do have to program the whole batch part on my own and I dont have the preview.)
Last edited by autocart on 11 Apr 2014 21:06, edited 1 time in total.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: batch rename with n chars

Post by SkyFrontier »

Please provide sample input/output for each, if script is an option for you.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

autocart
Posts: 1248
Joined: 26 Sep 2013 15:22

Re: batch rename with n chars

Post by autocart »

I am not exactly sure, but I think u are asking for examples. That could be a lot though, i.e. any string that is reoccuring at the beginning or end of a bunch of files and that I want to get rid of. Technically I can do that with search/replace but if I want to make sure that i do not accidently delete part of a filename if it should contain this substring a second time then I would have to go by the length of the string from a starting position (mostly beginning or end).

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: batch rename with n chars Rename RegEx Regular Expressio

Post by Stefan »

autocart wrote:would like to have, please:
keep n characters from beginning
keep n characters from end
delete n characters from beginning
delete n characters from end

You could do that by utilizing Regular Expressions.

For example:
- Match first four signs: .{4}
- Match the rest of the name: .+

Now use parentheses (...) to capture what you have matched:
- Match first four signs: (.{4})
- Match the rest of the name: (.+)

Then replace with the wanted ()-matching group: $1 or $2, while dropping the other.
That way you can achieve what you are after.


----------------------------------

With XYPlorer RegEx Rename you would use this like this:

keep 4 characters from beginning (here introduced a third group to match the extension additionally )
(.{4})(.+)(\..+) > $1$3


keep 4 characters from end ( 4 to keep, plus dot plus 3 signs extension = 8 )
(.+)(.{8}) > $2
OR
(.+)(.{4})(\..+) > $2$3


delete 4 characters from beginning
(.{4})(.+) > $2


delete 4 characters from end
(.+)(.{4})(\..+) > $1$3




Note: this are simple regexes.
For files names with dots we have to make them more error save.
And if you learn how to use RegEx you can improve that expressions even more and more.


- - -



If you need more, you can always incorporate a third-party,
standalone portable, free renaming tool like SIREN:

Use Siren with XYplorer
http://scarabee-software.net/forum/viewtopic.php?id=228


Download:
http://scarabee-software.net/en/download.html
I am still using the old v2 due to its small file size ;-)


And for even more advantage tasks (better multi-steps handling, PascalScript), use den4b ReNamer. (http://www.den4b.com/?x=products)


.

autocart
Posts: 1248
Joined: 26 Sep 2013 15:22

Re: batch rename with n chars

Post by autocart »

THX, Stefan!!
Stefan wrote:Note: this are simple regexes.
For files names with dots we have to make them more error save.
I tried various filename formats with dots. Your "simple" regexes always worked perfectly as intended. I could not find any format where it would have not worked.
Thx again.

EDIT:
OK, anyway trying to make it more safe and make it work with filenames with or w/o extension and with "too short" file names:
keep 4 characters from beginning
1.attempt: ^(.{4})(.+?)(\.[^.]+)?$ > $1$3
current attempt:
^(.{4})(.*)(\.[^.]+)$|^([^.]{4})([^.]*)$ > $1$3$4

keep 4 characters from end
1.attempt: ^(.+?)(.{4})(\.[^.]+)?$ > $2$3
current attempt:
^(.*)(.{4})(\.[^.]+)$|^([^.]*)([^.]{4})$ > $2$3$5

delete 4 characters from beginning
1.attempt: (.{4})(.+) > $2
current attempt:
^(.{4})(.+)(\.[^.]+)$|^([^.]{4})([^.]+)$ > $2$3$5

delete 4 characters from end
1.attempt: ^(.+?)(.{4})(\.[^.]+)?$ > $1$3
current attempt:
^(.+)(.{4})(\.[^.]+)$|^([^.]+)([^.]{4})$ > $1$3$4
or possibly
^(.+?)(.{4})??(\.[^.]+)?$ > $1$3

And then one could add these to the user commands with the result as in the picture. In the following preview the pattern can still be edited i.e. in order to set the number of 4 chars to 3 or 5.
Attachments
user-rename-commands.jpg
user-rename-commands.jpg (45.71 KiB) Viewed 3077 times

autocart
Posts: 1248
Joined: 26 Sep 2013 15:22

Re: batch rename with n chars

Post by autocart »

UPDATE:
Currently used code - with dynamic user input:
(Copy and Use at own risk!)

1) Delete X characters from the end:

Code: Select all

$nrOfChrs = input("Number of characters to delete from end:",,,,"cancel");
 if (($nrOfChrs + 0) > 0) {
  rename r, "^(.*)(.{" . $nrOfChrs . "})(\.[^.]+)$|^([^.]+)([^.]{" . $nrOfChrs . "})$ > $1$3$4", p, , 65;
 }
2) Delete X characters from the beginning:

Code: Select all

$nrOfChrs = input("Number of characters to delete from end:",,,,"cancel");
 if (($nrOfChrs + 0) > 0) {
  rename r, "^(.{" . $nrOfChrs . "})(.*)(\.[^.]+)$|^([^.]{" . $nrOfChrs . "})([^.]+)$ > $2$3$5", p, , 65;
 }
3) Keep X chararcters from the end:

Code: Select all

$nrOfChrs = input("Number of characters to delete from end:",,,,"cancel");
 if (($nrOfChrs + 0) > 0) {
  rename r, "^(.*)(.{" . $nrOfChrs . "})(\.[^.]+)$|^([^.]*)([^.]{" . $nrOfChrs . "})$ > $2$3$5", p, , 65;
 }
4) Keep X characters from the beginning:

Code: Select all

$nrOfChrs = input("Number of characters to delete from end:",,,,"cancel");
 if (($nrOfChrs + 0) > 0) {
  rename r, "^(.{" . $nrOfChrs . "})(.*)(\.[^.]+)$|^([^.]{" . $nrOfChrs . "})([^.]*)$ > $1$3$4", p, , 65;
 }

Post Reply