将一个目录中的大量文件移动到多个目录 [英] Moving a large number of files in one directory to multiple directories

查看:217
本文介绍了将一个目录中的大量文件移动到多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个Windows批处理脚本来移动约2,000个文件并将其拆分,以便每个文件夹有10个文件.我曾尝试创建批处理脚本,但是语法确实让我感到困惑.这是我到目前为止所拥有的

I am looking to create a Windows batch script to move about 2,000 files and splitting them up so that there are 10 files per folder. I have attempted creating a batch script but the syntax really boggles my mind. Here is what I have so far

@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变量的地方遇到问题吗?我敢肯定还有其他错误,因为即使使用pause,CMD窗口也会关闭.感谢您的帮助或指导!

Basically what this script does is loop through each .txt file in the directory. It creates a new directory in the beginning and moves 10 files into that directory, then creates a new folder again and moves another 10 files into that directory, and so on. However, I'm having problems where the n variable is not being incremented in the loop? I'm sure there's other errors too since the CMD window closes on me even with pause. Any help or guidance is appreciated, thanks for your time!

推荐答案

您需要了解的几件事:

    需要
  • SETLOCAL ENABLEDELAYEDEXPANSION,因为您要更改变量并在单个括号内使用更改后的值.命令行上的SET /?将提供一些信息.在互联网上搜索该词,您会找到更好的解释.
  • 我将!nf!格式用于变量的位置与延迟扩展有关.
  • 正如ghostdog74所述,您并没有增加%nf%.
  • 我将nf初始化为0而不是1.这样,您要将文件移动到的文件夹号与您刚创建的文件夹号相同.在您的代码中,创建文件夹X,然后递增X,然后尝试将文件移动到X + 1.
  • 您必须使用MOVE来移动文件,MV无效.
  • SETLOCAL ENABLEDELAYEDEXPANSION is needed, since you are changing variables and using their changed values in a single parenthesized block. SET /? on the command line will give some info. Search the internet for this term and you will find a better explanation.
  • The places I use the !nf! format for variables is related to delayed expansion.
  • As ghostdog74 mentioned, you weren't incrementing %nf%.
  • I initialized nf to 0 instead of 1. This way, the folder number you want to move files to is the same as the folder number you just created. In your code, you create folderX, then increment X, and then try to move the file to X+1.
  • You have to use MOVE to move a file, MV is not valid.

此批处理文件有效...但是请确保您进行测试!我只测试了少量文件.

This batch file works...but make sure you test! I only tested on a small amount of files.

@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

这篇关于将一个目录中的大量文件移动到多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆