如何从DOS批处理中的一组其他更长的字符串中删除变化的字符串

问题描述 投票:1回答:1

背景信息:目录c:\ documents充满了来自不同人员的.doc和.xls文件。为了识别谁编辑了文件,在文件名中的某些位置插入了缩写。每个文件名可以具有一个或多个初始集。这次我只对.doc文件感兴趣。该目录的横截面如下所示:

depot.inventory.20180921.[CMP]-[OxA](DOT)-(TTR).edited.doc
rack_location_(IIY)collected.2018.11.24.edit[UTS]_{POM}.doc

并且列表会持续显示数百个文件。我想生成这些文件的副本而没有编辑器首字母,并将它们放到名为c:\ uniform

的目录中

这里的常数是,每组缩写的首字母均为3个字母,可以是大写或小写,并用某种括号括起来。在任何给定时间,我都有一个文件编辑者姓名缩写列表,每行格式一组,例如

CMP
OXA
TTR
DOT
UTS
IIY
POM

并且在任何给定的日期也使用大约100-150个名称

到目前为止,我已经弄清楚了如何从所有.doc文件中删除一组首字母,如下所示:

for /R "C:\documents" %%f in (*.doc) do (
    call :Sub %%~nf
    )

:Sub
set str=%*
set str=%str:[DOT]=%
echo %str%

[这里,在此代码段中,我以[DOT]为例,我想将字符串[[DOT]]设置为变量并从编辑器缩写文件中读取它,但是每个文档文件需要很多时间。

因此,我的批处理程序将循环遍历源目录中的所有* .doc文件,对于每个文件,它将循环遍历100-150个名称,并删除这些字符串并形成新的文件名,然后从源目录,带有新文件名的目标目录,这是从源文件名的版本中删除的编辑器缩写。我该如何进行第二循环?我对语法感到困惑。

string batch-file dos
1个回答
0
投票

这是此异常文件复制任务的批处理批处理文件。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFolder=C:\documents"
set "DestinationFolder=C:\uniform"

rem Is there no *.doc file to process in source directory?
if not exist "%SourceFolder%\*.doc" goto :EOF

rem Do nothing if the text file with editors' initials
rem does not exist in the batch file directory.
if not exist "%~dp0EditorsInitials.txt" goto :EOF

rem Create the destination directory on not already existing
rem and veriy the real existence of the destination directory.
md "%DestinationFolder%" 2>nul
if not exist "%DestinationFolder%\" goto :EOF

rem Read the editors' initials from text file and create a space separated
rem list of them assigned to the environment variable EditorsInitials.

setlocal EnableDelayedExpansion
set "EditorsInitials="
for /F "usebackq" %%I in ("%~dp0EditorsInitials.txt") do set "EditorsInitials=!EditorsInitials! %%~I"
endlocal & set "EditorsInitials=%EditorsInitials:~1%"

rem For each non-hidden *.doc file in source directory get file name with
rem file extension and with path if there is one specified left to *.doc
rem and assign it to the environment variable FullFileName. The file name
rem only is assigned to the environment variable FileName. Then delayed
rem environment variable expansion is enabled again for running two nested
rem loops which runs case-insensitive string substitutions on the file name
rem string value to remove the editors' initials from the file name. Next
rem one more loop is used to remove also .edited and .edit from file name.
rem The current *.doc file is finally copied with cleaned file name to
rem the configured destination directory. A date in file name remains.

for %%I in ("%SourceFolder%\*.doc") do (
    set "FullFileName=%%I"
    set "FileName=%%~nI"
    setlocal EnableDelayedExpansion
    for %%J in (%EditorsInitials%) do for %%K in ("-" "." "_" "") do (
        set "FileName=!FileName:%%~K[%%J]=!"
        set "FileName=!FileName:%%~K(%%J)=!"
        set "FileName=!FileName:%%~K{%%J}=!"
    )
    for %%J in (".edited" ".edit") do set "FileName=!FileName:%%~J=!"
    copy "!FullFileName!" "%DestinationFolder%\!FileName!%%~xI" >nul
    endlocal
)
endlocal

此批处理文件执行命令copy,并在批处理文件目录中包含文件EditorsInitials.txt,该文件包含带有参数的两个示例* .doc文件的编辑者姓名缩写的发布列表:

"C:\documents\depot.inventory.20180921.[CMP]-[OxA](DOT)-(TTR).edited.doc" "C:\uniform\depot.inventory.20180921.doc"
"C:\documents\rack_location_(IIY)collected.2018.11.24.edit[UTS]_{POM}.doc" "C:\uniform\rack_locationcollected.2018.11.24.doc"

为了了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • [call /? ...解释%~dp0 ...驱动器和参数0的路径,参数0是包含此批处理文件的目录的完整路径,始终以反斜杠结尾。
  • copy /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • rem /?
  • set /?
  • setlocal /?

另请参阅有关Using command redirection operators的Microsoft文章,以获取>nul2>nul的解释。

© www.soinside.com 2019 - 2024. All rights reserved.