LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2008, 04:46 AM   #1
frankkky
Member
 
Registered: Sep 2006
Posts: 52

Rep: Reputation: 15
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'
 
Old 09-17-2008, 05:02 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.

Last edited by pixellany; 09-17-2008 at 08:11 AM.
 
Old 09-17-2008, 07:01 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.

Last edited by Telemachos; 09-17-2008 at 07:03 AM.
 
Old 09-17-2008, 10:12 AM   #4
frankkky
Member
 
Registered: Sep 2006
Posts: 52

Original Poster
Rep: Reputation: 15
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.
 
Old 09-17-2008, 12:14 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by frankkky View Post
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.
 
Old 09-18-2008, 01:30 AM   #6
frankkky
Member
 
Registered: Sep 2006
Posts: 52

Original Poster
Rep: Reputation: 15
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?
 
Old 09-18-2008, 01:51 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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

Last edited by ghostdog74; 09-18-2008 at 02:04 AM.
 
Old 09-18-2008, 01:52 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 09-20-2008, 10:19 AM   #9
frankkky
Member
 
Registered: Sep 2006
Posts: 52

Original Poster
Rep: Reputation: 15
thanks for all the above, 'will go over it then
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple shell script help needed vbsaltydog Linux - Networking 10 05-02-2006 01:47 AM
renaming file to include datestamp - script PirateJack Linux - Newbie 3 03-20-2006 07:30 AM
simple script needed drum2jc Linux - Software 1 01-05-2006 12:28 AM
Simple Renaming Script Boffy Programming 5 08-12-2005 09:07 AM
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 02:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration