Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 将一个目录中的大量文件移动到多个目录_Batch File - Fatal编程技术网

Batch file 将一个目录中的大量文件移动到多个目录

Batch file 将一个目录中的大量文件移动到多个目录,batch-file,Batch File,我希望创建一个Windows批处理脚本来移动大约2000个文件,并将它们拆分,以便每个文件夹有10个文件。我曾尝试创建一个批处理脚本,但语法真的让我难以理解。这是我到目前为止所拥有的 @echo off :: Config parameters set /a groupsize = 10 :: initial counter, everytime counter is 1, we create new folder set /a n = 1 :: folder counter set /a n

我希望创建一个Windows批处理脚本来移动大约2000个文件,并将它们拆分,以便每个文件夹有10个文件。我曾尝试创建一个批处理脚本,但语法真的让我难以理解。这是我到目前为止所拥有的

@echo off

:: Config parameters
set /a groupsize = 10
:: initial counter, everytime counter is 1, we create new folder
set /a n = 1
:: folder counter
set /a nf = 1

for %%f in (*.txt) do (
:: if counter is 1, create new folder
if %n% == 1 (
    md folder%nf%
    set /a n += 1
)

:: move file into folder
mv -Y %%f folder%nf%\%%f

:: reset counter if larger than group size
if %n% == %groupsize% (
    set /a n = 1
) else (
    set /a n += 1
)
)
pause

基本上,这个脚本所做的是循环遍历目录中的每个.txt文件。它首先创建一个新目录并将10个文件移动到该目录中,然后再次创建一个新文件夹并将另外10个文件移动到该目录中,依此类推。但是,我遇到了
n
变量在循环中没有递增的问题?我确信还有其他错误,因为即使使用
暂停
,CMD窗口也会关闭。感谢您的帮助和指导,谢谢您的时间

您没有增加
%nf%

您需要知道的一些事情:

  • SETLOCAL ENABLEDELAYEDEXPANSION
    是必需的,因为您正在更改变量,并在一个括号内的块中使用其更改的值<命令行上的代码>设置/?将提供一些信息。在互联网上搜索这个词,你会找到更好的解释
  • 我使用
    的地方!nf变量的格式与延迟扩展相关
  • 正如ghostdog74所提到的,您没有增加
    %nf%
  • 我将
    nf
    初始化为0,而不是1。这样,您要将文件移动到的文件夹编号与刚才创建的文件夹编号相同。在代码中,创建folderX,然后递增X,然后尝试将文件移动到X+1
  • 您必须使用
    MOVE
    来移动文件,
    MV
    无效
此批处理文件有效…但请确保您进行了测试!我只测试了少量的文件

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

:: Config parameters
SET groupsize=10
:: initial counter, everytime counter is 1, we create new folder
SET n=1
:: folder counter
SET nf=0

FOR %%f IN (*.txt) DO (
  :: if counter is 1, create new folder
  IF !n!==1 (
    SET /A nf+=1
    MD folder!nf!
  )

  :: move file into folder
  MOVE /Y "%%f" folder!nf!

  :: reset counter if larger than group size
  IF !n!==!groupsize! (
    SET n=1
  ) ELSE (
    SET /A n+=1
  )
)

ENDLOCAL

PAUSE
这是一个批处理文件(此处称为“x.bat”),其中包含指向要将文件放入其中的文件夹的链接:

mkdir "FilesToHere\1"
mkdir "FilesToHere\1\Example 1"
mkdir "FilesToHere\1\2"
mkdir "FilesToHere\1\2\Example2"
mkdir "FilesToHere\1\2\Example2\3"
mkdir "FilesToHere\1\2\Example2\4"
mkdir "FilesToHere\1\2\Example2\3\Example3"
mkdir "FilesToHere\1\2\Example2\3\Example 1"
mkdir "FilesToHere\1\2\Example2\3\Example4"
mkdir "FilesToHere\1\2\Example2\3\Example4\5"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example5"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example5\6"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example5\7"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example5\7\Example 1"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example5\7\Example6\1"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example5\7\Example6\1\Example7"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example5\7\Example7"
mkdir "FilesToHere\1\2\Example2\3\Example4\5\Example 1"
mkdir "FilesToHere\1\2\Example2\3\Example8"
mkdir "FilesToHere\1\Example9"
2000个文件/20个文件夹=100个文件/文件夹。(您可以根据自己的文件/文件夹首选项更改以下数字。“[…]”表示,例如,在表示为“1,2,3,[…]20”的列表中缺少16个列表项。目录“s”包含2000个文件。“FilesToHere”位于“C:\user\Documents\ex1\ex2\”。您可以通过以下方式分发文件:

  • 打开“cmd.exe”并运行
  • 运行“C:\user\Documents\FI\le\t.bat”,其中包含
  • 使用在gvim中打开的文件“C:\user\Documents\FI\le\x.bat”运行以下操作(如果一个或多个文件名包含“```”,请使用)
  • 运行“C:\user\Documents\FI\le\x.bat”,其中包含
  • 运行“C:\user\Documents\FI\le\x.bat”,将其更改为包含
  • 注意:如果您得到错误“'■t'不被识别为内部或外部命令、
    可操作程序或批处理文件。”(就像我在Windows 10上所做的那样),那么您必须将正在运行的任何批处理文件从Unicode转换为ANSI。(另见)

    如果您试图将10000个文件移动到100个文件夹中(10000/100=100个文件/文件夹),并且100个文件夹以不均匀的方式分布(例如,100个文件夹不都在一个目录中,100个文件夹彼此之间具有父子关系),我发现此方法非常有用。相关链接/文本:

    • -如何使用CMD将前N个文件从当前目录移动到另一个目录

    @Brian:给我看一个安装了Python或Perl的Windows版本。。。其他语言也可以,但是仍然使用批处理文件有很多正当的理由。此外,上面的代码并没有那么复杂。我看过(也写过)更糟的。谢谢编辑约翰。当我说ENABLEDELAYEDEXANSION是数学工作的必要条件时,我过于简单化了。那不是真的。但是,您确实需要它来访问更改后的值。谢谢,在我接受此答案之前,我明天将查看此内容:)工作正常。谢谢你,阿佛里亚
    cd C:\user\Documents\FI\le\s
    dir/b>../temp.txt
    
    cd C:\user\Documents\FI\le\s
    powershell -command "& {Get-Content ../temp.txt -TotalCount 100}" > ../temp1.bat
    gvim -c "execute \"normal! 100dd\"" -c "wq" ../temp.txt
    powershell -command "& {Get-Content ../temp.txt -TotalCount 100}" > ../temp2.bat
    gvim -c "execute \"normal! 100dd\"" -c "wq" ../temp.txt
    powershell -command "& {Get-Content ../temp.txt -TotalCount 100}" > ../temp3.bat
    gvim -c "execute \"normal! 100dd\"" -c "wq" ../temp.txt
    [...]
    powershell -command "& {Get-Content ../temp.txt -TotalCount 100}" > ../temp20.bat
    gvim -c "execute \"normal! 100dd\"" -c "wq" ../temp.txt
    
    :%s/\\/\\\\/ge | %s/^mkdir "FilesToHere/gvim -c "%%s\/^\/move \\"\/g" -c "%%s\/$\/\\" \\"C:\\\\user\\\\Documents\\\\ex1\\\\ex 2\\\\FilesToHere/g | %s/"$/\\\\\\"\/g" -c "wq" ..\/temp```.bat/g | let i=1|g/```/s//\=i/|let i=i+1
    
    cd C:\user\Documents\FI\le\s
    gvim -c "%%s/^/move \"/g" -c "%%s/$/\" \"C:\\user\\Documents\\ex1\\ex 2\\FilesToHere\\1\\\"/g" -c "wq" ../temp1.bat
    gvim -c "%%s/^/move \"/g" -c "%%s/$/\" \"C:\\user\\Documents\\ex1\\ex 2\\FilesToHere\\1\\Example 1\\\"/g" -c "wq" ../temp2.bat
    gvim -c "%%s/^/move \"/g" -c "%%s/$/\" \"C:\\user\\Documents\\ex1\\ex 2\\FilesToHere\\1\\2\\\"/g" -c "wq" ../temp3.bat
    [...]
    gvim -c "%%s/^/move \"/g" -c "%%s/$/\" \"C:\\user\\Documents\\ex1\\ex 2\\FilesToHere\\1\\Example9\\\"/g" -c "wq" ../temp20.bat
    
    cd C:\user\Documents\FI\le\s\
    call ..\temp1.bat
    [...]
    call ..\temp19.bat
    call ..\temp20.bat