-1

I have a small script to read from a directory in which files are going to copied to a specific folder. My script will read from the file and check whether we have received all the files that have been successfully copied.

As soon as all the files are received the next set of jobs are triggered, which process those files.

I am using the normal Input operator to read the file:

if(open(FILE,abc.txt))
  print "it opened \n";
} else {
  print "it did not open \n";
}

The above method didn't work and also the read write (+<) didn't work. Why?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Deepak
  • 1
  • 4
    `use strict; use warnings;` – mob Oct 07 '15 at 04:13
  • What's the question? – Matt Jacob Oct 07 '15 at 04:31
  • The files are ftp ed in to that folder. So I want to consider a file has received only if the file has finished copying in to the folder. I thought if I open a file when it is copied in to(when in use) open will not allow. But I am able to open when file s in use. I want to validate that.. – Deepak Oct 07 '15 at 15:03

1 Answers1

1

try this if you want to check whether a file exists:

if ( -e 'abc.txt' )
{
  print "file exists\n";
}

If you are looking to check whether the file is available for read/write access ( ie ensure that another process writing the file is does not have a lock within a windows environment ) most crudely you can catch the unable to open file condition:

open(INFO,      "+<datafile") || warn("can't open datafile: $!");

In windows I believe that Determine whether a file is in use in Perl on Windows discusses how to catch the failure however that appears a little crude.

The approach at https://stackoverflow.com/a/4200474/5182165 looks more satisfying to me.

I would suggest looking at fileno() or fcntl or include lsof in your searches for more detail.

You may also want to look at flock() as described at http://docs.activestate.com/activeperl/5.8/lib/pods/perlopentut.html#file_locking

Another interesting approach is to watch the directory for file changes using Win32::ChangeNotify as discussed at Windows - Using perl monitor a directory for a new file drop/creation

Or you could use Win32::File::Object to check whether the file has an OFFLINE status as per http://codeverge.com/perl.beginners/win32-file-attribs-example/144600

Community
  • 1
  • 1
Peter Scott
  • 1,318
  • 12
  • 19
  • I actually want to check whether the file has been completely copied in to the drive. So I am trying to open the file to check whether the file is still in use (while copying) but I am able to open the file even when the files are getting copied in to that folder ( even file is in use).... I want to have a validation that only if the file has successfully copied in to that location I should consider use that file.... – Deepak Oct 07 '15 at 15:00
  • not sure what your use case is but it also sounds like you might get value by looking at something like https://en.wikipedia.org/wiki/CwRsync – Peter Scott Oct 07 '15 at 15:39