file-iocoldfusioncfmlopenbd

Copy files in folders using ColdFusion (openBD Engine)


I have been asked to solve the following problem: copy files in batch of 3 in newly created folders. Example: in folder let’s say “D:/TEST_CF/” there are 20 txt files. Divide them by 3 (the batch-give us 6 remainder 2), create 7 folders (in order to place the batches of 3 in the 6 folders and the remaining 2 files in the 7th folder). What I have done until now is to create the needed folders. But, I can’t copy the files into batches of 3 in the newly created folders. How should I tackle the above problem?

The code block

<html>
<body>

<cfprocessingdirective pageencoding="UTF-8">

<cfset directory = "D:/TEST_CF/">
<cfdirectory directory="#directory#" name="files" action="list" type="file">
<cfset filecount = #files.RecordCount#>
<cfset divisor = 3>
<cfset division = #filecount# / #divisor#>
<cfset remainder = #filecount# MOD #divisor#>
<cfset folders_to_create = Ceiling(#division#)>

<cfoutput>

    <cfif directoryExists(directory)>
        <cfdirectory action="list" directory="#directory#" name="directories" recurse="true" type="dir" />
        <cfloop query="directories">
            <cfdirectory action="delete" directory="#directory#/#directories.name#" recurse="yes">
        </cfloop>
    </cfif>

    <cfloop from="1" to="#folders_to_create#" index="i">
        <cfdirectory
          action="create"
          directory="#directory#newfolder#i#">
    </cfloop>

    <cfif folders_to_create EQ 1>
        #folders_to_create#&nbsp;folder created
    <cfelse>
        #folders_to_create#&nbsp;folders created
    </cfif>

</cfoutput>

</body>
</html>

Solution

  • As Steve said, you need a loop two or better a function to do the fileMove action.

    Note