簡體   English   中英

使用.bat根據文件名中的模式將文件分類到文件夾中

[英]Sorting files into folders based on a pattern in their name using .bat

考慮一個父文件夾

C:\Users\..\Parent

在父目錄下有3個文件夾M1,M2,M3

C:\Users\..\Parent\M1
C:\Users\..\Parent\M2
C:\Users\..\Parent\M3.

在M1,M2,M3下有100個子文件夾。

C:\Users\..\Parent\M1\MattP001M1
C:\Users\..\Parent\M1\MattP002M1
so on till 
C:\Users\..\Parent\M1\MattP100M1.

M2,M3也是如此。


在每個文件夾(MattP001M1..MattP100M1)下,都有大量的.wav文件(在avg上接近1500)。 這些wav文件在其命名中具有模式。 例如:有20個具有German_09mea4567_morename文件和15個具有German_4132azzi_morename文件,依此類推。 我在他們身上使用此腳本,將它們基於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下的所有文件夾執行此操作。

請注意:這是在一台機器上的設置。 在另一台機器上,而不是德語,還有其他語言。

希望我能使自己更加清楚。

@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之前刪除echo命令,然后move使其md

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM