simplest method for renaming files / Windows ?

G

Geoff Cox

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 confused as to which to use.
Which is the simplest?

Thanks

Geoff
 
J

John Bokma

Geoff said:
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 confused as to which to use.
Which is the simplest?

rename, *but* be careful not to rename new files over old files etc.
 
G

Geoff Cox

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
 
G

Geoff Cox

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
 
J

Joe Smith

Geoff said:
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.

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

A. Sinan Unur

On 25 May 2005 06:00:23 GMT, John Bokma <[email protected]> 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
 
J

Jürgen Exner

Geoff said:
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 confused 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
 
G

Geoff Cox

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top