Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping until file exists? 1

Status
Not open for further replies.

spook007

Programmer
May 22, 2002
259
US
I've got CF code that initiates a 3rd party app to generate a file on the web server. I'm trying to create a cfloop condition statement that will continue to loop until the file exists on the server and then redirects the user to it. Unfortunately the following code that I have is not working at all. I've checked the server and the file does indeed exist, but the conditional loop never caught it and goes on forever. Any suggestions welcomed!

<cfset user = url.timekeep>
<cfset filename = user & dateFormat(now(),"mmddyy")& ".pdf">
<cfset path = "/payroll/" & user & "/" & filename>
<cfset completePath = ExpandPath(path)>
<cset urlPath = " & user & "/" & filename>

<cfloop condition="fileExists(completePath)">
</cfloop>

<cflocation url="#urlPath" addtoken="no">
 
sorry, type-o on this line:

<cflocation url="#urlPath" addtoken="no">

should be:

<cflocation url="#urlPath#" addtoken="no">
 
conditional loops iterate instructions as long as the condition IS TRUE. also, to use this type of loop correctly, the instructions must change the condition every time the loop iterates, until the condition is false. it doesn't seem to me that the loop would go on forever in your case, unless the file did exist on its first iteration, else it should not be performed at all.

could you explain more about what the purpose of this loop is? i suspect there may be a better way of doing it.


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
I've got a third party software that will take my crystal reports and publish them on the web as pdf files. In order to trigger them it is monitoring the log.txt file located on the web server. When this file is modifed, the third party software will generate a report and publish it as a pdf in the same directory.

What I want my script to do is:
1. remove any previous pdf file (got this to work)
2. modify the log.txt file trigger 3rd party software to generate pdf file. (got this to work)
3. Have CF template monitor directory until file exists (should take approx 5-10 sec depending on size) (not working!!)

You are right about it going forever, my original code had other stuff that kept it going forever. I removed that and now it just doesn't run the loop.
 
I was always under the impression that conditional loops would continue to execute until the condition was met?
 
i would not use a conditional loop for this, would be nice if CFMX had a <CFPAUSE> tag similar to bluedragon- but that's not the case.

Rather, I would use an index loop and keep checking for the file until the file exists, then break when condition is met.
Code:
<cfloop index="i" from="1" to="5000">
   <cfif (fileexists condition)>
      :::CODE FOR WHEN FILE EXISTS:::
      <cfbreak>
   </cfif>
</cfloop>

its not tested, and its not what i would consider a 'solid' solution, but it may work for you.


=========================================
I have not failed. I've just found 10,000 ways that won't work.
Thomas A. Edison
 
Thanks NorthStarDA, I think what I'm going to try is loop between two templates. So the one template would check if the file exists, if not it will load up a new template that would check and go back and forth until the condition is met and would redirect the user to the pdf.

Your suggestion will work, but I'll have to make the cycle range large enough so that I can be assured that the file will definately be there. I'll try both ways and find out the best alternative. Thank you very much for your input!
 
You could use a simpler approach with a Meta Refresh of 5 seconds with the page verifying the pdf is there or not. This would require less processing cycles of a continuous loop and less page calls of one page after the other until the file appears.

Or Combine your 2pages with a <meta http-equiv="Refresh" content="5; URL=page2.cfm">

xtendscott
Web Site Design and CF Programming
 
Excellent suggesting... works like a charm! Thanks xtendScott!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top