|
Hi,
I was wondering how I could check for a file until it finally exists. The high-end depiction:
1. User runs my perl script, my perl script starts checking for files to read.
2. User runs another program which generates files.
3. My perl script reads the files as they are created by the other program.
The names of the files are already pre-determined, so there's no need for me to dynamically determine what has been created, or anything fancy like that. My script just wants to read x.txt, and I know it's not going to exist until later; this is what I have now:
open(COMM, "<", "/forum/CFPSTestComm.txt" || print "trying to open file");
while(!COMM)
{
open(COMM, "<", "/forum/CFPSTestComm.txt" || print "still trying to open file.");
}
Is there a better way to check until the file exists?
|
|
|
im not sure how cpu demanding your method is.
if it seems to be a bit too demanding of the cpu then just add a sleep 1; in there.
open(COMM, "<", "/forum/CFPSTestComm.txt" || print "trying to open file");
while(!COMM)
{
open(COMM, "<", "/forum/CFPSTestComm.txt" || print "still trying to open file.");
sleep 1;
}
otherwise there's always the
while !(-e "/forum/CFPSTestComm.txt")
{
open(COMM, "<", "/forum/CFPSTestComm.txt" || print "still trying to open file.");
sleep 1;
}
|
|
|
Thanks for the reply!
I tried that -e solution, but my script closes immediately after running. I'm assuming it is some flag to check that the file exists, but if not, could you enlighten me?
|
|
|
#oops, it was supposed to be with extra brackets around the "!":
while (!(-e "/forum/CFPSTestComm.txt"))
{
open(COMM, "<", "/forum/CFPSTestComm.txt" || print "still trying to open file.");
sleep 1;
}
#and yes the -e option is to check if file exists.
|
|
|
|
|
|
|
// |