Hi Joe, here is a minimally modified version that works fine. The rename will fail if the target dir does not exist so I have added a line to make it if it does not.
#!/usr/bin/perl -w use strict; my $dir = "c:/test/"; my $from = '.pl'; my $to = 'A.pl'; my $newdir = "c:/test/test/"; # make the target dir if it does not exist unless (-d $newdir) { mkdir $newdir or die "Unable to make dir $newdir $!\n"; } my $count = 0; while (<$dir*$from>) { my ($old,$new,$full); $full = $_; $new = $old = substr $full, length $dir; $new =~ s/$from$/$to/; print "Full name of original file is $full \n"; print "Name of original file is $old \n"; print "Full name of new file is $newdir$new \n"; $count++ if rename $full, $newdir.$new or die "Error renaming $!\n +"; } print "$count Files renamed!" if $count; # everything after the __END__ token is ignored by Perl __END__ # here is script with comments
cheers# first the indentation does not help you read this. # we indent to help identify blocks of code like: if ($this) { # do stuff } else { # do other stuff } # end of the if then else # so we are back at left margin #!/usr/bin/perl -w <- good use strict; <- good my $dir = "c:/Xwords/"; my $n=length "c:/Xwords/"; # confusing why use $n why not $len +gth # also why not length $dir # that is what you want afterall # why type you dir twice: laziness +and typos my $from = '.puz'; my $to = 'A.puz'; my $newdir = "c:/Xwords/PrintedXwords/"; while (<$dir*$from>) { my ($old,$new,$full); $old = $full = $_; # see how I coded this $new = $old = substr $_,$n; # it is the same but cleaner $new =~ s/$from$/$to/; # you have not coded anything so why the two blank lines print " \n \n"; # if you put in a leading space you get... # if you and blank lines do this print "\n\n.... print " full name of original file is $full \n"; print " name of original file is $old \n"; # you don't need the . to concatenate in a string # you just need "$newdir$new". also { are literals print " full name of new file is {$newdir.$new} \n"; # if you want 3 blank lines go \n\n\n" print " \n \n"; # this need to be written as I did with no curlies # curlies are block delilimiters # you should check if this suceeds or fails as demonstrated # above with the or die "message $!" the message lets you # say why you have died and $! if the Oh f@#$ varibale # where perl puts the error message. The die alone just # gives a line number rename $full, {$newdir.$new}; } # I have added a counter that will register if all goes to plan # this will always give you a cheery message regardless! print "Files renamed !";
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
In reply to Re: Renaming and moving files to a subdirectory
by tachyon
in thread Renaming and moving files to a subdirectory
by Joes
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |