Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change file names 1

Status
Not open for further replies.

rkdash

Technical User
Jul 15, 2004
14
US
Hi all,
I have hundreds of files with names like this:

101-fd34.pics
102-fd35.pics
103-fd36.pics
104-fd37.pics

and I want to rename them as:

fd34.picks
fd35.picks
fd36.picks
fd37.picks


That means, I want to get rid of the characters before "fd"
and add a k in "pics" to make it "picks".

Thanks for any help.
 
Sorry I realize this is the AWK forum, but It just seesm to me this would be bteer done as a shell script.

#! /bin/csh

foreach i ( *.pics )
set a = `echo $i | cut -d'-' -f2-`
set b = $a:r
mv $i ${b}.picks
end

if you have too many files for the Foreach run the script multiple times specifying different substrings of file names

foreach j ( 1 2 3 4 5 6 7 8 9 )
foreach i ( ${j}*.pics )


 
Code:
BEGIN {
    movecmd = "mv"  #modify for your system
    j = 34
    for (i=101; i<=104; i++) {
        oldname = sprintf("%03d", i) "-fd" sprintf("%02d", j) ".pic"
        newname = oldname
        sub(/^[0-9][0-9][0-9]-/, "", newname)
        sub(/pic$/, "picks", newname)
        syscmd = movecmd " " oldname " " newname
        print syscmd
        result = system(syscmd)
        if (result) {
            print "Couldn\'t execute \"" syscmd "\"!"
        }
       j++
    }
}
Output
mv 101-fd34.pic fd34.picks
Couldn't execute "mv 101-fd34.pic fd34.picks"!
mv 102-fd35.pic fd35.picks
Couldn't execute "mv 102-fd35.pic fd35.picks"!
mv 103-fd36.pic fd36.picks
Couldn't execute "mv 103-fd36.pic fd36.picks"!
mv 104-fd37.pic fd37.picks
Couldn't execute "mv 104-fd37.pic fd37.picks"!


Of course, I don't have these files, so the rename command doesn't work, but as you can see, it's forming the command correctly. If you have these files, it should work.



 
Create script "fixnames.awk":
Code:
BEGIN { rename = "mv "
  for (i=1; i<ARGC; i++)
  { $0 = ARGV[i]
    sub( /^.*-/, "" ); sub( /pics$/, "picks" )
    system( rename ARGV[i] " " $0 )
  }
}
Run it with:
awk -f fixnames.awk *.pics

 
Thanks mikevh and tdatgod,

it works !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top