【问题标题】:Looping through cfquery for file move is failing循环通过cfquery进行文件移动失败
【发布时间】:2015-06-23 17:54:34
【问题描述】:

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

我正在运行一个查询并使用<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. 还验证了文件存在并且文件名中没有奇怪的格式。

【问题讨论】:

  • 第 1 步 - 查看您的数据。具体来说,转储 qryGetFilesJustUploaded。
  • 你可以使用expandpath()函数来设置源和目标吗?

标签: coldfusion coldfusion-9 cfloop cffile


【解决方案1】:

您可以尝试在每次文件操作后加上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>

【讨论】:

    【解决方案2】:

    已解决

    当我将查询设置为文件名时,我忘记将其设置为“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>
    

    【讨论】:

    • 嗯..在哪里? OP 中的所有查询引用都使用查询和列名,即qryGetFilesJustUploaded.fileupload
    • 当我设置变量 'filename=#qryGetFilesJustUploaded.fileupload#' 时,它起作用了。老实说,这个 CF 系统并不传统。为了让事情顺利进行,我不得不求助于一些非常规的方法。它是通过不那么适当的方式被黑客攻击在一起的。这就是使用它并向其添加代码如此痛苦的原因。当我在本地测试它们时,它们可以工作,但在它们被破解的环境中,它们经常会失败。
    • 这与您上面描述的不同。虽然它仍然是does not make any difference。结果与直接使用qryGetFilesJustUploaded.fileupload 相同,如在原始代码中。所以问题是别的。老实说,这听起来像是一个本地化问题,要么无法重现,要么不太可能在您的环境之外发生。所以你可能应该删除线程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 2012-10-29
    • 1970-01-01
    相关资源
    最近更新 更多