使用.bat根据文件名中的模式将文件分类到文件夹中 [英] Sorting files into folders based on a pattern in their name using .bat
问题描述
考虑一个父文件夹
C:\Users\..\Parent
在父项下有3个文件夹M1,M2,M3
Under parent there are 3 folders M1,M2,M3
C:\Users\..\Parent\M1
C:\Users\..\Parent\M2
C:\Users\..\Parent\M3.
在M1,M2,M3下,有100个子文件夹。
Under M1,M2,M3 there is 100 sub folders.
C:\Users\..\Parent\M1\MattP001M1
C:\Users\..\Parent\M1\MattP002M1
so on till
C:\Users\..\Parent\M1\MattP100M1.
与M2,M3类似。
在每个文件夹(MattP001M1..MattP100M1)下,都有大量的.wav文件(平均水平上接近1500个)。
这些wav文件的命名方式相同。
例如:存在20个 German_09mea4567_morename
的文件和15个 German_4132azzi_morename
的文件,依此类推。
我正在对它们使用此脚本,根据之后的唯一部分将它们分组到文件夹中(09mea4567)。
Under every folder(MattP001M1..MattP100M1) there are a ton of .wav files(close to 1500 on an avg).
These wav files have a pattern in their naming.
e.g: There are 20 files with German_09mea4567_morename
and 15 files with German_4132azzi_morename
and so on.
I am using this script on them to group them in folders based on the unique part after(09mea4567).
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.wav) do (
set f=%%a
set g=!f:~7,8!
md "!g!" 2>nul
move "%%a" "!g!"
)
现在,这对于一个文件夹来说很好。我想对M1(MattP001M1,..,MattP100M1),M2,M3下的所有文件夹执行此操作。
Now this is fine for one folder. I want to do this for all the folders under M1(MattP001M1,..,MattP100M1), M2, M3.
请注意:这是在一台计算机上的设置。在另一台机器上而不是德语上,还有其他语言。
Please note: This is a setup on one machine. On a different machine instead of German there is some other language.
希望我使自己更加清晰。
Hope i made myself much clearer enough.
推荐答案
@echo off
rem Prepare environment
setlocal enableextensions disabledelayedexpansion
rem configure where to start
set "root=c:\somewhere"
rem For each file under root that match indicated pattern
for /r "%root%" %%f in (*_*_*.wav) do (
rem Split the file name in tokens using the underscore as delimiter
for /f "tokens=2 delims=_" %%p in ("%%~nf") do (
rem Test if the file is in the correct place
for %%d in ("%%~dpf.") do if /i not "%%~p"=="%%~nd" (
rem if it is not, move it where it should be
if not exist "%%~dpf\%%~p" echo md "%%~dpf\%%~p"
echo move "%%~ff" "%%~dpf\%%~p"
)
)
)
目录创建和文件移动将回显到控制台。如果输出正确,请在 md
和 move
echo 命令>使其生效。
Directory creation and file move are echoed to console. If output is correct, remove echo
command before md
and move
to make it work.
这篇关于使用.bat根据文件名中的模式将文件分类到文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!