批处理:从文件名(子字符串)创建文件夹

Batch: Create folders from filename (substring)

我有大量文件想以不同方式组织。批处理脚本应使用文件名中日期左侧的子字符串创建文件夹。

文件现在这样命名:

This_is_my_file_21.01.29_22-00_abc_115.avi
This_is_my_file_20.09.29_21-10_abc_15.avi
This_is_another_file_21.01.29_22-00_abc_55.avi

模式:

<下划线数目未知的名称>___<长度未知的字符串>_<1-999 中的数字 n>.avi

文件夹应该这样命名:

This_is_my_file <- 两个文件将进入此目录
This_is_another_file <- 只有一个文件。

问题是,如何为我的文件夹名称获取正确的子字符串?

这是我目前拥有的:

@echo off
setlocal

set "basename=."
for /F "tokens=1* delims=." %%a in ('dir *.avi /B /A-D ^| sort /R') do (
   set "filename=%%a"
   setlocal EnableDelayedExpansion
   

   
   for /F "delims=" %%c in ("!basename!") do if "!filename:%%c=!" equ "!filename!" (
      set "basename=!filename!"
      md "!basename:~0,-23!"
   )
   move "!filename!.%%b" "!basename:~0,-23!"
   for /F "delims=" %%c in ("!basename!") do (
      endlocal
      set "basename=%%c
   )
)
@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files\t w o"

FOR /f "delims=" %%b IN ('dir /b /a-d "%sourcedir%\*.avi" ' ) DO (
 SETLOCAL ENABLEDELAYEDEXPANSION
  CALL :countus "%%b"
  IF DEFINED subdir (
   MD "!subdir!" 2>NUL
   ECHO MOVE "%sourcedir%\%%b" "%sourcedir%\!subdir!\"
  ) ELSE (
   ECHO Failed pattern check %%b
  )
 ENDLOCAL
)
GOTO :EOF

:: count number of underscores before pattern YY.MM.DD_hh-mm
:countus
SET /a ucount=0
:countusloop
SET /a ucount+=1
SET /a scount=ucount+1
FOR /f "tokens=%ucount%,%scount%delims=_" %%q IN ("%~1") DO SET "str1=%%q"&SET "str2=%%r"
IF NOT DEFINED str2 SET "subdir="&GOTO :EOF 
:: is %str1%.%str2:-=.%. of form np.np.np.np.np where np is a number-pair?
SET "candidate=%str1%.%str2:-=.%."
FOR /L %%c IN (10,1,99) DO IF DEFINED candidate SET "candidate=!candidate:%%c.=!"&IF NOT DEFINED candidate GOTO success
FOR /L %%c IN (0,1,9) DO IF DEFINED candidate SET "candidate=!candidate:0%%c.=!"&IF NOT DEFINED candidate GOTO success
GOTO countusloop
:success
SET "subdir=%~1"
FOR /f "delims=:" %%e IN ("!subdir:_%str1%_%str2%=:!") DO SET "subdir=%%e"
GOTO :eof

“移动”命令只是为了验证而回显。从 echo move 中删除 echo 以实际移动文件。

这个可能的解决方案使用了这样一个事实,即如果你向后工作,你的文件名有一个已知数量的下划线。我所做的就是用反斜杠替换那些下划线,显然不能包含在文件名中。然后我可以使用相对路径来增加文件名,就好像它是目录树一样,直到我只剩下日期序列前面的部分,然后我再次用下划线替换反斜杠。我将结果与 一起使用,它有一个移动选项,如果目标目录不存在,它将自动创建。一开始,我在与批处理文件相同的目录中执行目录搜索,使用 where.exe,(您可以在第三行将其从 "%~dp0." 更改为 ".",如果您想改用当前目录,请根据需要 "any other path")。 where.exe 不仅将 ? 通配符恰好视为一个字符(不同于 dir 命令,后者是零或一),而且忽略 8.3 命名。因此,它完全按照写入的方式处理 .avi 扩展(而不是 'beginning with' .avidir 或标准的 for 循环)。

总之,不妨试试看:

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "}=" & For /F Delims^= %%G In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe
 "%~dp0.":"?*_??.??.??_??-??_?*.avi" 2^> NUL') Do (Set "}=%%~nG"
    SetLocal EnableDelayedExpansion & For %%H In ("\!}:_=\!") Do (
        EndLocal & For %%I In ("%%~pH..\..") Do (Set "}=%%~pI"
            SetLocal EnableDelayedExpansion & Set "}=!}:~1,-1!"
            For %%J In ("!}:\=_!") Do (EndLocal & %__AppDir__%robocopy.exe ^
             "%%~dpG." "%%~dpG%%~J" "%%~nxG" /Mov 1> NUL))))

如果您想要更进一步的健壮性,并且不想使用更合适的脚本技术,下面的看起来非常复杂,版本是相同的代码,除了它使用 来验证日期和时间序列。它过滤那些包含以下模式的 avi 文件,avi 文件名中的 _yy.MM.dd_hh-mm_,使用从 1970 年初到 2021 年底的所有日期:

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "}=" & For /F Delims^= %%G In ('(Set PATHEXT^=^) ^& %__AppDir__%where.exe
 "%~dp0.":"?*_??.??.??_??-??_?*.avi" 2^> NUL ^| %__AppDir__%findstr.exe
 /RC:"_[789][0123456789].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[789][0123456789].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[789][0123456789].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[789][0123456789].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[789][0123456789].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[789][0123456789].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
 /C:"_[789][0123456789].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[789][0123456789].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[789][0123456789].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[789][0123456789].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[789][0123456789].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[789][0123456789].1[012].3[01]_2[0123]-[012345][0123456789]_"
 /C:"_[01][0123456789].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[01][0123456789].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[01][0123456789].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[01][0123456789].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[01][0123456789].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[01][0123456789].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
 /C:"_[01][0123456789].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[01][0123456789].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[01][0123456789].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[01][0123456789].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
 /C:"_[01][0123456789].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
 /C:"_[01][0123456789].1[012].3[01]_2[0123]-[012345][0123456789]_"
 /C:"_2[01].0[123456789].0[123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_2[01].0[123456789].0[123456789]_2[0123]-[012345][0123456789]_"
 /C:"_2[01].0[123456789].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_2[01].0[123456789].[12][0123456789]_2[0123]-[012345][0123456789]_"
 /C:"_2[01].0[123456789].3[01]_[01][0123456789]-[012345][0123456789]_"
 /C:"_2[01].0[123456789].3[01]_2[0123]-[012345][0123456789]_"
 /C:"_2[01].1[012].0[123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_2[01].1[012].0[123456789]_2[0123]-[012345][0123456789]_"
 /C:"_2[01].1[012].[12][0123456789]_[01][0123456789]-[012345][0123456789]_"
 /C:"_2[01].1[012].[12][0123456789]_2[0123]-[012345][0123456789]_"
 /C:"_2[01].1[012].3[01]_[01][0123456789]-[012345][0123456789]_"
 /C:"_2[01].1[012].3[01]_2[0123]-[012345][0123456789]_"') Do (Set "}=%%~nG"
    SetLocal EnableDelayedExpansion & For %%H In ("\!}:_=\!") Do (
        EndLocal & For %%I In ("%%~pH..\..") Do (Set "}=%%~pI"
            SetLocal EnableDelayedExpansion & Set "}=!}:~1,-1!"
            For %%J In ("!}:\=_!") Do (EndLocal & %__AppDir__%robocopy.exe ^
             "%%~dpG." "%%~dpG%%~J" "%%~nxG" /Mov 1> NUL))))