[英]Batch move files to new subfolders based on name
I'm using Windows 7 and I have half a million images stored in one folder "C:\\capture".我使用的是 Windows 7,并且在一个文件夹“C:\\capture”中存储了 50 万张图像。 These images forms 330 image sequences and they are named as follows:
这些图像形成了 330 个图像序列,它们的命名如下:
1.0000000000.png
...
1.0000003299.png
... ...
330.0000000000.png
...
330.0000000010.png
I would like to move these into 330 subfolders named after the first part of the names.我想将这些移动到以名称的第一部分命名的 330 个子文件夹中。
C:\capture\1\1.0000000000.png
...
C:\capture\1\1.0000003299.png
... ...
C:\capture\330\330.0000000000.png
...
C:\capture\330\330.0000000010.png
So I'm basically only interested in everything before the first '.'所以我基本上只对第一个 '.' 之前的所有内容感兴趣。 in the names.
在名称中。 How do I write a batch file that creates the subfolders and moves the corresponding files into them?
如何编写创建子文件夹并将相应文件移动到其中的批处理文件?
To be executed from command line inside the directory to be processed (better if you try with a copy)从要处理的目录内的命令行执行(如果您尝试使用副本更好)
for /l %a in (1 1 330) do (md %a 2>nul & if exist %a\ move /y "%a.*.png" %a\ )
If you want to use it inside a batch file, percent signs need to be escaped, replacing all %a
with %%a
如果要在批处理文件中使用它,则需要转义百分号,将所有
%a
替换为%%a
for /l %%a in (1 1 330) do (
md %%a 2>nul
if exist %%a\ move /y "%%a.*.png" %%a\
)
It will generate all the prefixes and for each one execute a move command to move the matching files to the final folder (previously created)它将生成所有前缀,并为每个前缀执行一个移动命令将匹配的文件移动到最终文件夹(之前创建的)
Here is a more general batch script, that is boundary agnostic , in the sense that it will automatically decide how many subfolders have to be created, based on the "prefixes" of the images, so you don't have to modify it in case the number of image sequences changes.这是一个更通用的批处理脚本,即边界不可知,从某种意义上说,它将根据图像的“前缀”自动决定必须创建多少个子文件夹,因此您不必修改它以防万一图像序列的数量发生变化。
@echo off
set DIR="C:\capture"
pushd %DIR%
setlocal ENABLEDELAYEDEXPANSION
for %%g in (*.png) do (
set t=%%~nxg
for /F "delims=." %%a in ("!t!") do (
if not exist %%a (md %%a)
move !t! %%a > nul
)
)
endlocal
popd
Taking my inspiration from sokins answer, I'll weigh in with this effort...从 sokins 的答案中汲取灵感,我会为此努力……
@echo off
setlocal enabledelayedexpansion
for %%i in (C:\capture\*.png) do (
for /f "tokens=3 delims=.\" %%a in ('echo %%i') do (
set prefix=%%a
if not exist C:\capture\!prefix! md C:\capture\!prefix!
move "%%i" C:\capture\!prefix!
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.