in reply to Re: File remaing and removal to a subdirectory
in thread File renaming and removal to a subdirectory

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 !";

Replies are listed 'Best First'.
Re: Re: Re: File remaing and removal to a subdirectory
by tachyon (Chancellor) on Jul 12, 2001 at 11:13 UTC

    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