Home > Archive > PERL Miscellaneous > May 2005 > simplest method for renaming files / Windows ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
simplest method for renaming files / Windows ?
|
|
| Geoff Cox 2005-05-25, 3:58 am |
| Hello
At the moment I
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
I know there are better ways - but am as to which to use.
Which is the simplest?
Thanks
Geoff
| |
| John Bokma 2005-05-25, 3:58 am |
| Geoff Cox wrote:
> Hello
>
> At the moment I
>
> 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
>
> I know there are better ways - but am as to which to use.
> Which is the simplest?
rename, *but* be careful not to rename new files over old files etc.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
| |
| Geoff Cox 2005-05-25, 3:58 am |
| On 25 May 2005 06:00:23 GMT, John Bokma <john@castleamber.com> wrote:
>rename, *but* be careful not to rename new files over old files etc.
John,
I thought rename did not apply to Perl with Windows? Only for unix? am
I wrong?
Assuming it does work do I have to?
1. rename orig files to new names
2. delete orig files
3. rename new files to orig file names
Cannot just rename files in one step?
Geoff
| |
| Geoff Cox 2005-05-25, 8:56 am |
| On 25 May 2005 06:00:23 GMT, John Bokma <john@castleamber.com> wrote:
John,
I have just tried
rename ("d://perl/progs/test/file1.txt","file2.txt") ;
and
this appears to delete the orig file and create new file with the new
name ...
Cheers
Geoff
| |
| Joe Smith 2005-05-25, 8:56 am |
| 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 .
[color=darkred]
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
| |
| A. Sinan Unur 2005-05-25, 3:56 pm |
| Geoff Cox <geoff.cox@notquitecorrectfreeuk.com> wrote in
news:vi6891tknivdpmqn60tg8bbtgnjpk4jmim@
4ax.com:
> On 25 May 2005 06:00:23 GMT, John Bokma <john@castleamber.com> wrote:
What did John write? You have been around long enough to know to quote
some context when replying.
> I have just tried
>
> rename ("d://perl/progs/test/file1.txt","file2.txt") ;
>
> and
>
> this appears to delete the orig file and create new file with the new
> name ...
perldoc -f rename
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Jürgen Exner 2005-05-25, 3:56 pm |
| Geoff Cox wrote:
> At the moment I
> 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
If you don't mind asking me, but what is the purpose of this excercise, i.e.
what are you trying to achive with steps 1-4 above?
For all practical purposes you got an identical set of files, just some file
attributes (created, last modified, maybe owner, group, access right) would
be different and there are better ways to change those than to copy each and
every file and rename it back to the original name.
> I know there are better ways - but am as to which to use.
> Which is the simplest?
Depends on what you actually want to achive. Your steps 1-4 are almost a
NOP.
jue
| |
| Geoff Cox 2005-05-25, 3:56 pm |
| On Wed, 25 May 2005 12:17:56 GMT, "Jürgen Exner"
<jurgenex@hotmail.com> wrote:
>Geoff Cox wrote:
>
>If you don't mind asking me, but what is the purpose of this excercise, i.e.
>what are you trying to achive with steps 1-4 above?
!! I did not make it very clear - I have a set of files which are them
acted on by a program and changed so I have been giving them a
different name so as not to overwrite the originals ( I have been
opening the files, making changes and then writing them back with an
OUT command. For a separate reason I need the files to have the
original name fro the next step so have been changing them back to the
original name !!
anyway all is better now as I have realised that
rename ("d://perl/progs/test/file1.txt","file2.txt") ;
does work with Windows so am now using this approach.
Thanks
Cheers
Geoff
>For all practical purposes you got an identical set of files, just some file
>attributes (created, last modified, maybe owner, group, access right) would
>be different and there are better ways to change those than to copy each and
>every file and rename it back to the original name.
>
>
>Depends on what you actually want to achive. Your steps 1-4 are almost a
>NOP.
>
>jue
>
|
|
|
|
|