0

如何遍历查询并一次传递一个结果而不是所有结果?

我正在运行查询并使用<cfloop>循环遍历结果,对于查询中的每个文件名,<cffile>需要将该文件从一个文件夹移动到下一个文件夹,如下所示:

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>

当谈到运行第一个 cffile 命令时,我收到一条错误消息,指出 C:.... C:.... 不是有效的源,看起来它所做的是一次指定多个文件而不是而不是一次抓取一个,直到完成抓取查询中引用的所有文件。我该如何解决?

更新:代码未成功循环查询结果。1. 使用 filename="document1.pdf" 测试代码,将其传递给 FindFilePath 方法并带回文件路径并执行其余代码没有问题。但是,当将其替换为 filename="qryGetFilesJustUploaded" 时,查询中的每个文件名似乎都没有成功传入,因此可以返回该文件的文件路径。

  1. 是否转储了查询以验证我的查询是否有效并且文件列表可用。
  2. 还验证了文件存在并且文件名中没有奇怪的格式。
4

2 回答 2

2

您可以尝试sleep(5000)在每个文件操作之后放置。这是因为文件操作需要一些时间才能完成。并且可能在第一个文件操作正在进行时,下一个文件操作开始了。因此,在某些情况下,您可能会遇到此类问题。尝试这个。

<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>
于 2015-06-24T06:35:01.017 回答
-1

解决

当我将查询设置为文件名时,我忘记将其设置为“queryname.columnname”语法。

工作代码:

<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>
于 2015-06-24T16:09:15.457 回答