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# folder created
<cfelse>
#folders_to_create# folders created
</cfif>
</cfoutput>
</body>
</html>
As Steve said, you need a loop two or better a function to do the fileMove action.
Note
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>