弱化模式匹配条件

时间:2018-08-03 12:19:19

标签: batch-file

对于我的文件夹名称,我使用类似的名称

Fringe.S04
Fringe S01
From.Dusk.Till.Dawn_S01.ITA
Fear_The_Walking_Dead_S01
Fear_The.Walking_Dead_S01
Gossip Girl s2
Gossip.Girl_s2

Hawaii_Five_0_S05
Hawaii.Five_0.S01
Hawaii Five-0 S05
Cape_Wrath_Pack_2
Cape Wrath Pack 1
C Era Una Volta
C'Era Una Volta
C' Era Una Volta

我希望忽略名称之间的这些标点符号

.
_
'

因为我希望匹配文件夹名称的模式像这样

Hawaii_Five_0 = 夏威夷五人0
夏威夷。Five_0.S01= 夏威夷五个5
夏威夷5-0 S05 = 夏威夷5-0
Fear_The.Walking_Dead_S01 = 恐惧行尸走肉
Fear_The Walking_Dead = 恐惧行尸走肉
Fringe.S04 = 边缘
边缘S01 = 边缘
...

我使用的实际代码是

:: Q:\Test\2018\07\27\SO_51539948.cmd
@Echo off
Pushd %1
For /d %%A in (*) do For /f "delims=" %%B in (
  'Dir /B "*%%~nxA*" 2^>Nul '
) do If "%%~nxA" NEQ "%%~nxB" Move "%%~fB" "%%~fA\" 2>&1>>Nul
Popd

2 个答案:

答案 0 :(得分:1)

以下批次尝试统一文件夹名称,
但是由于示例中的差异,它无法完美运行。

  1. .ita扩展名作为最后一个元素不符合方案
  2. Hawaii Five 0将在第二次运行中减少为Hawaii Five

编辑:重命名文件夹后效果略好

:: Q:\Test\2018\07\27\SO_51672484.cmd
@Echo off
Set "Base=%~1"
if not defined Base set "Base=%~dp0"
Pushd "%Base%"
For /d %%A in (*) do Call :Unify %%~nxA
Popd

Goto :Eof
:Unify change all delims to space
set "Folder=%*"
Set "Unified=%Folder:.= %"
Set "Unified=%Unified:_= %"
Set "Unified=%Unified:-= %"
Set "Unified=%Unified:'= %"
Set "Unified=%Unified:  = %"
Set "Unified=%Unified:  = %"

:: check for trailing S##
For %%S in (%Unified%) Do Set "Last=%%S"
Set "Series="
Echo=%Last%|Findstr /ie " s*[0-9][0-9]*" 2>&1>Nul && Set "Series=1"
Echo Unified:%Unified%
Echo Folder :%Folder%
Echo Series :%Series%
if defined Series (
    call set "Unified=%%Unified: %Last%=%%"
) else (
    if not exist "%Base%\%Unified%" (
        Ren "%Base%\%Folder%" "%Base%\%Unified%"
        Goto :Eof
    )
)
if "%Folder%" Equ "%Unified%" goto :Eof
if not exist "%Base%\%Unified%" MD "%Base%\%Unified%\"
move "%Base%\%Folder%" "%Base%\%Unified%\"

示例输出:

> tree
Auflistung der Ordnerpfade für Volume RamDisk
Volumeseriennummer : 5566-7788
A:.
└───Videos
    ├───C Era Una Volta
    │   ├───C' Era Una Volta
    │   └───C'Era Una Volta
    ├───Cape Wrath Pack
    │   ├───Cape Wrath Pack 1
    │   └───Cape_Wrath_Pack_2
    ├───Fear The Walking Dead
    │   ├───Fear_The.Walking_Dead_S01
    │   └───Fear_The_Walking_Dead_S01
    ├───Fringe
    │   ├───Fringe S01
    │   └───Fringe.S04
    ├───From.Dusk.Till.Dawn_S01.ITA
    ├───Gossip Girl
    │   ├───Gossip Girl s2
    │   └───Gossip.Girl_s2
    └───Hawaii Five 0
        ├───Hawaii Five-0 S05
        ├───Hawaii.Five_0.S01
        └───Hawaii_Five_0_S05

答案 1 :(得分:1)

在这种方法中,插入其他定界符非常简单:

编辑:已修改代码,以满足注释中给出的要求。

@echo off
setlocal EnableDelayedExpansion

pushd %1

set "digits=0123456789"
for /F "delims=" %%l in ('dir /B /AD') do for /F "tokens=1-9 delims='-._ " %%a in ("%%l") do (
   set "out="
   set "end="
   for %%t in (%%a %%b %%c %%d %%e %%f %%g %%h %%i) do (
      if "!digits:%%t=!" neq "%digits%" set "end=1"
      set "t=%%t"
      for /F %%n in ("!t:~1,1!") do (
         if /I "!t:~0,1!" equ "S" if "!digits:%%n=!" neq "%digits%" set "end=1"
      )
      if not defined end set "out=!out! %%t"
   )
   if /I "!out!" equ " Hawaii Five" set "out= Hawaii Five 0"
   move "%%l" "!out:~1!"
)

在此新代码中,“ Hawaii_Five_0”名称已作为特殊情况正确管理。