Solved

Working with files in coldfusion

Posted on 2012-03-28
5
219 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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