Page 1 of 1

batch rename with n chars

Posted: 09 Apr 2014 16:53
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

Re: batch rename with n chars

Posted: 11 Apr 2014 20:22
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.)

Re: batch rename with n chars

Posted: 11 Apr 2014 20:30
by SkyFrontier
Please provide sample input/output for each, if script is an option for you.

Re: batch rename with n chars

Posted: 11 Apr 2014 20:56
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).

Re: batch rename with n chars Rename RegEx Regular Expressio

Posted: 11 Apr 2014 21:18
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)


.

Re: batch rename with n chars

Posted: 11 Apr 2014 21:26
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.

Re: batch rename with n chars

Posted: 19 Oct 2015 08:23
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;
 }