Renaming files

A collection of especially useful xplorer² topics and ideas. New users may find it helpful to look here before searching the other forums for information. >>>>>> Please post new material in the relevant forum. (New stuff posted here will be removed.) Thanks. -fg-

Moderators: fgagnon, nikos

User avatar
JamieG
Member
Member
Posts: 47
Joined: 2004 Oct 03, 11:43
Location: Paris, France

Ok Narayan, but which renamer do you want to document?

Post by JamieG » 2006 Apr 18, 17:53

Hi Narayan,

I'll be glad to help out if you can tell me which bulk-renamer(s) you want to document for those three examples.  Is it MRename1.7 or awxRenamer?  (I assume the last part of your posting, under the "****", refers to THERename and that you've got your examples for it worked out... the syntax you show, "?1*2 ", isn't from the RE world.)

The reason I ask is that I'll have to download one or both to see what particular RE syntax they implement.  There are quite a few, and though I can answer for perl's REs and the original Unix REs, I'm not familiar with Java's syntax, VB's, or any other family branches which have spawned throughout the MS world.  I imagine, though, that the basics are pretty much the same.

So tell me which ones you want the examples for and I'll get on it tomorrow.  (This is a good occasion anyway for me to install a bulk-renamer... up 'til now I've simply written perl programs when I've needed to rename a bunch of files in ways that x2 doesn't allow.  But that's a pretty clunky and lond-winded way to go about it, and awxRenamer sounds pretty good.)

User avatar
FrizzleFry
Platinum Member
Platinum Member
Posts: 1141
Joined: 2005 Oct 16, 19:09

Post by FrizzleFry » 2006 Apr 18, 21:51

A more robust solution for MRename to cap the first letter and lowercase the rest is:

Select: *1[A-Za-z]G{1}2*3
Rename: \1\U2\L3

Since MRename can only do single character search and replace I do not think there are general solutions that can be placed in the context menu for your 2 and 3 options. You can do string replacement in the MRename window using

Select: *1searchtext*2
Rename: \1replacetext\2

Update: Actually, MRename already has string search and replace in the QuickMenu: REPLACE-OTS first or last.  OTS means on the spot.  It uses the \X operator to request strings at the time of execution.

narayan
Platinum Member
Platinum Member
Posts: 1430
Joined: 2002 Jun 04, 07:01

Post by narayan » 2006 Apr 19, 02:15

Thanks, FrizzleFry! Yes, the option for string replacement is already there--Missed it!

There is a remote chance that the filename contains the same string multiple times. Then we have to use the same command 2-3 times to be sure. (or better, use another renamer).

***
@which renamer I was referring to-
JamieG, in my last post, I meant MRename. Since awxrename and MRename both have context menu-based conversion, we are concentrating on these two at the moment.

The other two (THE Rename and Lupas Renamer) have more readymade options, but they cannot add commands to the context menu of x2. But they do add a context menu option in x2, to send the selected files to their own GUI. (If the application is not already running, it will get launched, and then the selected files are loaded. Now you can rename them using the application's full GUI.)

THE Rename
is the most powerful: Huge number of readymade commands, freeform commands, RegEx, etc.; and some unusual operations like "caddy". Try it!

***
@RegEx or not:
What I described in the last post is not pure RegEx: MRename uses two masks, which use a combination of a RegEx string and MRename's own syntax  (The first mask is used to select the file, and also to chop up the name into "blocks". The second mask is used to rename the file by re-arranging those blocks, and if required, adding some text/number patterns).

***
@how much to document:
Since the applications have their own help file, all we have to do in our manual is to add special instructions (if any) about how to use the renamer with x2; and some scripts for additional options (like the ones we are discussing right now.)

User avatar
JamieG
Member
Member
Posts: 47
Joined: 2004 Oct 03, 11:43
Location: Paris, France

Some RE examples

Post by JamieG » 2006 Apr 19, 08:00

Problem 1 (medic5608d's) : strip off the first 15 bytes of each filename

A simple awxRename syntax could be the following:

$file=~s/^.{15}//;

Example:

  paramedic dave 01 Skybox 113.jpg -> 01 Skybox 113.jpg
  paramedic dave 01 Skybox 117.jpg -> 01 Skybox 117.jpg
  paramedic dave 02 Whatsitsbox 001.jpg -> 02 Whatsitsbox 001.jpg


= = = = = = = = = = = = = = = = = = = = = = = = = = = =

Problem 2 : Capitalize the first letter and lowercase the rest

Here is an appropriate awxRename script for this:

$file=~s/([A-Za-z])(.*)/\u$1\L$2/;

For example:

  0073apples.txt -> 0073Apples.txt
  0084ORANGES.TXT -> 0084Oranges.txt
  0099pEars.Txt -> 0099Pears.txt


Perhaps you want to get rid of the initial sequence number:

$file=~s/[^A-Za-z]*([A-Za-z])(.*)/\u$1\L$2/;

Run on similar filenames (we've added spacing or punctuation between the initial number and the text), this produces:

  0073 - apples.txt -> Apples.txt
  0084 ORANGES.TXT -> Oranges.txt
  0099.pEars.Txt -> Pears.txt


= = = = = = = = = = = = = = = = = = = = = = = = = = = =

Problem 3 : split a StuddlyCap string into separate words, keeping the upper-case letter of the first word only

This is a three-step operation.  Here is awxRename's script for doing this; it holds three regular expression replacement commands:

$file=~s/([A-Z])/ \l$1/g;$file=~s/^ //;$file=~s/([A-Za-z])/\u$1/;

In order, they:

1.  replace each upper-case letter by a space and its lowercase equivalent
2.  if the first step put a space at the start of the filename, remove it
3.  upper-case the first letter in the string

  WorksForMe.txt -> Works for me.txt
  3-1RenameAllFile.txt -> 3-1 Rename all files.txt


= = = = = = = = = = = = = = = = = = = = = = = = = = = =

Problem 4 : another StuddlyCap-to-words example

This is similar to the preceding example although this time we're not going to bother lower-casing all words following the first.  It serves to show a particularity of regular expressions: there are often many different ways of achieving a result.  Here are three examples.

The first one does the work in two steps, first sticking a space in front of each upper-case letter in the filename and then getting rid of any space which might have been added to the start.

$file=~s/([A-Z])/ $1/g;$file=~s/^ //;

The second is for RE afficionados and does it in one step.  It replaces all upper-case letters with a space and the lower-case equivalent except at the start of the filename:

$file=~s/(?<!^)([A-Z])/ $1/g;

The two previous examples would, however, turn "HiHo.Txt" into "Hi Ho. Txt".  If you know that the filenames you want to rename have extensions and you don't want to risk putting a space there, add an additional condition to the previous example: only add spaces as long as there's still a dot further along in the string.

$file=~s/(?<!^)([A-Z])(?=.*\.)/ $1/g;

Here's an example of the last one:

  ItWasADarkAndStormyNight.txt -> It Was A Dark And Stormy Night.txt
  WorksForMe.Txt -> Works For Me.Txt

User avatar
nikos
Site Admin
Site Admin
Posts: 14355
Joined: 2002 Feb 07, 15:57
Location: UK
Contact:

Post by nikos » 2006 Apr 19, 08:33

i haven't be following that discussion... regexps are totally unfamiliar and give me a headache -- i'd sooner write a full VBS script to do what i want :)

i just want to remind you that x2's regexp code isn't 100% perl compatible so before putting these things in the manual please check  them with x2 first to make sure they behave as indended

narayan
Platinum Member
Platinum Member
Posts: 1430
Joined: 2002 Jun 04, 07:01

Post by narayan » 2006 Apr 19, 09:35

Of course! all patterns are tested as we go.

BTW the expressions (so far, at least) are not specific to any flavor.

JamieG/FrizzleFry, we have discussed awxrenamer. We need equivalent scripts for MRename, so we understand its comparative strengths and weaknesses vis-a-vis awxrenamer.

It is interesting to note that both are capable of concatenating commands: In case of MRename, there seems to be a top limit of three, going by its GUI. On the other hand, awxrenamer seems to take more (unlimited?) commands, separated by a ";" delimiter.

So which is better, and why?

User avatar
JamieG
Member
Member
Posts: 47
Joined: 2004 Oct 03, 11:43
Location: Paris, France

Post by JamieG » 2006 Apr 19, 11:37

Nikos's points are well taken.  (Even the remark about getting headaches from using REs... they're not for the faint-of-heart! :spin:  But they're a pretty compact syntax for doing fairly complicated text manipulations.)

A few remarks:

- Nikos is right in cautioning about giving RE examples which wouldn't work in x2's RegExp facility.  I haven't used it because I don't search inside files, only by filenames.  But I imagine x2 relies on software libraries I know nothing about (Java or VBS) and I'm willing to bet that my last example uses perl-specific syntax.  I'll have a try at checking it against x2, but it'll have to wait until tomorrow when my head clears up (after the Arsenal-Villareal Champion's League match tonight  :party: ).

- The name of the product is "awxRename" (might as well get that one right if it's going in the manual).

- The developer can be contacted through his web site, http://arniworld.de/ or email address <development@arniworld.de>.  The site lists a whole bunch of awxXYZZY utilities that seem interesting.

I'll first finish looking at awxRename before trying MRename.  I didn't notice any help text for it this morning when I ran off the previous note to Narayan, but as far as I can gather, its concept is simplicity itself... one types in a small perl program.  If anybody can think of a really tough renaming problem, I'll see if I can produce an example of how one might achieve it in awxRename.

There are other issues with it that I want to check... where does it get the perl run-time system from, and which version?  How does it behave in different character set locales?  (It prepends all scripts with a "use locale;" directive which I don't have on my system, so I just zap it.  I only used non-accented English-text filenames.)

Anyway, I'll get back to you in a day or two.  Best, Jamie

BRX
Silver Member
Silver Member
Posts: 277
Joined: 2002 Feb 08, 12:12

Post by BRX » 2006 Apr 19, 12:17

It's nice you all seem to like this little gem too. There are so many good freeware tools deserving more attention. I will have a look at MRename too though.

JamieG and FrizzleFry, thanks for your very useful scripting help. Maybe you can help me out with something too.

I've tried lots of renamers (Lupas, 1-4a, THE Rename, Flexible File Renamer, Antrenamer, Joe and a few more. All the mentioned ones are quite good and have their own advantages. My personal fav still is Flexible File Renamer.

But all of them don't do a rename from an input text file where you can list multiple renames, preferably with two simple columns, e.g.

1234    4567
8910    1112

where all occurences of the first columns will be replaced with the respective ones in the second.

AntRenamer does it more or less but from within the GUI.

Could this somehow be done with awxrename? There seems to be the capability and also there's some example file for replacing German (single) Umlauts with "normal letters".

BTW, to conclude the renamer suggestion, here's just one I want to mention because (as awxrename as context renamer) it works quite differently than most others and might suit some needs better:

Oscar's Renamer

at http://www.mediachance.com/free/index.html

It also has a shell extension and can be started from the context menu but limited to the current folder.

Nothing to fancy (no regexp) but the contents of the folder is displayed as editable text file with some tools and even a macro function. Not on par with the other mentioned file renamers but maybe still interesting because of the different method.

narayan
Platinum Member
Platinum Member
Posts: 1430
Joined: 2002 Jun 04, 07:01

Post by narayan » 2006 Apr 19, 14:23

BRX,

Apparently you face the following situation:
1. You know the correct filenames, yet end up with files having wrong names.

2. You have a separate text file that describes a one-to-one correspondence of wrong and correct strings

3. This happens frequently.

4. Each time you have a different set of names (the old set cannot be reused.)

I was wondering if this is an interesting challenge, or does it have a real-life application, where this happens as a routine?

Secondly, how do you get the text file?
>> By typing them manually, or  
>> By manipulating some text output of a process

If it has to be typed in, you can do it in THE Rename, within the GUI (it does not allow text file import or copy-paste).

For that you can use the Edit | Abbreviations... menu option.
The window lets you define pairs of strings (search for; replace with).

And although you can't import a text file directly, here is the trick:

In THE Rename, you can save that abbreviation file. This is a text file, but has the extension "abr". It contains the same list that is shown in THE Rename's Abbreviation window, but delimited with þ (lowercase thorn, an Icelandic character, with  character code of þ).

The trick is to edit this file, and add as many strings as you want.
You also have to correct the following statement (number of records) given at one end of the file:

NumberOfAbbreviations=xx.

Now save the file and then re-open the file in THE Rename.
It will load the file correctly.

An alternative is to compose the statements in Excel. (Use concatenate function.)

Or someone could write a PERL program to create that abr file from a Tab-separated/comma-separated text file.

BRX
Silver Member
Silver Member
Posts: 277
Joined: 2002 Feb 08, 12:12

Post by BRX » 2006 Apr 19, 15:55

Well, there's a real life application for my request (at least for me).

I'm collecting daily cartoons from the net, lots of them are available at www.unitedmedia.com.

The downloaded gifs don't have a correct and sortable publishing date, so I rename them. And each file has a varying number which includes the date split up in various ways. It's annoying.

The thing is, for each file of the series the number the same for the same date, so e.g. the number 2006036638407 is in all files from the day 20060407. And it's only part of the file, too. So I would make the columns like that.

And yes, I'm working with copying and s&r to make a textfile. It's just easier to type all this in though the antrenamer procedure was a major help already.

Anyway, I imagine there are other means of application. So it's not only a challenge. ;-)

narayan
Platinum Member
Platinum Member
Posts: 1430
Joined: 2002 Jun 04, 07:01

Post by narayan » 2006 Apr 19, 16:03

If there is a common pattern (at least for a few files at a time), then THE Rename can chop the old name, extract different parts of the old name and rearrange them to give you the new name.

See "tokens" in help file.

User avatar
FrizzleFry
Platinum Member
Platinum Member
Posts: 1141
Joined: 2005 Oct 16, 19:09

Post by FrizzleFry » 2006 Apr 19, 20:00

A script to read a text file to do the rename is fairly trivial but it seems to me that you are defeating the purpose of these renaming tools if you are manually editing the filenames.  I think this is what Oscar's Renamer allows you to do.

With REs you can break the filename into fields which you can adjust to your liking. For example:

original filename: dilbert2006106560419.gif

awxRename (Perl) RE:
$file=~s/(.*?)(\d{4})(\d*)(\d\d)(\d\d)/$1 - $2 - $4 - $5 - $3/;

new filename: dilbert - 2006 - 04 - 19 - 10656.gif

Each set of parens corresponds to a $# variable

(.*?) - $1 - dilbert - characters up to
(\d{4}) - $2 - 2006 - the first 4 consecutive digits
(\d*) - $3 - 10656 - 0 or more digits
(\d\d) - $4 - 04 - 2 digits
(\d\d) - $5 - 19 - 2 digits

Notice the use of the ? in the first set of parens.  The ? makes the first operator non-greedy and it is why this operator stops matching at the first 4 consecutive digits.

BRX
Silver Member
Silver Member
Posts: 277
Joined: 2002 Feb 08, 12:12

Post by BRX » 2006 Apr 20, 14:57

Thanks FrizzleFry, your help will get me on my way I hope.

And no, it doesn't defy the "purpose of the rename tool" in this case. I'm pretty comfortable with text editing and solved lots of filehandling problems with x², 10 finger typing, a good text editor and batches.

But if I want to rename a month of unitedmedia cartoons I end up with, I don't know 900 + files. And I would have to make for each bunch of a day of the month a different renaming action. Even the length of the numbers isn't always the same. They're differently structured.

IMO it is much easier to have a "preconfigured" textfile where I have already the names and dates (or manipulated them with a text editor) and just add the strings for the to be replaced variables.

narayan
Platinum Member
Platinum Member
Posts: 1430
Joined: 2002 Jun 04, 07:01

Post by narayan » 2006 Apr 20, 16:21

Yes, that's what I meant: Find different patterns, and derive how to rename them using tokens. Save these formulas. Then, when you want to rename a bunch o files, run the appropriate formula.

User avatar
FrizzleFry
Platinum Member
Platinum Member
Posts: 1141
Joined: 2005 Oct 16, 19:09

Post by FrizzleFry » 2006 Apr 20, 18:06

BRX, so is your text file listing

oldname newname

pairs, or

searchtext replacetext

pairs?

Post Reply