1

I'm trying to loop through a folder on a server to get a list of specific files and then have those files copied to a temp directory or zipped and copied to a temp directory. However I keep running into wall as I'm not sure how to do this. (Still an amateur with ColdFusion)

so here I do a SQL Query where I retrieve the exact attachments from specific dates I am looking for:

<cfquery name="test" datasource="test" cachedwithin="#CreateTimeSpan(0,0,10,0)#" result="r">
SELECT Test.TestNum, Test1.Test1Date, TestReport.Attachment
FROM Test
INNER JOIN more SQL code here..
WHERE Test1.Test1Date >= '#daterangevariablehere#'
AND NOT more SQL code here as well..
ORDER BY Test.TestNum
</cfquery>

The SQL Query is correct as in SQL Server Mgmt Studio, it executes properly and even before I try to do a cfdump it shows the correct list of files I am trying to get. However when I try to loop through them to copy or zip them, it's a total fail -> blank page.

I tried to:

<cfloop query="test">
<cfif test.recordcount gt 0>
<cfzip action="zip" file="#LocationOfwhereIwantTheEndResult" source="#WhereTheFolderWithTheAttachmentsReside#">
</cfzip>
</cfloop>

I even tried to do this, wrapped around the above code:

<cfdirectory action="list" name="test" directory="#WhereTheFolderWithTheAttachmentsReside#"></cfdirectory>

Side note: some of the variables and code are made up for the sake of security and anonymity..but I wanted to show the structure of what I'm trying to accomplish I hope I was clear(!) if otherwise please do let me know and I will provide more detail or information. ANY help is really appreciated, I have been banging my head against a wall on this one and feel like it might be something super simple. Please help! :)

Subah
  • 45
  • 9
  • You don't have a closing `` for this ``.(which is useless inside the `` anyway) – David Faber Feb 05 '19 at 17:18
  • Sorry I forgot to put that in the question but I do have a closing in my code. Can you elaborate or be specific as to why it's "useless" inside a ? – Subah Feb 05 '19 at 17:22
  • Why are you testing for a recordcount within a loop? you would do that before looping – volume one Feb 05 '19 at 17:23
  • 1
    @Subah, if the value of recordCount is `0` for this query then the loop will never execute. You can put the `` _outside_ the loop and it will have an effect. – David Faber Feb 05 '19 at 17:23
  • @volumeone because I'm trying to loop through the sql query I initially have where I get the attachments list of what I need and then if there is anything that is returned from the last 7 days (sometimes there is not) so I test to see if it there is or not -> why I placed a .recordcount gt 0 there.. – Subah Feb 05 '19 at 17:27
  • Thanks @DavidFaber I will try that. Disclaimer (incase you didn't really read what I put up in my question before I presented some sample code): I am still learning ColdFusion(!!) – Subah Feb 05 '19 at 17:29
  • What version of ColdFusion? – Shawn Feb 05 '19 at 18:01
  • @Shawn It's ColdFusion 2016. – Subah Feb 05 '19 at 21:21

2 Answers2

2

Without knowing the actual values, it sounds like your query contains absolute paths of individual files. One option is to loop through the query and add a cfzipparam for each file. Here's a stand alone example, you can adapt. (It uses CFML because that's what's in your example, but if your comfortable with script syntax, look for the cfscript based versions in the link above).

CFML Example:

<!--- For Demo only to simulate your database query --->
<cfset test = queryNew("Attachment"
            , "varchar"
            , [{Attachment="c:\path\to\fileA.png"}
                , {Attachment="c:\path\to\fileB.png"}
                , {Attachment="c:\path\to\fileC.png"}
            ])>

<cfzip action="zip" file="c:\path\to\TheNameYouWant.zip">
    <cfloop query="test">
        <!--- If Attachment only contains a file name (i.e. "someFileName.pdf") 
           use your variables to build a full path, i.e. "c:\path\to\someFileName.pdf" 
         --->
        <cfzipparam source="#Attachment#">
    </cfloop>
</cfzip>

CFScript Example

<cfscript>
   test = queryNew("Attachment"
            , "varchar"
            , [{Attachment="c:\path\to\fileA.png"}
                , {Attachment="c:\path\to\fileB.png"}
                , {Attachment="c:\path\to\fileC.png"}
            ]);

    cfzip (action="zip", file="c:\path\to\TheNameYouWant.zip") {
        for (row in test) {
            cfzipparam (source=row.Attachment);
        }
    }
</cfscript>

Brief aside, you don't have to post actual paths. Most of the time, it's irrelevant anyway ;-) However please do post hard coded values that represent the actual path - NOT variables. The reason being variables leave a LOT of open questions, that could easily be answered by using a dummy path like c:\path\to\fileA.pdf. In this specific case, knowing things like your o/s, whether the sources are files/directories or relative/absolute paths, would help us rule out several potential issues right off the bat.

SOS
  • 6,430
  • 2
  • 11
  • 29
  • Thank you for the advice/help! I actually don't have individual files laid out to copy because there are over 60 of them that need to be copied and zipped - which is why I can't individually write out their path, that would take me forever. I wanted a more efficient solution or a loop that could resolve this. I initially tried cfdirectory to list out all the files and it did but to get each of the file in that list copied and into a zip file, I am lost on this..thoughts? – Subah Feb 05 '19 at 21:22
  • Also, would like to add this is a script that will be run every week to get those files as they will be different every week, they are generated daily. – Subah Feb 05 '19 at 21:24
  • You don't need to write out anything :-) The example above uses a ["manual" query](https://cfdocs.org/querynew) named "test" for demo purposes, so others reading this thread can test the code without having to create database tables. Since you already have a database query in your script, just add the `....` code, and change the `file` name and path as desired. Assuming "Attachments" contains an absolute path (you didn't provide an example, so I'm just guessing...), it will create a zip file containing all of the files in that query. – SOS Feb 05 '19 at 21:54
0
<cfif test.recordcount>
  <cfloop query="test">
    <cfzip action="zip" file="#LocationOfwhereIwantTheEndResult#" source="#WhereTheFolderWithTheAttachmentsReside#">
    </cfzip>
  </cfloop>
</cfif>

I suspect that your file and source locations are incorrect. Use ExpandPath() function to make sure its going to the correct place.

rrk
  • 15,677
  • 4
  • 29
  • 45
volume one
  • 6,800
  • 13
  • 67
  • 146
  • I only used these locations for the sake of the post and anonymity - not going to post the actual location on this website (because I don't know you at the end of the day) However I have tried 'ExpandPath()' and it leads me nowhere. The File and Source locations are correct because outside of the loop it actually zips the file. I need it to be a specific list according to the SQL Query I have above. – Subah Feb 05 '19 at 17:31
  • 1
    To @halfer - next time you edit a question, there should be an actual reason to do so. My question has ColdFusion in front so it is visible and helpful to others when doing a Google search - very standard. Removing that makes it a vague unsearchable resource. – Subah Feb 11 '19 at 17:28