in reply to File renaming and removal to a subdirectory

Hi Joes this snippet will rename all the files in a directory that end in $from so they end in $to for you. As every operating system has some form of copy a -> b function using it will be the quickest way to achieve the second part of your goal.

my $dir = "c:/"; my $from = '.puz'; my $to = 'A.puz'; while (<$dir*$from>) { my ($old,$new); $old = $new = $_; $new =~ s/$from$/$to/; rename $old, $new; }

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: File remaing and removal to a subdirectory
by Joes (Acolyte) on Jul 12, 2001 at 10:53 UTC
    Sorry tachyon, your script to change file names did not work.

    I have enclosed the actual script used FYI

    It went right through and printed the last line, but the files in C:/ Xwords had not changed their name.

    #!/usr/bin/perl -w use strict; my $dir = "c:/Xwords"; my $from = '.puz'; my $to = 'A.puz'; while (<$dir*$from>) { my ($old,$new); $old = $new = $_; $new =~ s/$from$/$to/; rename $old, $new; } print "Files renamed !";

      The problem is that you have forgotten the / after Xwords. It works fine if you add it.

      my $dir = "c:/Xwords/"; my $from = '.puz'; my $to = 'A.puz'; while (<$dir*$from>) { my ($old,$new); $old = $new = $_; $new =~ s/$from$/$to/; rename $old, $new; }

      The problem is that the * in the glob is rather like *.* in something like copy *.* a: - if the * matched everything you would not need the . there. In a glob the * will match a filename like 'foo.bar' but not a path/filename like '/foo.bar' Without the trailing / the glob <$dir*$from> is trying to match files in c:/ that match the filename /Xwords\w*.puz/ If you put a file in C:/ called Xwordstest.puz it will get renamed. In the original script the $dir was 'c:/' not 'c:', that is the difference and the problem.

      A useful trick when a script does not work is to slip in some print statements to see what is going on. If you place a print in the while loop you will see that the while loop is never entered in your example.

      Good to see you back.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print