批处理文件帮助:根据文件名将文件分类到专门命名的文件夹中,但有例外
Batch File Help: Sorting files into specifically-named folders based on filename with exceptions
我的操作系统是 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) 开头的文件名。
- 获取超出第一个分隔符 (_) 的字符串。
至于第二点,我认为文件名有时可能太复杂而无法正确区分将哪一部分用作文件夹的名称。例如,它排序:
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 EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\#Sorted"
rem // Change into target directory:
pushd "%_ROOT%" && (
rem // Loop through matching files:
for /F "eol=| tokens=1,2,* delims=_" %%A in ('dir /B /A:-D-H-S "*_*.txt"') do (
rem // Skip if file name consists of less than two `_`-separated parts:
if not "%%B"=="" (
rem // Check if file name consists of more than two parts:
if "%%C"=="" (
rem // Two parts, so part one becomes directory name:
2> nul md "@%%A"
move "%%A_%%B" "@%%A\%%A_%%B"
) else (
rem // More than two parts:
set "ONE=%%A"
setlocal EnableDelayedExpansion
rem // Check length of part one:
if "!ONE:~3!"=="" (
rem /* Part one contains not more than 3 characters,
rem hence parts one and two become directory name: */
endlocal
2> nul md "@%%A_%%B"
move "%%A_%%B_%%C" "@%%A_%%B\%%A_%%B_%%C"
) else (
rem /* Part one contains more than 3 characters,
rem hence only part one becomes directory name: */
endlocal
2> nul md "@%%A"
move "%%A_%%B_%%C" "@%%A\%%A_%%B_%%C"
)
)
)
)
rem // Return from target directory:
popd
)
endlocal
exit /B
您可以使用以下代码快速完成此操作:
@(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
您的规格不够明确。我 假设 文件夹名称由文件名的第一部分组成,不是数字。此方法适用于您的文件名示例:
@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"
我的操作系统是 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) 开头的文件名。
- 获取超出第一个分隔符 (_) 的字符串。
至于第二点,我认为文件名有时可能太复杂而无法正确区分将哪一部分用作文件夹的名称。例如,它排序:
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 EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=D:\#Sorted"
rem // Change into target directory:
pushd "%_ROOT%" && (
rem // Loop through matching files:
for /F "eol=| tokens=1,2,* delims=_" %%A in ('dir /B /A:-D-H-S "*_*.txt"') do (
rem // Skip if file name consists of less than two `_`-separated parts:
if not "%%B"=="" (
rem // Check if file name consists of more than two parts:
if "%%C"=="" (
rem // Two parts, so part one becomes directory name:
2> nul md "@%%A"
move "%%A_%%B" "@%%A\%%A_%%B"
) else (
rem // More than two parts:
set "ONE=%%A"
setlocal EnableDelayedExpansion
rem // Check length of part one:
if "!ONE:~3!"=="" (
rem /* Part one contains not more than 3 characters,
rem hence parts one and two become directory name: */
endlocal
2> nul md "@%%A_%%B"
move "%%A_%%B_%%C" "@%%A_%%B\%%A_%%B_%%C"
) else (
rem /* Part one contains more than 3 characters,
rem hence only part one becomes directory name: */
endlocal
2> nul md "@%%A"
move "%%A_%%B_%%C" "@%%A\%%A_%%B_%%C"
)
)
)
)
rem // Return from target directory:
popd
)
endlocal
exit /B
您可以使用以下代码快速完成此操作:
@(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
您的规格不够明确。我 假设 文件夹名称由文件名的第一部分组成,不是数字。此方法适用于您的文件名示例:
@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"