Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Renaming files in dir

by larus (Acolyte)
on Mar 19, 2009 at 08:13 UTC ( #751642=perlquestion: print w/replies, xml ) Need Help??
larus has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm very new to perl. I'm trying to substitute some characters in file names in some directory. Here is the code
#!/bin/perl #usage: #C:\Perl>perl "C:/Perl/" use warnings; use strict; use File::Copy; my $path = $ARGV[0]; die "You must supply a full directory path" unless (-e $path && -d $pa +th); my @dirs; opendir(DIR, $path) || die "can't opendir $path: $!"; my @fileet = grep { -f "$path/$_" } readdir(DIR); closedir(DIR); foreach my $file (@fileet) { $file =~ s/ä//g; $file =~ s/ö//g; move($file, "$path/$file"); }
This renames and copies the file but only the name of the file, not what it contains. Would you provide some help here? I'm using Windows XP.

Replies are listed 'Best First'.
Re: Renaming files in dir
by bart (Canon) on Mar 19, 2009 at 08:33 UTC
    If you're not moving the file between disks, there's no need to use a module. The built in rename will work just fine.

    And I suspect your problems could be related that you don't give a full path for both arguments of move, but to just one. Maybe, instead of prepending "$path/" in front of the file name everywhere, you could chdir to $path first, and then you can use relative paths, i.e. just use the bare file basename.

    A possible second reason for trouble is that the characters you're trying to change are not in Ascii. Under the hood, (western) Windows uses 2 character sets: CP1252, Microsoft's extension to Latin-1, for the GUI, and an old DOS compatible code page (4 hundred something?) for the console — and maybe for the filesystem, too. Just try to run a script like

    print "\n";
    in the console, and you'll see what I mean. If that is indeed a problem, you'll have to find out the character code of the characters you're trying to change.
      Thanks, scandinavian characters are not the problem, the code I posted produces right substitutions. The problem is that I don't know how to save (replace) those files in the same directory with the new (chars substituted) names.
        The problem is that I don't know how to save (replace) those files in the same directory with the new (chars substituted) names.
        Like I said: use the full path for both arguments (old and new name) for move/rename, or chdir to that directory first, and use just the bare filename.
Re: Renaming files in dir
by cdarke (Prior) on Mar 19, 2009 at 09:06 UTC
    From the File::Copy documentation:
    "The move function also takes two parameters: the current name and the intended name of the file to be moved."

    Seems to me that you are creating a new filename (in $file) but not retaining the old file name for the first parameter to 'move'.

    You should test the return value for an error, for example:
    move ($oldname, $newname) or die "Unable to rename '$oldname' to '$newname': $!";
      Well, I tried this
      foreach my $file (@fileet) { my $newfile = 0; $newfile =~ s/ä//g; $newfile =~ s/ö//g; move("$file", "$path/$newfile"); }
      but this produces no output. What I'm doing wrong here?
        foreach my $file (@fileet) { my $newfile = $file; $newfile =~ s/ä//g; $newfile =~ s/ö//g; move("$file", "$path/$newfile"); }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://751642]
Approved by moritz
erix hammers a jdporter nail randomly
[erix]: average between 1 and 20

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2018-06-21 16:28 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.