Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that gets rid of blank spaces in my filenames but dont want it to overwrite another file that might have the same name. For example if my filename change was from "file name.txt" to "file_name.txt" I need to make sure there is not another "file_name.txt" so I dont overwrite an already existing file with my renaming. So I need to check if "file_name.txt" exists before I rename the "file name.txt".
use strict; my $file = '.'; opendir(MYDIR,$file); my @files = readdir(MYDIR); foreach $_ (@files) { next if($_ =~ m/^\.+$/); if ( m/ /g ) { my $oldfile = $_; s/ /_/g; if (! -e $oldfile) { rename $oldfile, $_; } else { print "Could not rename $_ because $oldfile na +me already exists and can not be overwritten\n"; } } print "$_\n"; }

Replies are listed 'Best First'.
Re: Checking for existing filename before renaming file
by VSarkiss (Monsignor) on Feb 14, 2003 at 15:54 UTC

    Well, you don't say what the problem is, but fortunately, I have the ultra-powerful ESP::MindRead module at hand. You're checking whether $oldfile exists, but what you really want is to check that the target file doesn't exist. That is, the one contained in $_ at this point:

    my $oldfile = $_; s/ /_/g;
    In other words, test if (! -e $_)

    If that's not the problem, could you please state what it is? ;-)

Re: Checking for existing filename before renaming file
by Tomte (Priest) on Feb 14, 2003 at 15:56 UTC

    if (! -e $oldfile)
    should read
    if (! -e $_)
    and
    print "Could not rename $_ because $oldfile name already exists and ca +n not be overwritten\n";
    should read print "Could not rename $oldfile to ${_}, because ${_} $oldfile already exists and can not be overwritten\n";

    this was easy to debug, did you try to find out?
    it helps to look at the variable-values...

    hth

    regards,
    tomte


Re: Checking for existing filename before renaming file
by amrangaye (Friar) on Feb 14, 2003 at 16:23 UTC
    Hi. Seems the above replies have already solved your problem. But just a point of (hopefully) correction here. Do you have to keep explicitly using $_ ? Your code would be much more readable, you know, if you didn't do that. And use \s for your space, instead of a space character.
    use strict; my $file = '.'; opendir(MYDIR,$file); my @files = readdir(MYDIR); foreach (@files) { next if( m/^\.+$/); if ( m/\s/g ) { my $oldfile = $_; s/\s/_/g; if (! -e) { rename $oldfile, $_; } else { print "Could not rename $oldfile because ${_} +name already exists and can not be overwritten\n"; } } print "$_\n"; }
      Thanks
Re: Checking for existing filename before renaming file
by martymart (Deacon) on Feb 14, 2003 at 16:04 UTC
    you could use a file test operator to check that files exist before they are overwritten. Type perldoc perlfunc at a command prompt and look in the section "Alphabetical List of Perl Functions" to see the full list of file test operators.
    print "Save data to what file?"; $filename=<STDIN>; chomp $filename; if (-s $filename){ warn "$file contents will be overwritten!\n"; warn "$filename was last updated ", -M $filename, "days ago.\n"; }
    -M =returns the age (in days)
    -s =returns the size of the data (in bytes)

    Martmart