Re: simplest method for renaming files / Windows ?



Geoff Cox wrote:

I thought rename did not apply to Perl with Windows? am I wrong?

The rename() function works just fine with Windows. Whatever gave you the impression that it doesn't? (If you are referring to perl's '-i' command line option with Windows, that is a different subject.)

Assuming it does work do I have to?

1. rename orig files to new names
2. delete orig files

But if you've done a rename, the original file is no longer there, therefore there is nothing for step 2 to delete.

3. rename new files to orig file names

What? You mean put everything back the same as it was? Why do that if you are not going to change anything?

Cannot just rename files in one step?

Sounds like you are very confused.

1. use Find::File
2. open each file and print OUT each line to  files with a new name
3. delete the original files
4. rename the new files to the original ones with another Perl script

That makes more sense. Just do step 4 in your original script.

	==================================================

Here is how "perl -pi -e 's/foo/bar/g' *.txt" works with Unix:
  1) Open input file (whatever.txt) for reading.
  2) Delete the input file.  (Can't do this with Windows.)
  3) Open output file (whatever.txt).
  4) Read lines from the input file one at a time.
  5) Change all instances of 'foo' to 'bar'.
  6) Write the modified line to the output file.
  7) Repeat previous three steps until EOF.
  8) Close the input file.  If no processes still have it open
     for reading, this is when the file is actually deleted.
  9) Close the output file.
 10) Repeat for all files specified on the command line.

Here is how 'perl -pi.bak -e "s/foo/bar/g" *.txt' works (Windows + Unix)
  1) Rename filename to filename.bak (whatever.txt to whatever.txt.bak).
  2) Open the renamed file for reading.
  3-7) = same as above
  8) Close the input file.  The original file is still there, albeit
     with a different name.
  9-10) same as above.

Here's how to do those steps using File::Find

  sub wanted {
    my $fil = $_;	# Name of input file, relative to current dir
    my $tmp = "$fil.tmp";
    rename $fil,$tmp or die "Cannot rename $fil to $tmp - $!\n";
    open my $out,'>',$fil or die "Cannot write to $fil - $!\n";
    open my $in, '<',$tmp or die "Cannot read from $tmp - $!\n";
    while (defined my $line = <>) {
      $line =~ s/foo/bar/g;
      print $out $line or warn "Problem writing to $fil - $!\n";
    }
    close $out or warn "Problems closing $fil - $!\n";
    close $in;
    unlink $in or warn "Unable to remove $tmp - $!\n";
  }


-Joe .



Relevant Pages

  • Re: Great SWT Program
    ... a good tutorial on Unix CLI tools? ... command there, ... then rename everything in the search scope with the CLI, ... I'd have to boot up a Windows system and try stuff, ...
    (comp.lang.java.programmer)
  • Re: The rename =?windows-1252?Q?command=85?=
    ... From the very beginning of Linux the Windows users had to ... learn that rename is not part of the commands. ... When first starting to use computers, if we do not count the very first ... I accidently found that rename command, ...
    (Ubuntu)
  • Re: OT computer question
    ... but not with just a single command. ... I am thinking this can't be done with what comes windows. ... This is all done at the DOS PROMPT, from inside a "Command Prompt" window. ... make a file with the names of all the files you want to rename. ...
    (rec.outdoors.rv-travel)
  • Re: The rename =?ISO-8859-1?Q?command=2E=2E=2E?=
    ... I'm sorry to waste your summer. ... years I started to use Windows, just because wherever I was there was ... I used mv since about 1988 and I am not sure I ever used rename in ... I accidently found that rename command, ...
    (Ubuntu)
  • Re: Multi AV ???
    ... command line scan instead of scans with a Windows UI. ... Rename would be great!!! ... Playing Nice on Usenet: ... Keeping Windows Clean ...
    (microsoft.public.security.virus)