批处理文件帮助:根据文件名将文件分类到专门命名的文件夹中,但有例外

问题描述

我的操作系统是Windows 7,我有一些文件,例如:

123.txt
abcd_123.txt
abcd_1234.txt
bcde_123_456.txt
bcde_123_4567.txt
cde_fgh_123_456.txt
cde_fgh_123_4567.txt

我希望基于这些文件名的开头部分(或不带尾随数字)生成文件夹,并以特定字符开头,然后将文件相应地分类到其中。结果示例:

@abcd\abcd_123.txt
@abcd\abcd_1234.txt
@bcde\bcde_123_456.txt
@bcde\bcde_123_4567.txt
@cde_fgh\cde_fgh_123_456.txt
@cde_fgh\cde_fgh_123_4567.txt

*123.txt is skipped / not sorted.

这是我到目前为止提出的代码

@echo OFF
    
setlocal enabledelayedexpansion

set var_dir="#Sorted"

for /f "delims=_" %%i in ('dir /b /a-d *_*.txt') do (
 mkdir "#Sorted\@%%i" 2>nul
 move "%%i_*.txt" "%var_dir%\@%%i" >NUL 2>nul
)

echo Sorting Complete!
@pause

GOTO :EOF

它有效,但是我不确定如何:

  • 忽略以数字(0-9)开头的文件名。
  • 获取一个delim(_)之后的字符串。

对于第二点,我认为文件名有时可能太复杂而无法正确地区分将哪个部分用作文件夹的名称。示例,它排序:

cde_fgh_123_4567.txt

进入:

@cde\cde_fgh_123_4567.txt

正因如此,我一直在考虑算法,例如:

Set Folder Name to 
(1) string before (first) "_" if string is greater than 3 characters 
OR
(2) entire string before second "_" if first string is less than or equal to 3 characters

因此,以上示例应更改为:

@cde_fgh\cde_fgh_123_4567.txt

如何改进批处理代码以获得所需的结果?

解决方法

您的规格不够清晰。我假定,文件夹名称由文件名的第一部分组成,而不是数字。此方法适用于您的文件名示例:

@echo off
setlocal EnableDelayedExpansion

for %%a in (*.txt) do (
   call :getFolder "%%~Na"
   if defined folder (
      if not exist "!folder!\" mkdir "!folder!"
      move "%%a" "!folder!"
   )
)
goto :EOF


:getFolder filename
set "file=%~1"
rem Separate filename in parts at undescores and
rem collect them while part is not a number (greater than zero)
set "folder="
set "part=%file:_=" & set /A "num=part" & (if !num! equ 0 set "folder=!folder!_!part!") & set "part=%"
if defined folder set "folder=@%folder:~1%"
exit /B

例如,这是使用您的数据获得的结果:

File "123.txt" folder ""
File "abcd_123.txt" folder "@abcd"
File "abcd_1234.txt" folder "@abcd"
File "bcde_123_456.txt" folder "@bcde"
File "bcde_123_4567.txt" folder "@bcde"
File "cde_fgh_123_456.txt" folder "@cde_fgh"
File "cde_fgh_123_4567.txt" folder "@cde_fgh"
,

您可以执行以下操作:

XmlDocument
,

您可以使用以下代码快速完成此操作:

@(SETLOCAL ENABLEDELAYEDEXPANSION
  ECHO OFF
  SET "_SrcFolder=C:\Admin\CMD\s-e\tmp"
  REM Use for DIR to pre-filter the list as much as possible
  SET "_FileGlob=*_*.txt"
  REM Used for FindStr Matches a Value that Begins with non nummeric characters,followed by an underscore multiple times,followed by any number of numeric charactrs and undeerscore and ending in .txt
  SET "_FileRegex=!_SrcFolder:\=\\!\\[a-Z][a-Z_]*_[0-9][0-9_]*.txt$"
)

CALL :Main

( ENDLOCAL
  EXIT /B
)

:Main
  REM Loop through the file sin the directory filtering non-matches and then perform actions based on matches
  ECHO."%_FileRegex%"
  FOR /F "Tokens=*" %%A IN ('
    DIR /S/B/A-D "%_SrcFolder%\%_FileGlob%" ^|
      FINDSTR /r "%_FileRegex%"
  ') DO (
    SET "_FileName=%%~nA"
    REM Break File Name after the Characters needed for the directory
    FOR /F "TOKENS=* delims=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" %%a IN (
      "%%~nA"
    ) DO (
      REM Create Folder
      IF NOT EXIST "%_SrcFolder%\@!_FileName:_%%a=!\" (
        ECHO.&ECHO.== Creating Folder "@!_FileName:_%%a=!"
        MD "%_SrcFolder%\@!_FileName:_%%a=!\"
      )
      REM Move Original File to the New Directory 
      ECHO. + Moving "%%~nxA" TO "@!_FileName:_%%a=!"
      MOVE /Y "%%A" "%_SrcFolder%\@!_FileName:_%%a=!\%%~nxA" >NUL
    )
  )
GOTO :EOF