Solved

Working with files in coldfusion

Posted on 2012-03-28
5
220 Views
Last Modified: 2012-03-28
I get a daily txt file with image links (see attached txt file)textfile.txt
And I parse these links into a MySQL 5 database table.
What I want to do is download these images to a folder on my server, and rename them to match the MLS Number, like 117220-1.jpg, 117220-2.jpg, 117220-3.jpg, etc..

In looking at adobe.com I found the following bit of code that should download the image to my folder but what I need help with is when there are more than one photo per MLS Number as described above. Am I even on the right track? Any help appreciated!

<!---query my table and loop over results--->
<cfquery name="photos">
select * from listings.photos where Date Last Modified IS #Now()#
</cfquery>

<cfoutput query="photos">

<cfhttp
    method="Get"
    url="#Photo Link#"
    path="c:\myimagesfolder"
    file="#MLS Number#.jpg">
<cfoutput>
    #cfhttp.MimeType#
</cfoutput>

</cfoutput>
0
Comment
Question by:Bang-O-Matic
  • 3
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 37779164
If it's not a lot of images (because cfhttp would timeout), you could use a grouped cfoutput with a counter variable.  The counter would act as a suffix for the image names.  Just increment it on each loop and reset it each time the MLS number changes.  That'll give you names like: 52188_1.jpg, 52188_2.jpg, etc...
               
* note: must order by MLS number or it won't work
* use a broader date filter than IS NOW()

<cfquery name="photos">
    SELECT  PhotoLink, MLSNumber FROM  listings.photos
    WHERE  DateLastModified > #theLastDateYouPulledImages#
    ORDER BY MLSNumber
</cfquery>

<cfoutput query="photos" group="MLSNumber">
        <!--- this is a new MLS number. reset the counter --->
        <cfset counter = 1>
        <cfoutput>
            <cfhttp method="Get" url="#PhotoLink#"
                        path="c:\myimagesfolder"
                        file="#MLSNumber#_#counter#.jpg">
             
              ... do other stuff
        </cfoutput>
</cfoutput>
0
 

Author Comment

by:Bang-O-Matic
ID: 37779227
Hi _agx_, thanks for this solution! Initially I will have to download all images, approx 80,000. Then I can just run this update daily. How should I approach getting all the images the first time?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37779289
I doubt you'll be able to get all 80K in one shot.  The page will almost certainly timeout 1st, assuming the other site doesn't throttle access and block that kind of volume.

I'd probably set it up as a one time scheduled task. The task would process the images in batches of N (some reasonable number like 100 or 250). The basic process is:

- create a cfm script that queries your image table for the next N unprocessed images.
- loop through the results and for each one:
      1) download the image
      2) mark the record as processed
      lather, rinse, repeat until there are no more images

- Finally query the table to get the total unprocessed images remaining. If it's > 0, have the scheduled task run itself again. When there are no more images to process the task ends.

Though if you want to use the "grouped" output above, you'd also have to keep track of the last MLS number and counter from the previous run. Otherwise you might overwrite a few images. But you can easily pass them along along as URL parameters.
0
 

Author Comment

by:Bang-O-Matic
ID: 37779315
Sounds like a plan. Thanks for your help!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37779420
WHERE  DateLastModified > #theLastDateYouPulledImages#

Typo correction that obviously should be on or before theLastDateYouPulledImages. ;-)

.. and I left out the increment in the pseudo code. But that's pretty self explanatory.

<cfoutput query="photos" group="MLSNumber">
        <!--- this is a new MLS number. reset the counter --->
        <cfset counter = 1>
        <cfoutput>
            <cfhttp method="Get" url="#PhotoLink#"
                        path="c:\myimagesfolder"
                        file="#MLSNumber#_#counter#.jpg">
             
              ... do other stuff
            <cfset counter = counter + 1>
        </cfoutput>
</cfoutput>
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 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