Copying Multiple Directories, then verifying the transfer with Perl ?

Posted on 2001-06-17
Last Modified: 2010-03-05
Okay, heres the prob I have, I am just not sure if its possible.

I want a user to log on to their intranet "back up page"

then specify which directory they want to back up on their part of the server. Then the script will need to transfer that directory (unfortunately including subdirs and files) to another location, then if the data transfer was successful then delete the dirs/files that were backed up.

This is on a network, where the server has full rights/permissions. But I dont think that perl can transfer multiple subdirs ? so I was thinking along the lines of the perl script calling maybe a batch file in order to copy information (but then I remembered that perl cant talk to batch files, or cant it), it was these point that I thought sheesh,

lets ask the experts :)
Question by:scooter41
  • 4
  • 3
  • 2

Expert Comment

ID: 6200416
perl can do what you want, or you can use the system() or ` operator to run any batch file or commands straight from the perl.

if this is win32 and you don't want to get to technical,

just get the root directory to backup
and do
`xcopy $dirname newdir`;

if you want to be more technical, you can open each file and write it to a new location.

but I think at this point I would just use the commands made available by the os.


Author Comment

ID: 6201099
okay bob, thats brilliant, thanks for your help so far, but just one thing still remains, how do i check that the copy has been 100% error free before deleting ? the data will be important docs and is vital that it cannot be deleted unless backed up error free.

Do you think i could just take the properties of the two dirs for example and compare them ?

Expert Comment

ID: 6201230
just compare files' sizes.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

bebonham earned 200 total points
ID: 6204899
well, I mean

in the event that the file wouldn't be ok ?!?

99.99 unlikely

then the chance of the size being right but one character being switched is probably 99.99 so file size would still let .00001% corrupt files through :)

personally, I have never seen it happen put you can use

$nfilesize=${[stat $newFile]}[7];
$ofilesize=${[stat $oldFile]}[7];


Author Comment

ID: 6205230
Thanks Bob !

Expert Comment

ID: 6206845
if course getting the file size is easier with -s

die ("bla...!\n") if ( (-s $newfile) != (-s $oldfile) );

Expert Comment

ID: 6208883
thanks shlomoy!

I just learned stat :)

funny how in perl, seems like everybody is good at different stuff...

just wish I new the basics a little better.

especially the mysterious (to me) pipe and fork.


Expert Comment

ID: 6209047
to learn more about pipe and fork read:

               Opens a pair of connected pipes like the
               corresponding system call.  Note that if you set
               up a loop of piped processes, deadlock can occur
               unless you are very careful.  In addition, note
               that Perl's pipes use stdio buffering, so you may
               need to set `$|' to flush your WRITEHANDLE after
               each command, depending on the application.

               See the IPC::Open2 manpage, the IPC::Open3
               manpage, and the Bidirectional Communication entry
               in the perlipc manpage for examples of such

               On systems that support a close-on-exec flag on
               files, the flag will be set for the newly opened
               file descriptors as determined by the value of
               $^F.  See the section on "$^F" in the perlvar

       fork    Does a fork(2) system call to create a new process
               running the same program at the same point.  It
               returns the child pid to the parent process, `0'
               to the child process, or `undef' if the fork is
               unsuccessful.  File descriptors (and sometimes
               locks on those descriptors) are shared, while
               everything else is copied.  On most systems
               supporting fork(), great care has gone into making
               it extremely efficient (for example, using copy-
               on-write technology on data pages), making it the
               dominant paradigm for multitasking over the last
               few decades.

               Beginning with v5.6.0, Perl will attempt to flush
               all files opened for output before forking the
               child process, but this may not be supported on
               some platforms (see the perlport manpage).  To be
               safe, you may need to set `$|' ($AUTOFLUSH in
               English) or call the `autoflush()' method of
               `IO::Handle' on any open handles in order to avoid
               duplicate output.

               If you `fork' without ever waiting on your
               children, you will accumulate zombies.  On some
               systems, you can avoid this by setting
               `$SIG{CHLD}' to `"IGNORE"'.  See also the perlipc
               manpage for more examples of forking and reaping
               moribund children.

               Note that if your forked child inherits system
               file descriptors like STDIN and STDOUT that are
               actually connected by a pipe or socket, even if
               you exit, then the remote server (such as, say, a  
               CGI script or a backgrounded job launched from a
               remote shell) won't think you're done.  You should
               reopen those to /dev/null if it's any issue.                                        

Expert Comment

ID: 6209049
these man pages will also be useful:

perldoc perlipc
perldoc perlfork
perldoc -f open

(you can also look those up in

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Existing perl code to be changed for ftp to sftp handling 14 241
Awk Question 2 132
Insert Text into odbc.ini file 15 71
Regex rule to match two different url 5 76
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question