简体   繁体   English

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

[英]Moving specific number of files into newly created numbered folders

I currently have around 550 files in a folder with the same format (.csv) and same headers (all started with the letters "YL").我目前在一个文件夹中有大约 550 个文件,它们具有相同的格式 (.csv) 和相同的标题(都以字母“YL”开头)。

I wonder if there is a way to splits these files (50 files at a time) (order doesn't matter) into numbered folders?我想知道是否有办法将这些文件(一次 50 个文件)(顺序无关紧要)拆分为编号文件夹? (ex. 1, 2, 3, 4, 5) And also create a subsequent folder for the leftover files? (例如 1、2、3、4、5)并且还为剩余文件创建一个后续文件夹?

I have found this scripts and tried to modify it for 50 files, but it looks like it only created a the first folder (subdir1)我找到了这个脚本并尝试为 50 个文件修改它,但看起来它只创建了第一个文件夹(subdir1)

@echo off
set /a counter=1
set /a filesperfolder=50
cd dir\dir_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

I disliked the script you found as it was hard to read and used a temp file to keep track of the list of files.我不喜欢您找到的脚本,因为它很难阅读并使用临时文件来跟踪文件列表。 (Also, it evidently doesn't work, so there's that.) (另外,它显然不起作用,所以就是这样。)

@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

Here is another way to do it.这是另一种方法。 We test if there are still files in the directory, if there is, create a new directory and copy 50 files.我们测试目录下是否还有文件,如果有,新建一个目录,复制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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM