batch-file - 使用部分名称创建文件夹和移动文件的批处理脚本
问题描述
我有一个文件列表:
Pcat2.zip
Pcat3.zip
Pcat22.zip
PcatGig10.zip
PcatGig21.zip
Pcolt2ned.zip
PColt3ned.zip
PColt10ned.zip
PColtI-1.zip
PColtIII-6.zip
PcoltIII-11.zip
PcoltIII-18.zip
PcoltIV-2.zip
PetPap25.zip
Pier4.zip
Pier16.zip
ProvSegrIV-4.zip
ProvSegrIII-1.zip
AttFIII-29.zip
AttFlI-5.zip
AttFlII-20.zip
AttFlVI-18.zip
我尝试使用根据文件名中的键字符串创建目录的脚本,并将文件移动到其中,如下所示:
|
+---Pcat
| Pcat2.zip
| Pcat3.zip
| Pcat22.zip
|
+---PcatGig
| PcatGig10.zip
| PcatGig21.zip
|
+---Pcolt
| Pcolt2ned.zip
| PColt3ned.zip
| PColt10ned.zip
| PColtI-1.zip
| PColtIII-6.zip
| PcoltIII-11.zip
| PcoltIII-18.zip
| PcoltIV-2.zip
|
+---PetPap
| PetPap25.zip
|
+---Pier
| Pier4.zip
| Pier16.zip
|
+---ProvSegr
| ProvSegrIV-4.zip
| ProvSegrIII-1.zip
|
+---AttF
| AttFIII-29.zip
|
\---AttFl
AttFlI-5.zip
AttFlII-20.zip
AttFlVI-18.zip
但是,我也想去掉十进制和类似罗马的数字
I-5
III-6
VI-18
VI-18
III-29
...
为了实现这一点,我尝试了这个脚本,但它不起作用。我在这里看一下 在批处理或 powershell 脚本中实现正则表达式以生成文件夹并在相对文件夹中移动文件,按文件名中的键字符串排序
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "SPLITCHAR=-" & rem // (a single character to split the file names)
set "SEARCHSTR=_" & rem // (a certain string to be replaced by another)
set "REPLACSTR= " & rem // (a string to replace all found search strings)
set "OVERWRITE=" & rem // (set to non-empty value to force overwriting)
rem // Get file location and pattern from command line arguments:
set "LOCATION=%~1" & rem // (directory to move the processed files into)
set "PATTERNS=%~2" & rem // (file pattern; match all files if empty)
rem /* Prepare overwrite flag (if defined, set to character forbidden
rem in file names; this affects later check for file existence): */
if defined OVERWRITE set "OVERWRITE=|"
rem // Continue only if target location is given:
if defined LOCATION (
rem // Create target location (surpress error if it already exists):
2> nul md "%LOCATION%"
rem /* Loop through all files matching the given pattern
rem in the current working directory: */
for /F "eol=| delims=" %%F in ('dir /B "%PATTERNS%"') do (
rem // Process each file in a sub-routine:
call :PROCESS "%%F" "%LOCATION%" "%SPLITCHAR%" "%SEARCHSTR%" "%REPLACSTR%"
)
)
endlocal
exit /B
:PROCESS
rem // Retrieve first argument of sub-routine:
set "FILE=%~1"
rem // Split name at (first) split character and get portion in front:
for /F "delims=%~3" %%E in ("%~1") do (
rem // Append a split character to partial name:
set "FOLDER=%%E%~3"
)
setlocal EnableDelayedExpansion
rem // Right-trim partial name:
if not "%~4"=="" set "FOLDER=!FOLDER:%~4%~3=!"
set "FOLDER=!FOLDER:%~3=!"
rem /* Check whether partial name is not empty
rem (could happen if name began with split character): */
if defined FOLDER (
rem // Replace every search string with another:
if not "%~4"=="" set "FOLDER=!FOLDER:%~4=%~5!"
rem // Create sub-directory (surpress error if it already exists):
2> nul md "%~2\!FOLDER!"
rem /* Check if target file already exists; if overwrite flag is
rem set (to an invalid character), the target cannot exist: */
if not exist "%~2\!FOLDER!\!FILE!%OVERWRITE%" (
rem // Move file finally (surpress `1 file(s) moved.` message):
1> nul move /Y "!FILE!" "%~2\!FOLDER!"
)
)
endlocal
exit /B
该脚本需要包含所有要处理的文件的目录作为第一个命令行参数。创建的子目录放置在其中。可选的第二个命令行参数定义文件名模式以过滤某些文件类型/名称。假设它保存为 D:\Script\build-folder-hierarchy.bat,文件包含在 D:\Data 中,并且您只想处理 *.zip 文件,请按如下方式运行:
"C:\Script\build-folder-hierarchy.bat" "C:\Data" "*.zip"
解决方案
这是一个完成您需要的任务的脚本(请参阅所有解释性rem
备注):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~dp0TEST" & rem // (target directory containing files to process)
set "_MASK=*.zip" & rem // (pattern to match the files against)
set "_SEP=-" & rem // (separator between roman and decimal numbers)
set "_ROMAN=I V X L C D M" & rem // (characters that build up roman numbers)
rem // Change into the target directory:
pushd "%_ROOT%" && (
rem // Loop through all matching files:
for /F "delims= eol=|" %%F in ('dir /B /A:-D-H-S "%_MASK%"') do (
rem // Store full file name and base name in variables:
set "FILE=%%F" & set "NAME=%%~nF"
rem // Toggle delayed expansion to avoid trouble with `!`:
setlocal EnableDelayedExpansion
rem /* Split off (first) group of decimal numerals and everything after from
rem base name: */
for /F "delims=0123456789 eol=0" %%E in ("_!NAME!") do (
endlocal
rem // Store resulting file name prefix in variable:
set "PREF=%%E"
setlocal EnableDelayedExpansion
rem /* Check whether last character is the predefined separator character
rem between roman and decimal numbers and remove it in case; afterwards,
rem split off all characters that may build up a roman number: */
set "PREF=!PREF:*_=!"
if defined PREF if "!PREF:~-1!"=="!_SEP!" (
set "PREF=!PREF:~,-1!"
call :ROMAN PREF PREF
)
)
rem // Create sub-directory named like the retrieved file name prefix:
2> nul md "!PREF!"
rem // Move the currently iterated file into the sub-directory (no overwrite):
if not exist "!PREF!\!FILE!" > nul move /Y "!FILE!" "!PREF!"
endlocal
)
rem // Return from the target directory:
popd
)
endlocal
exit /B
:ROMAN
rem // Remove roman number from the end of a provided string:
set "#STR=%~2"
set "#RTN=%~1"
set "%#RTN%=!%#STR%!"
:ROMAN_LOOP
if defined %#RTN% (
rem /* Check whether the last character of the string is a valid roman numeral
rem and split it off in case: */
set "FLAG=" & for %%R in (!_ROMAN! !_ROMAN! !_ROMAN!) do (
if "!%#RTN%:~-1!"=="%%R" set "FLAG=#"
)
if defined FLAG set "%#RTN%=!%#RTN%:~,-1!" & goto :ROMAN_LOOP
)
exit /B
实施以下方法:
- 从文件名中拆分从第一个数字部分开始的所有内容;
- 检查
-
您的示例文件中的余数是否以分隔罗马数字和十进制数字的结尾; - 如果它确实删除它并从末尾删除由可能构建罗马数字的字符组成的最长序列而不检查它是否实际上是有效的;
推荐阅读
- node.js - 在 Heroku CI 环境中通过 CDN 服务 webpack 生成的 CSS 文件
- php - 如何在刀片 laravel 的另一个 foreach 中使用 foreach
- java - 将空字符串解析为 int
- php - PHP - 多维数组合并为一个数组
- nativescript - Nativescript蓝牙读取连续数据
- nested - ClickHouse - 在嵌套字段中搜索
- microsoft-graph-api - 如何从系统自动 GET 调用 Microsoft Graph Explorer 并通过脚本存储输出?
- angular - 根据Angular中的给定结构动态生成表
- ubuntu - Ubuntu 16.4:每个代码编辑器在打开后立即关闭
- sql - 为什么我们不应该在数据库触发器中包含业务逻辑?