Rename based on content?

  • Thread starter B. R. 'BeAr' Ederson
  • Start date
B

B. R. 'BeAr' Ederson

Hallo,

I'm looking for a *command line* utility to rename text files based on
an evaluation of the file content (using regular expressions). Of all
the utilities I know, THE Rename comes closest with its 'Replace with
file's content' option. Unfortunately, it only uses the first line of
the file. I need to use some text from the middle of the file. (Even
from the middle of a line, coming to this...)

The files I'd like to rename are derived from a large file split into
chapters using the CSplit utility (http://unxutils.sourceforge.net).
So it would be a solution, too, if someone knew another Split program
which supports RegEx naming of the output files. (Based on file content,
of course.)

No need to suggest scripting, though. I'll do a quick hack, anyway, if
nobody comes up with a better solution. ;-)

Thanks,
BeAr
 
B

B. R. 'BeAr' Ederson

On Sun, 1 May 2005 18:17:07 +0200, B. R. 'BeAr' Ederson wrote:

[Command line rename by RegEx of file content]
Seems, my request didn't ring a bell about some secret (or not so
secret) tool doing the above.

I finally got a satisfying solution, which I present here in case
someone else has the need to do such a rename. I already mentioned
the CSplit *nix tool. I used it to split the large source file to
chapters. The next tool needed is a decent Grep. I generally use
the version on the same web site as above mentioned CSplit:

http://unxutils.sourceforge.net

The missing link to get Grep output into a command line is the *very*
useful LMod tool by Horst Schaeffer:

http://home.mnet-online.de/horst.muc

All work together in this SplitRen.bat:
If "%1a" == "a" Goto End
If "%2" == "ReEntry" Goto Param
csplit -f~tmp -n3 -s -z %1 /###RegEx###/+0 {*}
Del ~r.bat
For %%i In (~tmp???) Do Call SplitRen.bat %%i ReEntry
call ~r.bat
Del ~r.bat
Goto End
:param
grep.exe -0 -h ###RegEx### %1 | grep.exe -o ###RegEx### | lmod.com cmd /c ren %1 "[$1].txt" >> ~r.bat
:End

-> The last but one line is very long. (Starts with grep.exe and ends
with ~r.bat. - Just in case it wraps in some readers...)

One needs to replace ###RegEx### with appropriate search strings. And all
parameters need to be checked on necessity and fitness for the purpose.

A few additional comments:

CSplit splits to chapter files. The first Grep selects a line, the second
a string on that line. I use a string which is already enclosed within
quotations. That's why I can tell LMod to extract the first (and only)
'word', i.e. the whole string inside (but without) the quotation chars.
The cmd /c part ensures the use of the correct Ren command. (The Ren of
Command.com only supports short file names.)

The ReEntry part is just for fun. Of course I could have done it using
two different batches, too. ;-)

BeAr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top