Operating System - Linux
1820290 Members
2880 Online
109622 Solutions
New Discussion юеВ

Re: Perl move files - does not error out

 
SOLVED
Go to solution
Ratzie
Super Advisor

Perl move files - does not error out

I need to move files from one directory to another.
I am using the perl File::Copy module

The script works, but I would like notification if the move fails.
I am moving to a folder, but if the folder is not there it will create a file with the folder name instead and not error out.

my @files=<$fin_tck_tmp/AC*>;
foreach (@files) {
move ($_, "/tmp/fin/") or die "could not move files $!";
}

Everything works if there is a folder called /tmp/fin
But if I remove the fin folder, it will create the a file call fin.
I need it to complete error out and keep those tickets in $fin_tck_tmp/
Other wise, I have files in the bitbucket that I can not get back.


3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: Perl move files - does not error out

Hi:

Use 'mkdir' and handle any exception!

# perl -le '$path="/zot/nopath"; mkdir($path,755) or die "mkdir failed for $path: $!\n"'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl move files - does not error out

Hi (again):

Oops; I dropped a part! Before you attempt to move files, either create the directory you need or verify that a directory exists:

# perl -le '$path="/tmp/fin";unless (-d $path) {mkdir($path,755) or die "mkdir failed for $path: $!\n"}'

Regards!

...JRF...
Ratzie
Super Advisor

Re: Perl move files - does not error out

Ah makes more sense.
I actually used the open DIR or die, before my move.
2 ways of skinning cat...