2

I moved some files from one directory to another in my server. I would like to zip the destination folder after moving the files.

<cfoutput>
    <cfset destination = expandPath("./TenantFiles/tempEmail/11/") />
    <cfif not directoryExists(destination)>
        <cfdirectory action="create" directory="#destination#">
    <cfelse>
        <cfdirectory action="delete" directory="#destination#" recurse="true">
        <cfdirectory action="create" directory="#destination#">
    </cfif>
    <cfloop query="myQuery">
        <cfset sourcefile = expandPath("./TenantFiles/11/#myQuery.TenantID#/#myQuery.DocumentName#") />
        <cfif FileExists(sourcefile)>
            <cfscript>
                FileMove(#sourcefile#, #destination#);
            </cfscript>
        </cfif>
    </cfloop>
    <cfzip action="zip" file="#destination#\ZipFile.zip" source="#destination#" filter="*.pdf" /> 
</cfoutput>

It is returning the error:

 Can not create a zip file with no entries.
Make sure that there is at least one entry in the zip file. 

However the #destination# folder has a lot of pdf files.

Thanks

myTest532 myTest532
  • 2,091
  • 3
  • 35
  • 78

1 Answers1

-1

Instead of going back and forth on possible issues I created a function that can be used to test if there is something else going on in your environment. I eliminated movement of files and other possible sources of problems.

Try just setting up a test folder matching this example and see if that works before using your real query and files. I set it to not store path of files in zip as that was the result of your file movement and adding but it may not be what you want in the end.

<cfset myQuery = queryNew("") />
<cfset TenantIDs = [1,2,3,4] />
<cfset DocumentNames = ['one.pdf','two.pdf','three.txt','four.doc'] />
<cfset queryAddColumn(myQuery, 'TenantID', "integer", TenantIDs) />
<cfset queryAddColumn(myQuery, 'DocumentName', "varchar", DocumentNames) />

<cfset zipPdfFiles(expandPath('./testfiles'), myQuery, "TenantID", "DocumentName", expandPath('./testfiles/zipFile.zip'), "pdf") />

<cffunction name="zipPdfFiles" output="true">
    <cfargument name="baseFilePath" required="true" />
    <cfargument name="fileQuery" required="true" />
    <cfargument name="folderColumn" required="true" />
    <cfargument name="fileNameColumn" required="true" />
    <cfargument name="zipFilePath" required="true" />
    <cfargument name="allowExtensions" default="" hint="comma separated list of extensions or blank for all" />

    <cfzip action="zip" file="#arguments.zipFilePath#" storePath="false">
        <cfloop query="arguments.fileQuery">
            <cfif not len(arguments.allowExtensions) or listFindNoCase(arguments.allowExtensions, listLast(myQuery.DocumentName, "."))>
                <cfzipparam source="#arguments.baseFilePath#/#arguments.fileQuery[arguments.folderColumn][currentRow]#/#arguments.fileQuery[arguments.fileNameColumn][currentRow]#" />
            </cfif>
        </cfloop>
    </cfzip>
</cffunction>
Dan Roberts
  • 4,664
  • 3
  • 34
  • 43
  • 1
    I think the way you did, you are creating the .zip file and adding each document to the zip file using cfzipparam. My question is how can I create a .zip file from an folder (not looping through the folder's files) – myTest532 myTest532 Oct 11 '19 at 14:38
  • @myTest532myTest532 on the surface your approach is fine but obviously something is causing a problem. There are a few way to go to debug... 1. output everything you can along the way so you can verify all paths 2. break up the code into steps that can be verified before next step is run 3. use simplified or known good examples of code. Have you done a simple test cfm file with just cfzip line to test compressing an existing simple folder of files? – Dan Roberts Oct 11 '19 at 15:24