Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Wait until file exits before proceeding

Posted on 2004-08-23
17
1,250 Views
Last Modified: 2013-12-24
I have a web page (in CF) that generates a swf flash chart from a complex mysql query.
The problem ... I try to display the flash chart but the page loads it before its finished!

SO ... I need a way to 'pause' [wait] my coldfusion page UNTIL a file exists in a specific directory on my server.
Is this possible?

Thanks!
Richard
0
Comment
Question by:rcbuchanan
  • 6
  • 6
  • 4
  • +1
17 Comments
 
LVL 21

Expert Comment

by:pinaldave
ID: 11873168
Hi rcbuchanan,
you can use loop for certain values and wait according to that.
Though the better architecture is that you do this in following order.

mysql query
html code
flash
htmlends
you will never find this problem of page loads before sql prepares the results...

Regards,
---Pinal
0
 
LVL 21

Expert Comment

by:pinaldave
ID: 11873181
if you can post your archi or design may be you will not have to wait for the results ...they will come before you start displaying your html code.
0
 

Author Comment

by:rcbuchanan
ID: 11873312
No. I'm hoping I can use 'fileexists' or such like ... where I can delay or LOOP until the filexists condition is TRUE.

Is that possible? i.e. LOOP (continuously) UNTIL fileexists = yes?

re your suggestion for better architecture. That won't help ... as I have multiple template cfm pages loading on my page ... with many mysql selects etc.

Richard
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 21

Expert Comment

by:pinaldave
ID: 11873352
Hi rcbuchanan,
you can do that for sure http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functi79.htm#wp1104406
now let us look the good thing : this may solve your problem
bad thing: not sure but if due to some reason this falls in the infinite look and may take your complite processing power and brings your server down. This is arguable but if things are very complex this may happen and server uses its all the memory and it stops responding.

If this does not happen you can happily use the fileexists you have mentioned there is example in the link, just put that in CFIF and it will work great. I have done this before( not in loop just checking and if it is there just move to some direction like that.)

Regards,
---Pinal
0
 

Author Comment

by:rcbuchanan
ID: 11873368
can I use <CFLOOP></CFLOOP> that loops until #fileexists('filename')# is 'yes'? or such like?
do you know the syntax?
0
 
LVL 21

Assisted Solution

by:pinaldave
pinaldave earned 100 total points
ID: 11873465
Hi rcbuchanan,
I think this is the way to do this if you want to follow your logic. That is fine too... you may have to change things according to your code, I have wrote here basic design and usage of cfexit which will bring you out from your loop and go further in the progress.
you can make this fundtion and call from other page also if you want.  


<cfloop index=i from=1 to=100000>
<cfif fileexists('filename') eq 'yes'>
<cfexit>
</cfif>
</cfloop>


Regards,
---Pinal
0
 

Author Comment

by:rcbuchanan
ID: 11873477
pinaldave:
thanks. well - ... i'm not good at cf. can I then do a loop that repeats EITHER for - say - 20 seconds OR until fileexists is 'yes' ?  that way ... I can either forgot about displaying chart if technical problem means it didn't get generated or I display it!  (if 20 seconds condition is reached - DON'T DISPLAY else if fileexists = yes ... DO display'

Any ideas? for the cf code that might do this for me? i suck at understanding the cfloop condition function.

Richard
0
 

Author Comment

by:rcbuchanan
ID: 11873486
PERFECT! I'll try it!  thanks!
0
 
LVL 21

Expert Comment

by:pinaldave
ID: 11873524
Hi rcbuchanan,
good good. There is nothing in CF which can loop for some time. Though, you can just assume your processing power and page execution lenth and put the to condtion in cfloop which suites you. this is trick but the time could have been better solution. after certain time the loop will be over itself and will process futhre. Do not say that you are not good at CF. I am learning everyday something. Today you tought me this trick. I am just writing syntax but you gave me idea of file exist usage in loop. So you are designer. Smiles,

Regards,
---Pinal
0
 
LVL 17

Expert Comment

by:anandkp
ID: 11878049
A conditional loop iterates over a set of instructions while a given condition is TRUE. To use this type of loop correctly, the instructions must change the condition every time the loop iterates until the condition evaluates as FALSE. Conditional loops are commonly known as WHILE loops, as in "loop WHILE this condition is true. "

Syntax
<CFLOOP CONDITION="expression">

CONDITION
Required. Sets the condition that controls the loop. The loop will repeat as long as the condition evaluates as TRUE. When the condition is FALSE, the loop stops.
u cld use this to check for file exists - see if it helps u ...
----------------
thou i wld suggest u look into CFFLUSH & use that to determine ur output ...
syntax : <cfflush interval = integer number of bytes>

that way u avoid the unwanted processing by the loops & as soon as u have "X" no. of bytes with u - u can flsuh it to the client browser ... pretty much the way u require - is it ?

K'Rgds
Anand
0
 
LVL 21

Expert Comment

by:pinaldave
ID: 11883869
Hi rcbuchanan,
did it helped? just curious.

Regards,
---Pinal
0
 
LVL 12

Expert Comment

by:jyokum
ID: 11889412
<!--- this will loop until the file [filename] exists or 20 seconds has passed --->
<cfscript>
filename=ExpandPath('filename.txt');
tStop=DateAdd('s',20,now());
for( ; ; ){
      if (FileExists(filename)) {
            WriteOutput('the file was found');
            break;
      } else if (DateCompare(now(),tStop) gte 0) {
            WriteOutput('operation timed out');
            break;
      }
}
</cfscript>

it's important to note that this is a VERY expensive operation... on my machine, it typically eats up about 90% cpu for the duration of the loop.
0
 
LVL 12

Expert Comment

by:jyokum
ID: 11889438
here's a much better method that's not processor intensive

<cfscript>
filename=ExpandPath('filename.txt');
tStop=DateAdd('s',20,now());
o_file = CreateObject('java','java.io.File').init(filename);
thread = CreateObject("java", "java.lang.Thread");
fileFound = false;
while(DateCompare(now(),tStop) lt 0) {
      if(o_file.exists()) {
            fileFound = true;
            break;
      }
      else thread.sleep(1000);
}
</cfscript>
<cfif fileFound>
      THE FILE WAS FOUND
<cfelse>
      OPERATION TIMED OUT
</cfif>
0
 

Author Comment

by:rcbuchanan
ID: 11897092
jyokum:
thanks. ... question : the 'filename' I need to 'check for' ... is based as follows:

<cfset string_opid = #numberformat(url.op,"00000")#>
<cfset string_date = #dateformat(now(),"yyyymmdd")#>
<cfset swfFilename = #string_opid# & #string_date# & ".swf">
<CFSET dirpath = "c:\web\opinionist\dev\htdocs\charts\">
<CFSET lookupFilename = #dirpath# & #swfFilename#>

how can I modify your suggested <cfscript> to look for the file as defined by 'lookupFilename' rather than 'filename.txt') ?

ALSO ... the file I need to look for ... is a flash file (.swf) created by SwiffCharts (see globfx.com).
I wonder ... I cannot 'show' the flash chart UNTIL it's completed (processed).  Would it be wiser for me to use your code to ONLY view the file when it exists and its filesize > 0 bytes?

My problem: I think my page is trying to a) generate the flash chart and b) display it BEFORE it is finished being created!  I think I should wait until the file exists AND it's file size > 0 (blank?)

Does this sound feasible to you?

pinaldave : i'm sorry ... but your suggestion just doesn't want to work in my page!

Richard



some  more info:
--------------------

I have a .cfm web page that DOES these two things:
      <cfinclude template="process_opinion_linechart.cfm">
      <cfinclude template="template_opiniontimeline.cfm">

the "process_opinion_linechart.cfm":
------------------------------------------

<cfset string_opid = #numberformat(url.op,"00000")#>
<cfset string_date = #dateformat(now(),"yyyymmdd")#>
<cfset swfFilename = #string_opid# & #string_date# & ".swf">
<CFSET dirpath = "c:\web\fred\dev\htdocs\charts\">

<cfif fileexists(#dirpath# & #swfFilename#) eq 'yes'>
<cfelse>

<!--- Sets the filename of the style.  This assumes that the style is in the same directory as this script --->
<cfset chartStyle = "newline460.scs">

<!--- Load Opinion Data --->
<cfquery name="loadData" datasource="fred">
select opinionvotes.vote_date, sum(opinionvotes.vote_want_yes) as want_yes, sum(opinionvotes.vote_want_no) as want_no, sum(opinionvotes.vote_agree_yes) as agree_yes, sum(opinionvotes.vote_agree_no) as agree_no from opinionvotes where opinionvotes.opinion_id=#url.op# group by opinionvotes.vote_date order by opinionvotes.vote_date
</cfquery>       
        
<!--- Don't have to touch this --->
<CFSETTING ENABLECFOUTPUTONLY="Yes" SHOWDEBUGOUTPUT="No">

<CFOBJECT TYPE="COM"
        NAME="chart"
        CLASS="SwiffChartObject.ChartObj.1"
        ACTION="CREATE">

<!--- Define seperators rules. Don't have to touch this --->
<CFSET sep= ";">
<CFSET ignoremultseparators= true>
<CFSET chart.SetSeparators(sep, ignoremultseparators)>

<cfset agree_total_yes = 0><cfset agree_total_no = 0>
<cfset want_total_yes = 0><cfset want_total_no = 0>
<cfset agree_string = ''><cfset want_string = ''>
<cfset myCategories = ''>
<cfset pointer = 0>

<cfloop query="loadData">

<!--- Create a string for each series --->
<CFSET myCategories= myCategories & "#dateformat(loadData.vote_date,'mm/dd')#" & sep>

<cfset agree_total_yes = agree_total_yes + #loadData.agree_yes#>
<cfset agree_total_no = agree_total_no + #loadData.agree_no#>
<cfset want_total_yes = want_total_yes + #loadData.want_yes#>
<cfset want_total_no = want_total_no + #loadData.want_no#>

<!--- Create a string for each series --->
<cfset agree_percent = #round(agree_total_yes / (agree_total_yes + agree_total_no) * 100)#>
<cfset want_percent = #round(want_total_yes / (want_total_yes + want_total_no) * 100)#>
<cfset agree_string = agree_string & "#agree_percent#" & sep>
<cfset want_string = want_string & "#want_percent#" & sep>

</cfloop>
 
<!--- Fill the chart with the series --->
<CFSET chart.SetCategoriesFromString(myCategories)>
<CFSET chart.AddSeries()>
<CFSET chart.SetSeriesValuesFromString(0, agree_string)>
<CFSET chart.AddSeries()>
<CFSET chart.SetSeriesValuesFromString(1, want_string)>
<CFSET style = dirpath & chartStyle>
<CFSET chart.LoadStyle( style )>

<!--- Set the dimensions of the movie.  IF YOU CHANGE THIS, YOU NEED TO CHANGE THE SIZE BELOW TO REFLECT THIS! --->
<CFSET chart.SetWidth( 460 )>
<CFSET chart.SetHeight( 200 )>

<!--- This just sets where the graph gets generated --->
<CFSET account_id = "100">
<CFSET filename = dirpath & swfFilename>
<CFSET chart.ExportAsFile(filename)>
<cfset chartInfo.filename = swfFilename>

</cfif>


the "template_opiniontimeline.cfm" does this:
----------------------------------------------------

<cfset string_opid = #numberformat(url.op,"00000")#>
<cfset string_date = #dateformat(now(),"yyyymmdd")#>
<cfset swfFilename = "http://www.fred.com/charts/" & #string_opid# & #string_date# & ".swf">

<table width="460" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
  <tr>
    <td width="460" colspan="1"><img name="label_hottopics" src="/newopex_graphics/label_opiniontimeline460x30.jpg" width="460" height="30" alt="" border="0"></td>
  </tr>
  <tr>
        <td width="460" height="200">
        <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="460" height="200">
        <param name="movie" value="<cfoutput>#swfFilename#</cfoutput>">
        <param name="quality" value="high">
        <embed src="<cfoutput>#swfFilename#</cfoutput>" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="460" height="200"></embed>
      </object>
      </td>
  </tr>
</table>



SO - When I run these pages ... sequentially : the process cfm then the display page ... I DON'T get the flash chart ... but instead ... only the swf page's file name as text!

BUT if I look at my /chart/ directory .. I CAN see the flash file HAS been successfully created!
AND when I run the page a 2nd time ... the flash chart DOES successfully LOAD!

Any ideas!?
Richard
0
 
LVL 12

Accepted Solution

by:
jyokum earned 400 total points
ID: 11897983
this adds some checking for the file size and uses your file name

<cfset string_opid = numberformat(url.op,"00000")>
<cfset string_date = dateformat(now(),"yyyymmdd")>
<cfset swfFilename = string_opid & string_date & ".swf">
<cfset dirpath = "c:\web\opinionist\dev\htdocs\charts\">
<cfset lookupFilename = dirpath & swfFilename>

<cfscript>
tStop=DateAdd('s',5,now());
o_file = CreateObject('java','java.io.File').init(lookupFilename);
thread = CreateObject("java", "java.lang.Thread");
fileFound = false;
while(DateCompare(now(),tStop) lt 0) {
      if(o_file.exists() AND o_file.length() gt 0) {
            fileFound = true;
            break;
      }
      else thread.sleep(1000);
}
</cfscript>
<cfif fileFound>
      THE FILE WAS FOUND
<cfelse>
      OPERATION TIMED OUT OR THE FILE IS 0 BYTE
</cfif>
0
 
LVL 12

Expert Comment

by:jyokum
ID: 11897994
oops, just noticed I left some test data in

change this
     tStop=DateAdd('s',5,now());
to
     tStop=DateAdd('s',20,now());

The number is the seconds to wait before timing out
0
 

Author Comment

by:rcbuchanan
ID: 11915399
did exactly what i asked for - thanks!
alas I'm still experiencing my error BUT I'lll open a new thread and will try again!

R.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Configure a Local PHP Interpreter for Phpstorm 2 337
.dwt files not viewable in browser - why? 2 102
exchange 2010 turning off 3des ciphers 2 303
Company website 6 30
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

861 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