Change filename - increment digit by 60


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change filename - increment digit by 60
# 1  
Old 07-31-2012
Change filename - increment digit by 60

I have a bunch of files, they have spaces! I am working in windows 7, but I am hoping I could install and use cygwin or some other unix platform to rename my files (suggestions welcome).

The files are named:

Wind Attn Vol - 474.wrl
Wind Attn Vol - 475.wrl
Wind Attn Vol - 476.wrl etc..

I want the digits to be incremented by 60


Wind Attn Vol - 534.wrl
Wind Attn Vol - 535.wrl
Wind Attn Vol - 536.wrl etc..

All suggestions are greatly appreciated.
# 2  
Old 07-31-2012
Hi,
Yes you can do with cygwin but dont forget to install necessary binaries like find,awk,perl, ssh while installing..
Try this untested code,
Code:
find /home/dir -name "*.wrl" |awk -F"[ .]" '{gsub(/ /,"\ ");ori=$0;$5=$5 60;print "mv ",ori,$0;}' |sh

First of all remove the last 'sh' command and see how the mv command printed in the screen. If its fine then execute with this whole command.
I assuming your directory names doesnt have any space and dot(.).
Cheers,
Ranga:-)

Last edited by rangarasan; 08-01-2012 at 01:36 AM.. Reason: typo
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 07-31-2012
Quote:
Originally Posted by rangarasan
Try this untested code,
Code:
find /home/dir -name "*.wrl" |awk -F[ .] '{gsub(/ /,"\ ");ori=$0;$5=$5 60;print "mv ",ori,$0;}' |sh

It has no chance of working, unless you quote the argument to -F.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 08-01-2012
Yes thanks for pointing out. I have updated in my previous post.
Cheers,
Ranga:-)
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 08-01-2012
If I understood the request correctly, I think the person submitting this request wants something more like:
Code:
ls *.wrl | awk '/[0-9]+/ {
        pos=match($0,"[0-9]+")
        val=substr($0,pos,RLENGTH)
        printf("mv \"%s\" \"%s%d%s\"\n",
                $0,substr($0,1,pos-1),val+60,substr($0,pos+RLENGTH))
        }'

which should show the commands to be executed. If the resulting commands are what is wanted, then pipe the output of the above through sh (or your favorite shell).
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-01-2012
As it is in windows 7, you can use vbscript to rename the files

1) change the folder name
2) save the script as rename.vbs
3) open the command prompt and goto the script folder
4) execute the script by cscript.exe rename.vbs
5) Check the output ( it will just display the old & new file name )
6) If you are ok with the output, then remove the ' from 'objFSO.MoveFile and run it once again.

Code:
 
Dim WshShell, oExec 
Set WshShell = WScript.CreateObject("WScript.Shell") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\\TEST"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    startpos=instr(objFile.Name,"-")
 endpos=instr(objFile.Name,".wrl")
 Number=(Mid(objFile.Name,startpos+2,(endpos-(startpos+2))))
 Number=Number+60
 Fpart=Mid(objFile.Name,1,startpos)
 Spart=Mid(objFile.Name,(Len(objFile.Name)-3),4)
 newFileName=Fpart & " " & Number & Spart
 wscript.echo "OLD FILE : " & objFile.Name & "    NEW FILE : " & newFileName
 FileName = objStartFolder & "\\" & objFile.Name 
 newFileName = objStartFolder & "\\" & newFileName
 'objFSO.moveFile FileName, newFileName
Next

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 08-01-2012
Thanks all for your elite comments. But unfortunately, I was just stuck with Windows 7 trying to get cygwin to work since cygwin has installation issues on Windows 7. I tried all day yesterday. Then I tried using LinuxConsole which creates a linux partition on windows, which I installed a long time ago, but for some reason it has issues with grub. But, I still did not give up, and am working to find a way to work on the linux/unix platform (any ideas?).

In the meanwhile, itkamaraj suggested a solution in Windows 7 platform. Thanks itkamaraj. Your code did work fantastically, but it spits out an error at the end - "Microsoft VBScript runtime error: Invalid procedure call or argument: 'Mid'". I just ignored this error since I got what I wanted for most part. But, when I had a slightly different file format, the code just fails with the error "rename.vbs(10, 2) Microsoft VBScript runtime error: Invalid procedure call or argument: 'Mid'"

The newfile format that I have is:

Wind Attn Coeff Tunnel - 474.wrl
Wind Attn Coeff Tunnel - 475.wrl
Wind Attn Coeff Tunnel - 476.wrl, etc.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to change date in a filename?

Hi i want to list files based on date and change the date alone in the files in a directory abc20120101.txt xyzxyxz20120101.txt ccc20120201.txt ddd20120301.txt In the above i want to select only files having date 20120101 and rename the date for those files like below abc20111231.txt... (3 Replies)
Discussion started by: Dewdrop
3 Replies

2. Shell Programming and Scripting

convert two digit in to single digit...

Hi Guys. My Input: ABCD 12 00 KL ABCD 12 08 DL ABCD 12 10 KK ABCD 12 04 LL ABCD 13 00 LP ABCD 13 1O LS Output: ABCD 12 0 KL ABCD 12 8 DL ABCD 12 10 KK ABCD 12 4 LL ABCD 13 0 LP (2 Replies)
Discussion started by: pareshkp
2 Replies

3. Shell Programming and Scripting

increment counter as suffix starting with the rightmost digit

Hi, I would like to add a suffix to a file name but maintain the suffix length to 5 digits. For eg, output > 1st_file.00001, 2nd_file.00002...10th_file.00010....100th_file.00100 Can anyone please advise me on how to go about it? Platform: SunOS mps201a 5.9 Generic_118558-39 sun4u... (7 Replies)
Discussion started by: danish0909
7 Replies

4. Shell Programming and Scripting

Change the filename variable value

Hi guys, I have a variable where i am storing the filename (with full path). I just need the value before ".txt". But instead of getting the filename i am getting the contents of the filename. FileName=/appl/data/Input/US/Test.txt a=`awk -F"." '{print $1}' ${FileName}` echo $a... (3 Replies)
Discussion started by: mac4rfree
3 Replies

5. Shell Programming and Scripting

awk length of digit and print at most right digit

Have columns with digits and strings like: input.txt 3840 3841 3842 Dav Thun Tax Cahn 146; Dav. 3855 3853 3861 3862 Dav Thun Tax 2780 Karl VI., 3873 3872 3872 Dav Thun Tax 3894 3893 3897 3899 Dav Thun Tax 403; Thun 282. 3958 3959 3960 Dav Thun Tax 3972 3972 3972 3975 Dav Thun Tax... (8 Replies)
Discussion started by: sdf
8 Replies

6. Shell Programming and Scripting

Change the filename

I have 100 files in a directory with a.1 a.2 a.3 a.4 How do i remove a. and i need the file names as 1 2 3 4 please help (2 Replies)
Discussion started by: srichunduru
2 Replies

7. UNIX for Dummies Questions & Answers

bash script to increment a digit in filename

Hi guys, Can someone help me out with this: I have a directory with files like the following, GHost++ 2010-03-14 04-01 DotaCash RD us_ca LC #7 (44m19s).w3g GHost++ 2010-03-14 04-06 DotaCash AP us_ca LC #8 (42m24s).w3g GHost++ 2010-03-14 04-07 DotaCash AR us_ca LC #10 (08m23s).w3g ... (4 Replies)
Discussion started by: hbjlee17
4 Replies

8. Shell Programming and Scripting

gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part... (6 Replies)
Discussion started by: timj123
6 Replies

9. Shell Programming and Scripting

Replace one digit by two digit using sed

Folks, Is there a simple way to replace one digit by two digit using sed. Example, mydigit1918_2006_8_8_lag1.csv should be mydigit1918_2006_08_08_lag01.csv. I tried this way, but doesn't work. echo mydigit1989_2006_8_8_lag1.csv|sed 's/]/0]/' Thank you, (5 Replies)
Discussion started by: Jae
5 Replies

10. Shell Programming and Scripting

Change new filename with date ??

Hi all, I am newbie and hope that you can help me to rename a file If I have a file name Perform.01222006.12345.Log now I would like to backup another file with another name like perform-20060112.dat This is a flat file, and I want to collect some field, then put it in a new file from... (9 Replies)
Discussion started by: sabercats
9 Replies
Login or Register to Ask a Question