Broad Network


Deleting and Copying Files with Perl

Handling Files and Directories in Perl – Part 7

Perl Course

Foreword: In this part of the series, I explain how to delete files, and copy files from one directory to another.

By: Chrysanthus Date Published: 27 Aug 2015

Introduction

This is part 7 of my series, Handling Files and Directories in Perl. In this part of the series, I explain how to delete files, and copy files from one directory to another. To move a file, you copy and then delete the source. You should have read the previous part of the series before reaching here, as this is a continuation.

Delete a File
To delete a file, you use the unlink function. On success, this function returns the number of files it successfully deleted. On failure, it returns false and sets $! (errno). You should have write permission to the directory. Assume that you have the file, myfile.txt in the directory, c:/dirA/dirB, you can delete the file as in the following code:

use strict;

    my $no = unlink("c:/dirA/dirB/myfile.txt");

    if (!$no)
        {
            print "error: $!";
        }
    else
        {
            print "No. of files deleted: $no";
        }

The unlink function can actually delete more than one file. Just separate the arguments with commas.

Copying a File
To copy a file, you need to have read permission for the source directory and read permission for the source file. You also need to have write permission for the destination directory.

With those permissions, you open the source file for reading and open the destination file for writing. As you copy one line from the source file, you send (print) the same line to the destination file. You then close the two files.

The next thing to do is to obtain the permissions and ownership of the source file and then set the same permissions and ownership to the destination file.

The stat() function returns 13 elements. The third element is the mode (permissions). The fifth element has the numeric user ID of file's owner and the sixth element has the numeric group ID of file's owner. You can use the stat function to obtain these values from the source file. To set the permissions of the destination file, you use the chmod() function. You assign the ownership using the chown() function.

The following code will copy the file, afile.txt from the directory, c:/dir1/dir2 to the directory c:/dirA/dirB.

use strict;

    open(in, "<", "c:/dir1/dir2/afile.txt") or die "cannot open file : $!";
    open(out, ">", "C:/dirA/dirB/afile.txt") or die "cannot open file : $!";

    while(<in>)
        {
            print out $_;
        }

    close in;
    close out;

    my (undef,undef,$mode,undef,$uid,$gid,undef,undef, undef,undef,undef,undef,undef) = stat("C:/dirA/dirB/afile.txt");

    my $cnt1 = chmod ($mode, "C:/dirA/dirB/afile.txt"); print $cnt1, "\n";
    my $cnt2 = chown ($uid, $gid, "C:/dirA/dirB/afile.txt"); print $cnt2, "\n";

Note that the filehandle in the while condition is in the angle brackets operator. Also know that the programmer did not need to know the actual numeric values for $mode, $uid and $gid.

Moving Files
To move a file, you copy the file first and then you delete the source file. So if the above file was to be moved, you will just have to add the following code segment at the bottom of the script:

    my $no = unlink("C:/dirA/dirB/afile.txt");

    if (!$no)
        {
            print "error: $!";
        }
    else
        {
            print "No. of files deleted: $no";
        }

In this case you also need the write permission for the directory of the source file.

That is it for this part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK

Comments

Become the Writer's Fan
Send the Writer a Message