LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple script needed for file renaming (https://www.linuxquestions.org/questions/programming-9/simple-script-needed-for-file-renaming-670462/)

frankkky 09-17-2008 04:46 AM

simple script needed for file renaming
 
ihave a list of files that i need to run script on. i need a simple script (preferably .bat or any other) to help me accomplish two things:
1, run a script over the folder that is containing these files to re-name these files
2, as some are audio files, i need a script to also re-name these file's 'Title'

pixellany 09-17-2008 05:02 AM

Quote:

.bat
Wash your mouth out with soap!!!---that is a DOS term (file extension meaning batch file)

You don't really describe two separate things---please tell us the criteria to be used for renaming all the files.....or give some before/after examples.

The general form would be:
Code:

for file in `ls <dirname>` do
.
<Insert code to do the necessary tests>
.
mv $file <newname>
done

Depending on your criteria, there may be short cuts.

Telemachos 09-17-2008 07:01 AM

If you are using Debian or a Debian-based distro, you have a program already installed name rename that can do wonders. Check man rename. (There's a program with the same name on other distros, but it doesn't seem nearly as powerful.) There are also lots and lots of programs, both gui and command-line, that can do renaming. Two I can think of quickly are GPRename (a gui, Perl-based renamer) and mmv (a command line renamer).

In my experience, renaming is one of those things that feels like it should be very easy, but there are lots of subtle gotchas. (For example, do your song titles have lots of spaces in their names?) So, even for a "simple" job, it's often better to get a utility that's robust and fully tested.

Beyond that, it's hard to give concrete advice without more details about what the files look like now and what you want them to look like. For something like this, some sample data (both before and after) would help a lot.

frankkky 09-17-2008 10:12 AM

specifically (in this path 'C:\Documents and Settings\johny\My Documents\My Music') i have audio files in this order:

1, 112file01.mp3
2, 112file02.mp3
3, 112file03.mp3
4, 112file04.mp3
5, 112file05.mp3
etc,

and wants to convert them to:

1, 00112_file 001.mp3
2, 00112_file 002.mp3
3, 00112_file 003.mp3
4, 00112_file 004.mp3
5, 00112_file 005.mp3
etc

reason is that i want them to remain sequentialy organized when moved into iTunes - for iPod. otherwise it mix up in the entire iTune.

Any sample script to do this please? in the real sense i intend to do this in xp machine, so if DOS solution is available it will be much appreciated. if not, i also have linux (CentOS) box.

ghostdog74 09-17-2008 12:14 PM

Quote:

Originally Posted by frankkky (Post 3283216)
Any sample script to do this please? in the real sense i intend to do this in xp machine, so if DOS solution is available it will be much appreciated. if not, i also have linux (CentOS) box.

for a ready script, you can try the Python script here on your Linux box.
example usage:
Code:

# ./script.py -D "/path/to/files"  -p "file" -e "_file" -l "*.mp3" # -l options lists files only
To be renamed or deleted):  [ /path/to/files/112file04.mp3 ]  to  [ /path/to/files/112_file04.mp3 ]
To be renamed or deleted):  [ /path/to/files/112file03.mp3 ]  to  [ /path/to/files/112_file03.mp3 ]
To be renamed or deleted):  [ /path/to/files/112file01.mp3 ]  to  [ /path/to/files/112_file01.mp3 ]
To be renamed or deleted):  [ /path/to/files/112file05.mp3 ]  to  [ /path/to/files/112_file05.mp3 ]
To be renamed or deleted):  [ /path/to/files/112file02.mp3 ]  to  [ /path/to/files/112_file02.mp3 ]
# ./script.py -D "/path/to/files"  -p "file" -e "_file"  "*.mp3" #remove -l  to actual rename

# ./script.py -D "/path/to/files"  -i "00" -l "*.mp3"
To be renamed or deleted):  [ /path/to/files/112_file04.mp3 ]  to  [ /path/to/files/00112_file04.mp3 ]
To be renamed or deleted):  [ /path/to/files/112_file05.mp3 ]  to  [ /path/to/files/00112_file05.mp3 ]
To be renamed or deleted):  [ /path/to/files/112_file01.mp3 ]  to  [ /path/to/files/00112_file01.mp3 ]
To be renamed or deleted):  [ /path/to/files/112_file02.mp3 ]  to  [ /path/to/files/00112_file02.mp3 ]
To be renamed or deleted):  [ /path/to/files/112_file03.mp3 ]  to  [ /path/to/files/00112_file03.mp3 ]
# ./script.py -D "/path/to/files"  -i "00" "*.mp3"

# ./script.py -D "/path/to/files"  -s "[0-9][0-9][.]mp3" -e " 001:100.mp3" -l  "*.mp3"

To be renamed or deleted):  [ /path/to/files/00112_file01.mp3 ]  to  [ /path/to/files/00112_file 001.mp3 ]
To be renamed or deleted):  [ /path/to/files/00112_file02.mp3 ]  to  [ /path/to/files/00112_file 002.mp3 ]
To be renamed or deleted):  [ /path/to/files/00112_file05.mp3 ]  to  [ /path/to/files/00112_file 003.mp3 ]
To be renamed or deleted):  [ /path/to/files/00112_file04.mp3 ]  to  [ /path/to/files/00112_file 004.mp3 ]
To be renamed or deleted):  [ /path/to/files/00112_file03.mp3 ]  to  [ /path/to/files/00112_file 005.mp3 ]

# ./script.py -D "/path/to/files"  -s "[0-9][0-9][.]mp3" -e " 001:100.mp3" "*.mp3" #actual rename

if you do not have Python, you can use the shell versions

Otherwise, you can just write a simple script using shell tools such as sed, rename, or even bash.

frankkky 09-18-2008 01:30 AM

thanks so much GhostDog, but i have two more questions to ask:

1,
As these .mp3 files inside each folder are just too many (i listed just 5 above though - but its just many), in all i have over 100 folders. Is there a way to write this script without specifically describing each .mp3 file name. in other words to write the script in a way that it can loop from one .mp3 file to another inside a particular folder?

2,
In addition to re-naming the files name, i also want to re-name these files 'Titles', so how will the script to do this look like?

ghostdog74 09-18-2008 01:51 AM

Here's a pseudocode for the shell
Code:


find /path -type f -name "112*mp3" -printf "%f %h %p\n"| while read FILES FILEPATH FULL
do
    echo $FILES $FILEPATH $FULL
    echo $FILES |sed 's/^11/0011/'
    FILES=`echo $FILES |sed 's/^11/0011/'`   
    echo $FILES |sed 's/file/_file/'
    # continue with sequence....
done


chrism01 09-18-2008 01:52 AM

His script does that if you read carefully. He says that if you remove the -l flag, it'll actually do the rename, instead of just showing you what it would have done.

frankkky 09-20-2008 10:19 AM

thanks for all the above, 'will go over it then


All times are GMT -5. The time now is 04:02 AM.