将特定数量的文件移动到新创建的编号文件夹中



我目前在一个文件夹中有大约550个文件,它们的格式(.csv(和标题相同(都以字母"YL"开头(。

我想知道是否有办法将这些文件(一次50个文件((顺序无关紧要(拆分到编号的文件夹中?(例如1、2、3、4、5(还为剩余文件创建一个后续文件夹?

我找到了这个脚本,并试图将其修改为50个文件,但看起来它只创建了第一个文件夹(subdr1(

@echo off
set /a counter=1
set /a filesperfolder=50
cd dirdir_main
:loopstart
set dirname=subdir%counter%
md %dirname%
echo %dirname%
dir /b | findstr /v /i "subdir*"> %temp%temp.txt && for /l %%l in (1,1,%filesperfolder%) do @for /f "tokens=1,2* delims=:" %%a in ('findstr /n /r "^" %temp%temp.txt ^| findstr /r "^%%l:"') do @move %%b %dirname%%%b >nul
set /a counter=%counter%+1
for /f "tokens=*" %%a in ('type %temp%temp.txt ^| find /c /v ""') do set _filesmoved=%%a
del %temp%temp.txt
IF %_filesmoved% LSS 50 goto done
goto loopstart
:done
cls
echo All files were moved!!
pause
exit

我不喜欢您发现的脚本,因为它很难阅读,并使用临时文件来跟踪文件列表。(而且,它显然不起作用,所以就是这样。(

@echo off
SET /a cnt=50
SET /a fnum=0
FOR /F "delims=" %%f IN ('dir /b /a-d *.csv') DO (
CALL :moveFile "%%f"
)
GOTO :end
:moveFile
IF "%cnt%" equ "50" CALL :makeDir
move "%~1" "%fnum%%~1"
SET /a cnt+=1
GOTO :EOF
:makeDir
SET /a fnum+=1
mkdir %fnum%
SET /a cnt=0
GOTO :EOF
:end

这里有另一种方法。我们测试目录中是否还有文件,如果有,创建一个新目录并复制50个文件。

@echo off & setlocal enabledelayedexpansion
set fold_cnt=1
:test
set file_cnt=50
dir /a-d YL*.csv | findstr /IRC:"File(s)"
if %errorlevel% equ 0 (
mkdir !fold_cnt!
) else (
goto :eof
)
for %%i in (YL*.csv) do (
if not !file_cnt! equ 0 (
set /a file_cnt-=1
move /Y "%%i" "!fold_cnt!%%i"
)
)
set /a fold_cnt+=1
goto test

最新更新