batch renaming files 
Author Message
 batch renaming files

I'm trying to figure out how to batch rename files.
ie if I have files with the letters 'dog' in them I want to replace them
with 'mouse'. so mydogname.txt becomes mymousename.txt
Is that possible with awk or sed? I can't figure this out.

--
Darryl
Australia
   #####\             _             /#####  
   #( )# |          _( )__         | #( )#  
   ##### |         /_    /         | #####  
   #" "# |     ___m/I_ //_____     | #" "#
   # O # |____#-x.\ /++m\ /.x-#____| # O #
   #m.m# |   /" \ ///###\\\ / "\   | #m.m#
   #####/    ######/     \######    \#####
Profession- Disk Jockey/Real time 3D graphics
    http://www.*-*-*.com/ ~daz/home.htm



Sun, 29 Jul 2001 03:00:00 GMT  
 batch renaming files


% I'm trying to figure out how to batch rename files.
% ie if I have files with the letters 'dog' in them I want to replace them
% with 'mouse'. so mydogname.txt becomes mymousename.txt
% Is that possible with awk or sed? I can't figure this out.

With awk:
 BEGIN {
    while (("ls" | getline) > 0) {
       old = $0
       if (sub("dog","mouse"))
          # these single quotes are in case of special characters in the
          # file names
          system("mv '" old "' '" $0 "'")
    }
    close("ls")
 }

That will get only one instance of dog in each file name. Use gsub if
you want to change more than one instance of dog per file name (eg, in
that script, dogeatdog.txt would be changed to mouseeatdog.txt, but
using gsub, you'd get mouseeatmouse.txt).

with ksh:
 for a in *dog*
 do
   mv $a ${a%dog*}mouse${a#*dog}
 done

This will not work if there's more than one dog in a name (eg, dogeatdog.txt
would be changed to dogeatmouseeatdog.txt by this).
--

Patrick TJ McPhee
East York  Canada



Sun, 29 Jul 2001 03:00:00 GMT  
 batch renaming files

Quote:


>% I'm trying to figure out how to batch rename files.
>% ie if I have files with the letters 'dog' in them I want to replace them
>% with 'mouse'. so mydogname.txt becomes mymousename.txt
>% Is that possible with awk or sed? I can't figure this out.

I have been using this for many years now.  It is a csh script using sed.

#
foreach i (`ls *$1*`)
echo $i
mv $i `echo $i | sed s/$1/$2/g`
end

-- glen



Mon, 30 Jul 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Substituting :: for __ in every htm file, and rename htlml file

2. FILE-STATUS and RENAME-FILE in Pygmy Forth

3. need help with rename-file/delete-file

4. Assembly routine to use a crack on any file, as %1 in a batch file

5. How to pass the argv variables from batch file to .tcl file

6. How to pass the argv variables from batch file to .tcl file

7. Renaming a file

8. Win2000 Gawk Renaming a file with system() command, command line to long

9. Files renamed in entire directory

10. Delete-rename a file

11. Renaming files

12. create directories..rename files

 

 
Powered by phpBB® Forum Software