简体   繁体   English

使用.bat按文件名将文件排序到文件夹中

[英]Sorting files into folders by file name using .bat

Consider a parent folder C:\\Users..\\Parent Under parent there are 3 folders M1,M2,M3 C:\\Users..\\Parent\\M1, C:\\Users..\\Parent\\M2, C:\\Users..\\Parent\\M3. 考虑一个父文件夹C:\\ Users .. \\ Parent在父级下面有3个文件夹M1,M2,M3 C:\\ Users .. \\ Parent \\ M1,C:\\ Users .. \\ Parent \\ M2,C:\\ Users。 \\父母\\ M3。 Under M1,M2,M3 there is 100 sub folders. 在M1,M2,M3下有100个子文件夹。 C:\\Users..\\Parent\\M1\\MattP001M1,C:\\Users..\\Parent\\M1\\MattP002M1,so on till C:\\Users..\\Parent\\M1\\MattP100M1. C:\\ Users .. \\ Parent \\ M1 \\ MattP001M1,C:\\ Users .. \\ Parent \\ M1 \\ MattP002M1,依此类推至C:\\ Users .. \\ Parent \\ M1 \\ MattP100M1。 Similarly for M2,M3 as well. 同样对于M2,M3也是如此。


Under every folder(MattP001M1..MattP100M1) there are a ton of .wav files(close to 1500 on an avg). 在每个文件夹(MattP001M1..MattP100M1)下都有大量的.wav文件(平均值接近1500)。 These wav files have a pattern in their naming. 这些wav文件在命名中有一个模式。 eg: There are 20 files with German_09mea4567_morename and 15 files with German_4132azzi_morename and so on. 例如:有20个带有German_09mea4567_morename的文件和15个带有German_4132azzi_morename的文件,依此类推。 I am using this script on them to group them in folders based on the unique part after(09mea4567). 我在他们上面使用这个脚本根据(09mea4567)之后的独特部分将它们分组到文件夹中。

SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (*.wav) do (
set f=%%a
set g=!f:~7,8!
md "!g!" 2>nul
move "%%a" "!g!"
)

Now this is fine for one folder. 现在这适用于一个文件夹。 I want to do this for all the folders under M1(MattP001M1,..,MattP100M1), M2, M3. 我想对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 this time 希望这次我能让自己更加清晰

@ECHO OFF &SETLOCAL
for /d %%x in ("x:\parent folder\folder*") do (
    pushd "%%~x"
    for %%a in (*.wav) do for /f "tokens=1-4delims=_" %%b in ("%%~na") do (
        md "%%~e" 2>nul
        move "*_*_*_%%~e%%~xa" "%%~e" 2>nul
    )
    popd
)

This Solved my problem. 这解决了我的问题。 Thanks to solution by MC ND 感谢MC ND的 解决方案

@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"  md "%%~dpf\%%~p"
            move "%%~ff" "%%~dpf\%%~p"
        )
    )
)

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

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