0

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>
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • What's the part causing you difficulty - copying files or doing the threesies? – Dan Bracuk Apr 07 '13 at 16:35
  • @Dan Bracuk The part that causing me problem is copy. I have changed the the above process using cfc. After folders creation all the files (20) are copied in the first folder. For the cfm follow [ http://pastebin.com/ugrjPJ7W ] and for the cfc follow [ http://pastebin.com/yXinvtvr ] –  Apr 07 '13 at 20:10
  • I dont have the exact syntac right now, but inside your folders to create loop, could you make a 2nd loop where you copy the files with filename file[1].filename, file[2].filename, etc... I could post a sample code tomorrow – steve Apr 08 '13 at 02:53

1 Answers1

0

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

Note

  • The logic is just a POC. You can refine it to a better one.
  • I have tried this code on CF10. It must work Okay on Open DB too.

    <cfprocessingdirective pageencoding="UTF-8">
    
    <cfscript>
        oDirectoryPath      = "D:/test_cf/"; //original Directory path
        files               = DirectoryList(oDirectoryPath, false, 'name', '*.*');
        filecount           = Arraylen(files);
        filePerFolder       = 3;
        numfoldersToCreate  = Ceiling(filecount/filePerFolder);
    
        function moveFilesToThisDirectory(dir,index){
            var beginAt = 1;
            var endAt   = begin + 2;
            var newDir  = ARGUMENTS.dir;
    
            beginAt = ARGUMENTS.index;
    
            if(beginAt neq 1){
                endAt = index * VARIABLES.filePerFolder;
                beginAt = endAt - (VARIABLES.filePerFolder - 1);
                if(endAt GT VARIABLES.fileCount){
                    endAt = VARIABLES.fileCount;
                }
            }
    
            while(beginAt LTE endAt){
                FileMove(oDirectoryPath & '\' & files[beginAt], newDir & '\' & files[beginAt]);
                beginAt = beginAt + 1;
            }
    
        }
    </cfscript>
    
    <cfif directoryExists(oDirectoryPath)>        
            <cfdirectory action="list" directory="#oDirectoryPath#" name="directories" recurse="true" type="dir" />
            <cfloop query="directories">
                    <cfdirectory action="delete" directory="#oDirectoryPath#/#directories.name#" recurse="yes">
            </cfloop>
    </cfif>
    
    <cfscript>
    
        i = 1;
        while(i LTE numfoldersToCreate){
            newDirectoryName = "#oDirectoryPath#newfolder#i#";
            DirectoryCreate(newDirectoryName);
            moveFilesToThisDirectory(dir:newDirectoryName,index:i );
            i = i + 1;
        }
    
        switch(numfoldersToCreate){
            case 0:
                WriteOutput(' No folder created');
                break;
            case 1:
                WriteOutput(' 1 folder created');
                break;
            default:
                WriteOutput(numfoldersToCreate & ' folders created');
        }
    </cfscript>
    

Sanjeev
  • 1,838
  • 1
  • 16
  • 28
  • Many thanks for looking into my problem. I what to ask if I can find a good book in array manipulation techniques (or a tutorial) –  Apr 08 '13 at 17:02
  • Just a minor suggestion. You may want to use different variable names to avoid confusion, as `begin` and `end` typically have special meaning in most languages. – Leigh Apr 08 '13 at 19:16
  • @Leigh good suggestion. I shall modify the code so that further search references will be neat. – Sanjeev Apr 09 '13 at 02:20