Computing.Net > Forums > Unix > script to change file names

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

script to change file names

Reply to Message Icon

Name: dkrishnachaitanya
Date: March 4, 2009 at 10:08:01 Pacific
OS: Windows Vista
Subcategory: General
Comment:

I have files in my directory as 'PS_1_20080320.txt'. I need a script which converts them to 'PS-AG-<5 Digit Sequence Number starting from 10001>-MMDDYYYY.dat'.
Could you help?



Sponsored Link
Ads by Google

Response Number 1
Name: dkrishnachaitanya
Date: March 4, 2009 at 11:18:45 Pacific
Reply:

could somebody help?


0

Response Number 2
Name: nails
Date: March 4, 2009 at 18:53:27 Pacific
Reply:

I do not understand your requirement. So, if file 'PS_1_20080320.txt'. is the first one in the list, what would the file name be?



0

Response Number 3
Name: dkrishnachaitanya
Date: March 4, 2009 at 22:57:49 Pacific
Reply:

There are around 250 files in a directory, one generated for each day starting from jan 20, 2008 Hence lets say the first file is 'PS_1_20080120.txt', second one is 'PS_1_20080121.txt', third one is 'PS_1_20080122.txt' etc

I need a script which runs on this directory and converts all these file names as 'PS-AG-10001-01202008.dat', 'PS-AG-10002-01212008.dat', 'PS-AG-10003-01222008.dat' etc

Could you please help?


0

Response Number 4
Name: nails
Date: March 5, 2009 at 09:04:20 Pacific
Reply:

Obviously, if you change the file structure you stated, this breaks. If this is a script you'll run often, then you probably want to change to the directory where the files reside. When it's the way you want it, change cp to mv.

#!/bin/ksh

# cd <to my file directory>
num=10000
ls -1t PS*.txt|while read file
do
   bf=$(basename $file .txt)
   # get the 3 character sequence number
   len=${#bf}
   seq3=$(echo "$bf"|cut -c$((len-3))-$len)
   # get the year
   yr=$(echo "$bf"|cut -c$((len-7))-$((len-4)) )
   # build the destination filename and move
   ((num+=1))
   cp "$file" "PS-AG-${num}-${seq3}${yr}.dat"
done


0

Response Number 5
Name: dkrishnachaitanya
Date: March 5, 2009 at 09:30:26 Pacific
Reply:

Thanks a lot nails for your time and response, appreciate it.

I am going to test run this in test environment tomorrow, will let you know if I have any questions.

Thank you.


0

Related Posts

See More



Response Number 6
Name: mr.jessie
Date: March 12, 2009 at 21:03:31 Pacific
Reply:

Hi to Samer,

I have the same need but the scenario is i'm going to rename a file based on the subdirectory's name. I am using asterisk with voicemail application. The asterisk automatically create a subdirectory based on voicemail account, i.e. if you have and account 80100 and have somebody leave a voicemail message, the asterisk will automatically create a folder "80100". Inside the new folder 80100, a auto-generated sub folder of "INBOX", resides a voicemail messages in msg####.wav format. This msg####.wav is what I need to rename into 80100-msg0000-ddmmyyhhmmss.wav. The msg#### is sequencial, meaning every new message the file name change creates into msg0000.wav, msg0001.wav, msg0002.wav and so on.

Since im newbie in scripting, i really need a help and guide how to do this. Big thanks and help on looking at this.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: script to change file names

script for changing file names www.computing.net/answers/unix/script-for-changing-file-names/6732.html

script to change case of extensions www.computing.net/answers/unix/script-to-change-case-of-extensions/5017.html

script to rename file names www.computing.net/answers/unix/script-to-rename-file-names/7621.html