快速方法在批处理文件中复制(移动)文件 [英] Fast methods to copy(move) files in batch file

查看:227
本文介绍了快速方法在批处理文件中复制(移动)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录中有大量的文件,我需要验证。

I have a massive number of files in one directory that I need to validate.

问题是,文件浏览器需要太多时间加载文件列表

The problem was, the file explorer takes too much time to load the file list and my whole computer becomes slow.

所以我写了以下代码通过移动一定数量的文件来分组文件(显示为%limit% / code>,将为700)到编号文件夹(显示为%DirN%

So I wrote the following code to group files by moving certain number of files(shown as %limit% and will be 700) to numbered folders(shown as %DirN%)

for /f "tokens=1-2 delims=:" %%a in ('dir /b /a-d ^|findstr /n /v ".bat .cmd .txt"') do if %%a lss %limit% robocopy "%cd%" "%cd%\%DirN%" "%%b" /mov >nul

这段代码本身工作得很好,就像它被设计的一样,但是发现了一个额外的问题:speed。

This code itself worked fine just as it was designed, but an additional problem was found: speed.

我处理的文件占用了20 GB的磁盘,代码似乎永远移动文件这种方式。

Since I am dealing with the files that are occupying 20 GB of my disk, the code seems to take forever to move files this way.

有更快的方式复制)文件?

Is there any faster way to copy(move) files?

ps。我尝试了 / move / xcopy 命令,但没有看到太多差异。

ps. I've tried /move and /xcopy commands but did not see much differences.

由于有上下文请求,我附上了完整的代码:

Since there was a request for context, I attach full code:

@echo off
pushd %~dp0

set DirN=-1
:Check_DirN
set LeftOver=
for /f "tokens=*" %%a in ('dir /b /a-d ^|findstr /v ".bat .cmd .txt"') do (set LeftOver=%%a)
if "%LeftOver%"=="" goto Done

set /a DirN+=1
if exist "%cd%\%DirN%" goto Check_DirN

:Create
md %DirN%

:Move
cls
echo Moving files to Directory %DirN%...
set /a limit=700+2
for /f "tokens=1-2 delims=:" %%a in ('dir /b /a-d ^|findstr /n /v ".bat .cmd .txt"') do if %%a lss %limit% robocopy "%cd%" "%cd%\%DirN%" "%%b" /mov >nul
goto Check_DirN

exit
:Done
del list.txt>nul 2>&1
echo Task Done!
pause>nul

注释


  1. 我使用 set / a 调整%limit% findstr / n / v

  2. 此脚本将被编译为.bat文件,并将放入包含要排序的文件的文件夹。

  1. I used set /a to adjust %limit% that are off due to findstr /n /v
  2. This script will be compiled to .bat file and will be put into a folder containing files to sort.






示例环境(最小化):



有1,500个文档,在父文件夹中包含名为0,2和4的子文件夹。


Example Environment(minimized):

There are 1,500 documents with subfolders named 0,2 and 4 in a parent folder. The script will be placed inside of the parent folder and be executed.


  1. 只有目录不存在时,才从0开始创建编号目录

  2. 将700个文件移动到新创建目录。

  3. 重复任务1和2,直到在
    中没有剩余的文件,父文件将被移动






脚本执行结果示例:



有一个名为0,1,2,3,4和5的子文件夹,在父文件夹中有一个脚本。
每个子文件夹1和3中将有700个文档。
子文件夹5中将有100个文档。
子文件夹0,2和4中将不会有任何变化。


Example Result of Script Execution:

There are subfolders named 0, 1, 2, 3, 4 and 5 with a script in a parent folder. There will be 700 documents each in subfolder 1 and 3. There will be 100 documents in subfolder 5. The will be no change in subfolders 0, 2 and 4.

推荐答案

我提供这个替代Magoo的答案。我使用你的初始 RoboCopy 命令,并且因为这是一个外部命令,删除对外部 FindStr 的依赖

I am providing this as an alternative to Magoo's answer. I have used your initial RoboCopy command and because that is an external command, removed the dependency on the external FindStr to hopefully take account of any speed difference.

@Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
SetLocal EnableDelayedExpansion
Set "DirN=-1"

:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
Set "limit=700"
For %%A In (*.bat *.cmd *.txt) Do (
    If Not Exist "%DirN%" MD "%DirN%"
    If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
    Set/A "limit-=1"
    If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
Timeout -1 1>Nul

这篇关于快速方法在批处理文件中复制(移动)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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