Friday, September 23, 2011

Changing a file name in a different folder, Perl?

I have a script that calls a folder the name the user inputted, then copies a file into that folder, I now need it to rename the copied file to the name of the folder, how do I do this?

So far i have

use File::Copy;

print %26quot;please enter filename: %26quot; ;

$folder=%26lt;%26gt; ;

chop($folder);

mkdir $folder;

$filetobecopied = %26quot;blank contact log.doc%26quot;;

copy($filetobecopied, $folder) or die %26quot;File cannot be copied.%26quot;;

print %26quot;Folder created and file moved :)%26quot;;

print %26quot;Please press Enter to exit:%26quot;;

$other=%26lt;%26gt;;



I use $other to pause the program, I need it to rename the blank contact log.doc to %26quot;foldername%26quot; contact log.doc, so if I called the folder bob the document would be called bob contact log.doc but I have no idea how to do this.Changing a file name in a different folder, Perl?You do NOT copy and then rename a file. You just copy it to the new name you want it to be.



use File::Spec::Functions;

my $new_name = $filetobecopied;

$new_name =~ s/blank/$folder/;

copy($filetobecopied,

catfile($folder, $new_name));

No comments:

Post a Comment