Perl, Tutorials

How to move or copy a file using Perl

There can be n number of ways to copy or move a file using Perl programming. Like invoking system command or using Linux or windows system command through backtick. But I found using a module called “File::Copy”, nice and cleaner way to accomplish the task. It will take care of Operating System issues and other dependencies.
And the good thing is, it’s available in core Perl!So we will use File::Copy module and will call copy() function to copy the file and move() function to move the files.

Ok then, How it works?

Copy Function
The copyfunction takes two parameters: a file to copy from and a file to copy to. Either argument may be a string, a FileHandle reference or a FileHandle glob. Obviously, if the first argument is a filehandle of some sort, it will be read from, and if it is a file name it will be opened for reading. Likewise, the second argument will be written to (and created if need be). Trying to copy a file on top of itself is a fatal error.

If the destination (second argument) already exists and is a directory, and the source (first argument) is not a filehandle, then the source file will be copied into the directory specified by the destination, using the same base name as the source file. It’s a failure to have a filehandle as the source when the destination is a directory.

Note that passing in files as handles instead of names may lead to loss of information on some operating systems; it is recommended that you use file names whenever possible. Files are opened in binary mode where applicable. To get a consistent behaviour when copying from a filehandle to a file, use binmodeon the filehandle.

Perl code to Copy a File

<br />
#!/usr/bin/perl<br />
use File::Copy;<br />
&nbsp;<br />
print &quot;content-type: text/html \n\n&quot;; #The header<br />
$filetobecopied = &quot;aliencoders.php&quot;;<br />
$newfile = &quot;alien_home/aliencoders.php.&quot;;<br />
copy($filetobecopied, $newfile) or die &quot;File cannot be copied.&quot;;<br />

Here, we have simply duplicated the “aliencoders.php” file . We have used a shorthand way in the example, but it is better to hardcode the full URL (file path)Move function

The movefunction also takes two parameters: the current name and the intended name of the file to be moved. If the destination already exists and is a directory, and the source is not a directory, then the source file will be renamed into the directory specified by the destination.

If possible, move() will simply rename the file. Otherwise, it copies the file to the new location and deletes the original. If an error occurs during this copy-and-delete process, you may be left with a (possibly partial) copy of the file under the destination name.

Note: It has syscopy and rmscopy functions too. Just go through the File::Copy CPAN or run

perldoc File::Copy in command prompt (I used Solaris box)

Perl code to Move the Files

<br />
#!/usr/bin/perl<br />
use File::Copy;<br />
&nbsp;<br />
print &quot;content-type: text/html \n\n&quot;; #The header<br />
$oldlocation = &quot;;;<br />
$newlocation = &quot;alien_perl/;;<br />
move($oldlocation, $newlocation);<br />

Source: CPAN

Share your comment