批量将文件夹中的文件分发到某些文件夹中

时间:2016-11-29 05:38:31

标签: batch-file

我有一个名为m_all的源文件夹,目标文件夹名为m_1, m_2, m_3... m_8

我想将源文件夹中的文件复制到目标文件夹中,

以这样的方式将2个文件改为m_1,将3个文件改为m_2,将2个文件改为m_3,将3个文件改为m_4 ..

这些目标文件中没有模式。

我该怎么做?

3 个答案:

答案 0 :(得分:0)

这是一个基本的例子,(我确信那里有一种更聪明的方式)。它应该使用您提供的模式复制文件,直到没有复制文件为止。 (这意味着,例如,最后一个文件可能是唯一复制到m_3的文件)

@Echo Off

(Set SrcDir=C:\Users\Cointreau\Documents\m_all)

If /I Not "%CD%"=="%SrcDir%" PushD "%SrcDir%" 2>Nul||Exit/B 
If "%~f0" Equ "%SrcDir%" Exit/B
For /L %%A In (1,1,8) Do If Not Exist "m_%%A" Exit/B
SetLocal EnableDelayedExpansion
Set "i=0"
For /F "Delims=" %%A In ('DIR/B/A-D-S') Do (Set/A "i+=1"
    If !i! LEq 2 (Copy "%%A" "m_1") Else (If !i! LEq 5 (Copy "%%A" "m_2") Else (
            If !i! LEq 7 (Copy "%%A" "m_3") Else (If !i! LEq 10 (
                    Copy "%%A" "m_4") Else (If !i! LEq 12 (Copy "%%A" "m_5"
                        ) Else (If !i! LEq 15 (Copy "%%A" "m_6") Else (
                            If !i! LEq 17 (Copy "%%A" "m_7") Else (
                                If !i! LEq 20 (Copy "%%A" "m_8"
                                    If !i! Equ 20 Set i=0)))))))))

没有包含代码来满足复制现有文件的要求,即覆盖。

更改第3行上括号之间的位置以适合您的实际源目录路径。

答案 1 :(得分:0)

这将在文件夹之间均匀分配文件,如果还有剩余文件,它们将被复制到最后一个文件夹

将在TK中收到输入文件夹。文件夹数量放在%1变量中。文件夹名称(%totalfolder% ... m_1)将分为两部分:m_8变量中的前缀(m_)和{{1}中计算的文件夹编号1}}变量

%folderprefix%

该脚本基本上维护每个文件夹中的文件数,如果文件夹完全填满,它将移动到下一个文件夹

答案 2 :(得分:0)

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem Configure source folder
    set "source=m_all"

    rem Initialize targets array
    set "targets=0"
    for %%a in ( 
        "m_1" "m_2" "m_3" "m_4" "m_5" "m_6" "m_7" "m_8" 
    ) do (
        set /a "targets+=1"
        setlocal enabledelayedexpansion 
        for %%b in ("!targets!") do endlocal & set "target.%%~b=%%~a"
    )

    rem Determine the number of files to copy
    for /f %%a in ('
        dir /b /a-d "%source%\*" ^| find /c /v ""
    ') do set "nFiles=%%a"

    rem Initialize progress variables
    set /a "copied=0", "toCopy=0", "target=1", "processedFiles=0"

    rem For each file in source
    for %%a in ("%source%\*") do (
        rem Enable delayed expansion on start of first loop
        if not "!!"=="" setlocal enabledelayedexpansion

        rem One more file is being processed
        set /a "processedFiles+=1"

        rem Determine if we need to reevaluate where to place the file
        if !toCopy! lss 1 (
            if !processedFiles! gtr !nFiles! (
                rem More files have been added to the folder, deal with it
                set /a "target=(processedFiles %% targets) + 1", "toCopy=1"

            ) else for /l %%t in (!target! 1 %targets%) do if !toCopy!==0 (
                rem We are still handling the expected set of files
                set /a "target=%%t", "toCopy=nFiles*target/targets-copied", "copied+=toCopy"
            )
        )

        rem Prepare variables to be accessed without delayed expansion
        for %%b in ("!target!") do for %%t in ("!target.%%~b!") do (
            setlocal disabledelayedexpansion
                rem DEBUG CODE - Remove echo prefix if the output is correct
                echo copy "%%~fa" "%%~ft"
            endlocal
        )

        rem One file processed in current target
        set /a "toCopy-=1"
    )