【问题标题】:Sorting files into folders by file name using .bat使用 .bat 按文件名将文件排序到文件夹中
【发布时间】:2013-12-17 13:56:39
【问题描述】:

考虑一个父文件夹 C:\用户..\父 在 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,依此类推,直到 C:\Users..\Parent\M1\MattP100M1。对于 M2、M3 也是如此。


在每个文件夹 (MattP001M1..MattP100M1) 下都有大量 .wav 文件(平均接近 1500 个)。 这些 wav 文件的命名有一定的规律。 例如:有 20 个带有 German_09mea4567_morename 的文件和 15 个带有 German_4132azzi_morename 的文件,依此类推。 我在他们身上使用这个脚本,根据 (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 下的所有文件夹执行此操作。

请注意:这是一台机器上的设置。在另一台机器上而不是德语上有一些其他语言。

希望这次我让自己更清楚了

【问题讨论】:

  • 也许我错了或者我不明白这个问题,但似乎为每个子文件夹重复相同的代码没有意义。假设有一个文件夹“01”,因为它们的第 7 位和第 8 位字符相等,所以其中的所有文件都放置在其中。对这些文件再次运行相同的代码只会在“01”文件夹下生成另一个“01”文件夹。

标签: file batch-file cmd aggregation


【解决方案1】:
@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
)

【讨论】:

  • @Matt 我做了一些改进。
  • 你能解释一下你的代码在做什么吗?帮助我更好地理解
【解决方案2】:

这解决了我的问题。感谢solutionMC 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"
        )
    )
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-07
    • 2016-06-22
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多