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.
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
This code itself worked fine just as it was designed, but an additional problem was found: speed.
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. 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
Comments
- I used
set /a
to adjust%limit%
that are off due tofindstr /n /v
- This script will be compiled to .bat file and will be put into a folder containing files to sort.
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.
Script requirements:
- Create numbered directory starting from 0, only if the directory doesn't exist
- Move 700 files to newly created directory. The files will be moved even if the number of files is less than 700.
- Repeat task 1 and 2 until there are no remaining files left in the parent directory.
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.