【发布时间】:2021-07-01 07:19:13
【问题描述】:
我有一个文件列表:
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
...
为了实现这一点,我尝试了这个脚本,但它不起作用。 我看看这里 Implement Regex in batch or powershell script to generate folders and move files in relative folders ordered by key string in file name
@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"
【问题讨论】:
-
我已尝试改进您的问题帖子,但在此过程中,我注意到了一个异常情况,我无法准确地将其置于您的问题中。关于剥离名称以及十进制和罗马数字的部分。首先是许多罗马数字,例如
I、V、X、L、C、D和M是字母,已经作为字符串组件包含在您的文件名中,其次,如果这些被删除,您将遇到多个有问题的名称冲突。请澄清它应该是什么意思,以及你目前在你的代码中做了什么来迎合它。此外,“它不起作用”也不是一个有用的解释。 -
@Compo 是的,你是对的。我将“罗马数字”替换为“类似罗马的数字”,因为在这里您不能将其视为字母而是字母数字字母,因为在文件名中您可以找到
I-5、III-6、VI-18、III-29。 . ... -
您的问题特别提到了剥离,而不是替换,您的代码似乎也没有这样做。本网站不提供您自己未尝试过的代码,它不是免费的脚本/编码服务。在许多地方,您可以与某人签订服务合同,为您提供编码和脚本解决方案,根据指南,这不是其中之一。还请调试您的脚本,正如我已经提到的,您需要解释脚本的哪一部分“不起作用”,即什么没有按预期工作,什么是/没有发生,等等。跨度>
-
另外,您还没有回答我的问题,脚本如何知道
PColtI或ProvSegrIV是字符串,还是与罗马数字连接的字符串?代码是否需要识别一个或多个字母字符的每个可能序列,这些字母字符可以被理解为有效的罗马数字本身?还是您希望它具有某种类似人类的 AI 能力? -
您还需要提前明确定义您希望创建的目录名称,因为必须有一种机制来属性
PcatGig字符串文件,之前Pcat字符串文件,否则如果先解析,前者将被放置到后者的位置!
标签: batch-file