Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Renaming Files

by Anonymous Monk
on Feb 19, 2003 at 19:28 UTC ( [id://236776]=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I need help! I'm trying to renaming "*.cfm" files to "*.html" and it keeps giving me an error message "No such file or directory" At least it is trying to rename the files. Do you have any ideas?
#! perl -w # Copy files to another directory use strict; use File::Copy; use File::Spec::Functions qw(catfile); my $line = 'C:\sql server scripts\test'; opendir MYDIR, $line or die "Could not opendir $line: $!\n"; my @allfiles = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR ; closedir(MYDIR); my @files = grep { !-d } @allfiles ; my @dirs = grep { -d } @allfiles ; print @files." files and ".@dirs." directories in $line\n" ; print map "$_\n", @allfiles; my @select_files = grep /\.cfm\z/i, @files; for my $file (@select_files) { copy catfile($line,$file), catfile("C:\\temp", $file); } my $newdir = 'C:\\temp'; opendir MYDIR, $newdir or die "Could not opendir $newdir: $!\n"; my @all_files = grep { $_ ne '.' and $_ ne '..' } readdir MYDIR ; closedir(MYDIR); my @change_files = grep { !-d } @all_files; foreach my $get_files (@change_files) { my $newfile = $get_files; $newfile =~ s/\.cfm$/.html/; if (-e $newfile) { warn "can't rename $get_files to $newfile: $newfile exists\n"; } elsif (rename $get_files, $newfile) { print "file was renamed to $newfile\n" } else { warn "rename $get_files to $newfile failed: $!\n"; } }

Replies are listed 'Best First'.
Re: Renaming Files
by dws (Chancellor) on Feb 19, 2003 at 19:39 UTC
    ... it keeps giving me an error message "No such file or directory" At least it is trying to rename the files. Do you have any ideas?

    When you open and read a directory, the names that you get don't have leading path components. Unless you're chdir()'d to the directory, you'll need to prepend the path before doing operations like -d, -e, or rename().

    In your case, that means prepending $line or $newdir.

Re: Renaming Files
by Shendal (Hermit) on Feb 19, 2003 at 19:49 UTC
    I strongly recommend File::Find. Here's an example of what it might look like:
    use File::Find; my $dir = shift || '.'; find \&wanted, $dir; sub wanted { next unless (-f && /\.cfm$/); my $newname = $File::Find::name; $newname =~ s/\.cfm$/.html/; rename $File::Find::name,$newname or die "Unable to rename $File::F +ind::name!"; }

    Cheers,
    Shendal
Re: Renaming Files
by Tomte (Priest) on Feb 19, 2003 at 19:54 UTC
    One of your problems seems to be
    'C:\\temp'

    you do not need to escape backslashes in strings enclosed in single-quotes; I may be wrong, though, I'm windows free since 2.5 years now.

    On another note: If the program exits with such a clear error-message, believe what it says and try to debug it the old way: just print out where you are and what the variable-values are.
    You'll see that your understanding of what programming is and what programs you've written actualy do, increases a lot if you take a closer look yourself.

    As a second note:
    The programm seems to be overly complicated for such a simple task, maybe you should google around a bit on this subject; learning by doing is a Good Thing(TM), but The-Right-Tool-For-The-Right-Job(tm) too ;-)

    Now to the solution: Change line ~43

    } elsif (rename "$newdir/$get_files", "$newdir/$newfile") {

    hth,
    regards,
    tomte


      eep! You most certainly do need to escape backslashes, whether in single quotes or not. This is because the backslash is the escape character. If you didn't have to escape backslashes, how would you represent a newline from within a single-quoted string? perl is smart, but not that smart.

      thor

        my perl seems to be...
        #!/usr/bin/perl -w print 'test\n'; print "test\n"; __END__ test\ntest

        Update:

        perldoc perldata

        String literals are usually delimited by either single or double quotes. They work much like quotes in the standard Unix shells: double-quoted string literals are subject to backslash and variable substitution; single-quoted strings are not (except for "\'" and "\\").

        regards,
        tomte


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://236776]
Approved by dws
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-16 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found