Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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?

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?

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?

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

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.

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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |