How do I loop through a query and pass one result at a time rather than all results?
I'm running a query and using <cfloop> to loop through the result and for every file name in the query, <cffile> needs to move that file from one folder to the next as follows:
cfquery name="qryGetFilesJustUploaded" datasource="#request.dsn#"> <!--- Limit to filed with pdf file type endings --->
SELECT fileupload
FROM [DevDBServer].[dbo].[upload_many]
WHERE (fileupload Like '%.pdf%') and (needs_compression = '1')
</cfquery>
cffunction name="MoveCompressReturnFile" access="public" returntype="void" description="Moves file to temp folder, compresses it then returns it to its original location">
<!---Lets Loop through and move all files references in query--->
<cfloop query="#qryGetFilesJustUploaded#">
<cffile action="move"
source="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin" >
<!--- Now lets compress it--->
<cfexecute name="C:\Program Files (x86)\neeviaPDF.com\PDFcompress\cmdLine\CLcompr.exe"
arguments="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# -co -ci jpg -cq 10 -gi jpg -gq 10 -mi jbig2 -mq 1"
outputfile="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\output.txt"
timeout="250">
</cfexecute>
<!---Now Lets Return the file back to its original folder--->
<cffile action="move"
source="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf" >
</cfloop>
</cffunction>
When it comes to the point of running the first cffile command, I get an error message that C:.... C:.... is not a valid source and what it looks like its doing is specifying multiple files at once rather than grab them one at a time until done grabbing all files referenced in the query. How do I fix this?
Update: Code is not successfully looping through the query results.
1. Tested the code using filename="document1.pdf" and it worked without issue passing that into the FindFilePath method and bringing back the file path and executing the remainder of the code. However when its replaced with the filename="qryGetFilesJustUploaded", each of the file names in the query doesn't seem to be getting passed in successfully so the file path of that file can be returned.
Did a dump of the query to verify my query works and the list of files is available.
Also verified the files exist and that there is no odd formatting in the file names.
You can try putting sleep(5000) after each file operation. This is because file operation take some time to complete. And it may be possible that while the first file operation is going on, the next file operation started. And thus in some cases you might encounter such kind of issues. Try this.
<cfloop query="#qryGetFilesJustUploaded#">
<cffile action="move" source="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin" >
<cfset sleep(5000)>
<!--- Now lets compress it--->
<cfexecute name="C:\Program Files (x86)\neeviaPDF.com\PDFcompress\cmdLine\CLcompr.exe"
arguments="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# -co -ci jpg -cq 10 -gi jpg -gq 10 -mi jbig2 -mq 1"
outputfile="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\output.txt"
timeout="250">
</cfexecute>
<cfset sleep(5000)>
<!---Now Lets Return the file back to its original folder--->
<cffile action="move"
source="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf" >
<cfset sleep(5000)>
</cfloop>
Resolved
When I set my query to the filename, I forgot to set it to "queryname.columnname" syntax.
Working Code:
<cffunction name="testFunc" access="public">
<!--Lets Get the file names from the DB table--->
<cfquery name="qryGetFilesJustUploaded" datasource="#request.dsn#">
SELECT fileupload
FROM [First_Title_Services_Dev].[dbo].[upload_many]
WHERE (fileupload Like '%.pdf%') and (needs_compression = '1')
</cfquery>
<cfset qryRecordCount = #qryGetFilesJustUploaded.RecordCount# >
<!--Set the methods and variables--->
<cfset fileSys = CreateObject('component','cfc.FileManagement')>
<cfloop query="qryGetFilesJustUploaded" startRow="1" endRow="#qryRecordCount#">
<cfset filename="#qryGetFilesJustUploaded.fileupload#" >
<cfset filePath = #fileSys.FindFilePath('#filename#','file')# >
<cffile action="move" source="#filePath#" destination="C:\compressionBin">
<cfset sleep(5000)>
<!---Compress the file now--->
<cfexecute name="C:\Program Files (x86)\neeviaPDF.com\PDFcompress\cmdLine\CLcompr.exe"
arguments="C:\compressionBin\#filename# C:\compressionBin\#filename# -co -ci jpg -cq 10 -gi jpg -gq 10 -mi jbig2 -mq 1"
outputfile="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\output.txt"
timeout="250">
</cfexecute>
<cfset sleep(5000)>
<!---Lets Return the file now--->
<!---Back to where you came from! :D--->
<cffile action="move"
source="C:\compressionBin\#filename#" destination="#filePath#" >
<cfset sleep(5000)>
</cfloop>
</cffunction>
Related
My code takes an uploaded file, resizes it and attempts to save it to another file, from a temp folder to destination folder. I've tried all variations of sample code found online. When the cfimage tag goes to save the resized image, the file disappears from the original location. Then this error is thrown:
d:\temp\imageUploads\HeadshotsampleRDB.jpg (The system cannot find the file specified)
If I change the destination to the final location, the original file reappears!
<cffile
accept="image/gif, image/jpeg, image/jpg, image/pjpeg, image/png, image/xpng, image/x-png"
action="upload"
nameconflict="makeunique"
filefield="newphoto"
destination="d:\temp\imageUploads">
<cfset destFileName = session.Lastname & "_" & CreateUUID() & "." & cffile.serverFileExt>
<cfimage action="info" source="d:\temp\imageUploads\#cffile.serverfile#" structname="info">
<cfif info.width GT 100 OR info.height GT 150>
<cfif info.width GT 100>
<cfset lawyerImage=ImageNew("d:\temp\imageUploads\#cffile.serverfile#")>
<cfset ImageResize(lawyerImage,"100","","highestPerformance",1)>
<cfimage action="write" source="#lawyerImage#" destination="d:\temp\imageUploads\#cffile.serverfile#" overwrite="yes">
<cfelse>
<cfset lawyerImage=ImageNew("d:\temp\imageUploads\#cffile.serverfile#")>
<cfset ImageResize(lawyerImage,"","150","highestPerformance",1)>
<cfimage action="write" source="#lawyerImage#" destination="d:\temp\imageUploads\#cffile.serverfile#" overwrite="yes">
</cfif>
</cfif>
<cffile action="rename"
source="d:\temp\imageUploads\#cffile.serverfile#"
destination="#imgsPath#\photos\#destFileName#">
I could swear I have hot fix 4 installed, I see the file, but the CF Admin only shows us at hot fix level 2.
I have a list of folders from a directory. All of the folders in this directory contain pdf files. There are about 20 folders. Is it possible to get the names of the pdf files, in all folders, without having to go through each directory (folder)?
<cfdirectory action="list"
directory="C:\wwwroot\WebServer\testing\uploads\all_folders"
recurse="false"
name="myList">
<cfdump var=#myList#>
Try using filter="*.pdf" attribute and type="file".
<cfdirectory
directory="C:\wwwroot\WebServer\testing\uploads\all_folders"
action="list"
recurse="yes"
type="file"
filter="*.pdf"
name="myList"
>
<cfdump var=#myList#>
I am trying to get a file from a auto download url using cfhttp.
I am using the following code:
<cfhttp method="get" url="http://www.example.com/getfile" path="E:/" file="abc.csv">
In this case I have specified the file type as CSV so I am able to get the file but file type can change.
I tried CFHTTP.MIMETYPE to get the file type and use like this:
<cfhttp method="get" url="http://www.example.com/getfile">
<cffile action="write" file="E:/abc.#listLast(cfhttp.MIMETYPE,'/')#" output="#cfhttp.FileContent#">
And this is working for CSV and XML files. But I want it to work to Excel files also.
Please help.
Thanks in advance.
<cfhttp method="get" url="http://www.example.com/getfile">
<cfset fileName = listlast(cfhttp["responseHeader"]["content-disposition"],";=")>
<cffile action="write" file="E:/abc.#fileName#" output="#cfhttp.FileContent#">
As "Regular Jo" mentioned, you need to add getAsBinary="Auto" to the cfhttp to get this working. Thank you to Deepak Kumar Padhy for the inital answer that pointed me in the right direction.
<cfhttp method="get" url="http://www.example.com/getfile" getAsBinary="Auto" result="cfhttp">
<cfdump var="#cfhttp#">
<!--- Returns the file name with double quotes, ex. '"Users.zip"' --->
<cfset fileNameWithQuotes = listlast(cfhttp["responseHeader"]["content-disposition"],";=")>
<!--- Remove the '"' in the file name so it is Users.zip --->
<cfset fileName = REPLACENOCASE(fileNameWithQuotes,'"','', 'ALL')>
<!--- Write the zip to the server location --->
<cffile action="write" file="E:/abc/#fileName#" output="#cfhttp.FileContent#">
<!--- if zip file, unzip the file to get the csv --->
<cfzip file="E:/abc/#fileName#" action="unzip" filter="*.csv" destination="E:/abc/" />
How to download all files from a folder in ColdFusion? I have a folder named "abc" and there are 5 different files in it. So I want to download all 5 files when I click on a hyperlink.
Currently, this is the code I am trying. It is downloading a selected file, but how do I download all files in it?
<cfheader name="Content-disposition" value='attachment; filename="database_error.png"'>
Check this code
<cfset fileName = createUUID() />
<cfif DirectoryExists("D:/tempDir")>
<cfdirectory action = "list" directory = "D:/tempDir" name = "getAllFiles" type="file" />
<cfzip action="zip" file="D:/zipfilefolder/#fileName#.zip" overwrite="yes" >
<cfloop query="getAllFiles">
<cfzipparam source="D:/tempDir/#getAllFiles.name#">
</cfloop>
</cfzip>
</cfif>
<cfheader name = "Content-disposition" value = 'attachment; filename="D:/zipfilefolder/#fileName#.zip"'>
<cfcontent deleteFile="true" file="D:/zipfilefolder/#fileName#.zip" type="application/x-zip-compressed" >
I have a query on a directory in Coldfusion 9. Why does this code run on mac giving the expected result but not on windows?
<cfoutput>
<cfset fileLocation = "d:/tmp"> <!--- On mac set to /tmp --->
<cfdirectory
action = "list"
directory = "#fileLocation#"
name = "files"
>
Files found #files.recordcount# <br/>
<cfquery name="dir" dbtype="query">
select *
from files
where directory = <cfqueryparam value = "#fileLocation#">
</cfquery>
Query gives #dir.recordcount#
</cfoutput>
Both machines run Coldfusion 10 and the directory exists. The output on the mac is
Files found 5
Query gives 5
and on windows
Files found 5
Query gives 0
Am I missing the obvious?
Ah my guess is windows reports the directory name as D:\tmp instead of the other slash: D:/tmp. Which is why your directory = #filelocation# isn't returning any matches. You might be able to make this more platform independent as described in this article.
I could have sworn there was a built-in function for this in CF now, but I can't seem to find it.