Hi again,
You help me a lot, I hope help anyone when I can...
So, my problem;
In perl again; I would like to verify if a file exists, if it's the case I wanna change his name for exemple if 78oov9.def exists copy and change his name in 78oov9.old... I've made a copy function and after a control iteration:
Anyone has an idea why it doesn't work?Code:# $file is the path and the name of my file with the extention added # $oldfile is the same, just with an extention .old # function for copying sub copyf { my ($from, $to, $buf) = @_; if (open (IN, $from)) { if (open(OUT, ">$to")) { while (read IN, $buf, 4096) { print OUT $buf; } close IN; close OUT; return 1; } close IN; } return 0; } # and the control: if (-e $file) { copyf("$file", "$oldfile") or die ("cannor copy\b"); }
Thanks from advance!
Last edited by dimitry; 06-05-2009 at 01:44 AM.
So, I've change of idea; I'll not use copy function, just unlink and rename, it's more easy. My code work correctly... just in case; here you are my code:
Bye!Code:# if $file already exists if (-e $file) { #if $oldfile already exists if (-e $oldfile) { # "delete" it unlink '$oldfile'; # and rename $file in $oldfile rename $file, $oldfile; } else { rename $file, $oldfile; } }
Lord Darkdriver by Dimitry
The code you posted will not work. You have improperly quoted the scalar argument to the unlink function:
unlink '$oldfile';
should be:
unlink $oldfile;
You should also be checking the succes or failure of all those system operations using die or warn or something to make sure they are doing what you want instead of blindly running code that might fail and seem to not work as you expect.
hello,
it's funny, it works perfectly... but I'll try on monday what you said about the quote and the warn.
I gonna give you news. Thanks for your advice.![]()
Lord Darkdriver by Dimitry
probably the rename() function is making it look like unlink worked. Try this:
unlink '$oldfile' or die "Can't delete $oldfile: $!";
Hi,
that's work too. I was looking for error in the logs but there is any about this java script...
Thanks again for your help!
Lord Darkdriver by Dimitry
Yea, whatever you say Dimitry.....![]()
So, I just said that it wasn't any error on the "log file" ....
What exactly you don't understand?
Lord Darkdriver by Dimitry
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks