PDA

View Full Version : Help renaming tons o' files.


Shalazar
01-04-2000, 07:16 PM
I'm not sure if the CNC has this capability or not.[nbsp][nbsp]I'm sure my FTP program doesn't, and thus I need some advice.

I had to redo my entire thumbnail gallery for 300 pictures. The catch?

1. Each thumbnail is delinieated from its full size pic's nomenclature by being preceded by a "th."[nbsp][nbsp]Therefore, the thumbnail corresponding to cc128.jpg (full size) would be thcc128.jpg.[nbsp][nbsp]This, for all intensive purposes, prevents me from bungling and overwriting a file I've worked on previously with the thumbnail.

2. However, the new list of thumbnails generated by Photoshop 5.5's web gallery batch function bear the same name as their corresponding full size image.[nbsp][nbsp]Therefore, I would have to manually rename each file with the "th" to keep my nomenclature correct -- OR, I would have to alter the code in each of the gallery web pages (which in MSWord is pretty simple with find/replace function).

But before resulting to the find/replace in the web pages themselves, I was wondering if there was a script, a cnc function, or an FTP function anywhere that can tack on a "th" to the start of a filename automatically.

As always -- Thanks!


------------------
Shalazar

www.charisma-carpenter.com (http://www.charisma-carpenter.com)
The Internet's Premier site for everything Charisma.

Charles Capps
01-05-2000, 01:45 AM
Lemme see...[nbsp][nbsp]Telnet:
mv * th*

DOS:
ren *.* "th*.*"

Or something like that.[nbsp][nbsp]That should work, I think.[nbsp][nbsp]That'll rename everything in the directory and precede it with an *, unless the proximity of the girlfriend is causing my brain to melt or something...[nbsp][nbsp]If so, pardon.[nbsp][nbsp]Recommend not to use on true content first.

urban
01-05-2000, 02:17 PM
You might be able to do it with a simple shell script...

for f in *.jpg; do
[nbsp][nbsp]mv $f th${f%}
done


[This message has been edited by urban (edited 01-07-00@11:34 am)]

Shalazar
01-05-2000, 02:23 PM
urban -

Thanks for the suggestion.[nbsp][nbsp]Now how do I go about implementing that?

Writing the script?[nbsp][nbsp]Where to put it? How to run it? etc.

------------------
Shalazar

www.charisma-carpenter.com (http://www.charisma-carpenter.com)
The Internet's Premier site for everything Charisma.

urban
01-05-2000, 06:09 PM
Sorry about that...

Be sure to test it on a temporary directory first.

1) Telnet to host.
2) Change to the directory with the images.
3) Create a file and insert the script:


for f in *.jpg; do
[nbsp][nbsp]mv $f th${f%}
done


5) Save script as myren.sh
6) To set script exec type: chmod u+x myren.sh
7) To run the script type: sh myren.sh

ALL files with a .jpg extenstion in the current directory should get renamed to a new file with a 'th' prepended to the beginning of the file name.[nbsp][nbsp]

Good luck.
[This message has been edited by urban (edited 01-05-00@6:14 pm)]

Shalazar
01-06-2000, 12:08 AM
urban -

You're a lifesaver!!![nbsp][nbsp]It works fast, and it works like a charm!

Thank you so much, I do appreciate your help and dilligence in helping find a working solution.

If you wouldn't mind e-mailing me (shalazar@charisma-carpenter.com), or posting here, I'd love to have an explanation of how the code works.[nbsp][nbsp]I'm not a super programmer, but I'm now very interested in the logic behind it!

------------------
Shalazar

www.charisma-carpenter.com (http://www.charisma-carpenter.com)
The Internet's Premier site for everything Charisma.

Shalazar
01-06-2000, 12:57 AM
I appreciate your help, but I've tried both of those options and neither appear to work.

In telnet, I get the following error for MV * TH*
....>mv: when moving multiple files, last argument must be a directory

In DOS, it sort of works.[nbsp][nbsp]But REN *.* TH*.* only clips off the first two letters of the files, the CC, and adds TH.[nbsp][nbsp]So whereas I began with a thumbnail named cc128.jpg, after that command I just get th128.jpg.[nbsp][nbsp]And whenever I try to do something like REN *.* THCC*.* I get an error that the files are in use, or that it is a duplicate file name.[nbsp][nbsp]

Needless to say...arg!

------------------
Shalazar

www.charisma-carpenter.com (http://www.charisma-carpenter.com)
The Internet's Premier site for everything Charisma.

sheila
01-06-2000, 01:11 AM
Oh, please post it, not just e-mail. Some of us would try to learn a little shell programming through eavesdropping, that way.

urban
01-06-2000, 10:59 PM
Well, I can give a little explanation, but I'm no expert at shell scripts...

The syntax of the for command is:

[nbsp]for name in words...; do commands; done

The for command will then execute the do commands once for each item in the results list, with name bound to the current member.

Here's a simple example that copies all *.txt files to *.txt~ :


for i in *.tex; do cp $i $i~; done


So, in the case of:

[nbsp][nbsp] for f in *.jpg; do
[nbsp][nbsp][nbsp][nbsp] mv $f th${f%}
[nbsp][nbsp] done

First, we're asking for all files that match *.jpg.[nbsp][nbsp]The point is, each time we go through the loop, $f holds the next file that matches the in criteria.

[nbsp][nbsp] for f in *.*; do mv $f th${f%}; done

The second line, the one that does the work is pretty simple as well:

[nbsp][nbsp][nbsp][nbsp] mv $f th${f%}

Of course we're just calling the move command (mv) and passing the current file in the loop, f.[nbsp][nbsp]The ${f%} is a parameter substitution of sorts...

I'm sure that's not much help.[nbsp][nbsp]There are several shell scripting tutorials available - if you have the time, motiviation, and need to learn it.[nbsp][nbsp]One the most comprehensive bash resources is (there is a section on shell parameter expansion that explains the different uses of the curly brackets {} ):

http://cnswww.cns.cwru.edu/~chet/bash/bashref.html

There are many more, but this is the one I reference the most.

Good luck and happy scripting.

sheila
01-07-2000, 01:50 AM
Thanks for the link. I've saved it.