0

我被要求解决以下问题:在新创建的文件夹中分批复制 3 个文件。示例:在文件夹中假设“D:/TEST_CF/”有 20 个 txt 文件。将它们除以 3(批次给我们 6 余数 2),创建 7 个文件夹(以便将 3 的批次放在 6 个文件夹中,其余 2 个文件放在第 7 个文件夹中)。到目前为止,我所做的是创建所需的文件夹。但是,我无法将文件复制到新创建的文件夹中的 3 批中。我应该如何解决上述问题?

代码块

<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>
4

1 回答 1

0

正如史蒂夫所说,您需要一个循环两个或更好的函数来执行 fileMove 操作。

笔记

  • 逻辑只是一个 POC。您可以将其改进为更好的。
  • 我在CF10上试过这个代码。它也必须在 Open DB 上正常工作。

    <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>
    

于 2013-04-08T04:49:52.923 回答