用于处理以特定字符

时间:2017-06-11 14:38:37

标签: batch-file cmd

我正在尝试运行一个批处理文件,该文件从特定文件夹中调用三个文件,每个文件以不同的字符集结尾。我可以将路径映射到文件夹没有问题,代码可以工作,但我无法在我要查找的三个特定结束字符之前创建一个通配符。文件夹中的文件名长度是可变的,我不知道它们是由前一个批处理文件创建的,但都以特定的三个字符(_BX.tif)结束。我使用了下面的代码但是它在_B4.tif之前没有识别*,所以它实际上在cmd行中打印了*。

for /d %%a in ("%mypath%\*") do (
script.exe "%%a\*_B4.tif" "%%a\*_B3.tif" "%%a\*_B2.tif" "%%a\*_output.tif"
)

是否有另一种方法可以添加通配符,以便文件名可以更改,但它只搜索最后三个字符?

非常感谢

2 个答案:

答案 0 :(得分:0)

我想通过迭代其中一组文件(结尾为_B4.tif),提取文件名的前导部分,然后检查相应的_B3.tif和_B2,我能够做你想做的事。 .tif存在:

@echo off
setlocal
:main
set mypath=%1
for %%f in (%mypath%\*_B4.tif) do call :get_prefix_and_execute_script "%%f"
exit /b

REM subroutine to extract prefix of filename and call script.exe
:get_prefix_and_execute_script
set filename=%1
echo Filename: %filename%
REM extract the first portion of the filename, excluding the remaining
REM 8 characters for: _B4.tif" (also excluding trailing quotation mark).
REM Start at 1 rather than 0 to exclude the leading quotation mark.
set prefix=%filename:~1,-8%
echo Prefix: %prefix%
set B4="%prefix%_B4.tif"
set B3="%prefix%_B3.tif"
set B2="%prefix%_B2.tif"
if not exist %B3% echo %B3% not found! & exit /b
if not exist %B2% echo %B2% not found! & exit /b
REM all 3 files exist...so call script.exe
set output="%prefix%_output.tif"
script.exe %B4% %B3% %B2% %output%
echo.
exit /b

for / D命令用于迭代目录,而不是文件。所以在这里我只使用了一个平原,没有开关。

这个脚本应该处理包含空格的文件名,如果这是一个问题。

这个脚本假设_B4.tif,_B3.tif和_B2.tif都存在。例如,如果您有:file_B3.tif和file_B2.tif,但file_B4.tif不存在,则此脚本将对此保持沉默。

我实际上没有script.exe,所以我将用以下代码替换:

echo script.exe %B4% %B3% %B2% %output%

现在,如果我创建以下文件:

echo. > test_B4.tif
echo. > test_B3.tif
echo. > test_B2.tif
echo. > "test me_B4.tif"
echo. > "test me_B3.tif"
echo. > "test me_B2.tif"

注意:这些实际上不是* .tif文件,因为它们只包含换行符。这仅用于演示脚本如何处理文件名。

这也是一些不符合模式的:

echo. > abc.tif
echo. > other.txt
echo. > xyz_B4.tif
echo. > xyz_B3.tif

目录列表显示:

06/11/2017  01:06 PM    <DIR>          .
06/11/2017  01:06 PM    <DIR>          ..
06/11/2017  01:06 PM                 3 abc.tif
06/11/2017  12:55 PM               851 filesets.bat
06/11/2017  01:06 PM                 3 other.txt
06/11/2017  12:49 PM                 3 test me_B2.tif
06/11/2017  12:49 PM                 3 test me_B3.tif
06/11/2017  12:49 PM                 3 test me_B4.tif
06/11/2017  12:49 PM                 3 test_B2.tif
06/11/2017  12:49 PM                 3 test_B3.tif
06/11/2017  12:49 PM                 3 test_B4.tif
06/11/2017  01:06 PM                 3 xyz_B3.tif
06/11/2017  01:06 PM                 3 xyz_B4.tif
              11 File(s)            881 bytes

我将批处理脚本保存为:filesets.bat

当我运行当前目录时,我得到了这个输出:

C:\test>filesets .
Filename: ".\test me_B4.tif"
Prefix: .\test me
script.exe ".\test me_B4.tif" ".\test me_B3.tif" ".\test me_B2.tif" ".\test me_output.tif"

Filename: ".\test_B4.tif"
Prefix: .\test
script.exe ".\test_B4.tif" ".\test_B3.tif" ".\test_B2.tif" ".\test_output.tif"

Filename: ".\xyz_B4.tif"
Prefix: .\xyz
".\xyz_B2.tif" not found!

答案 1 :(得分:0)

假设每个目录中只有一个文件*_B4.tif,文件*_B3.tif*_B2.tif与[{1}}具有相同的名称*和文件*_B4.tif也应该使用相同名称的部分*_output.tif,您可以试试这个,因为*部分本身不包含任何*

_

如果rem // Enumerate the directories: for /D %%a in ("%mypath%\*") do ( rem // Get file `*_B4.tif` and extract the `*` part: for /F "delims=_ eol=_" %%b in ('dir /B "%%~a\*_B4.tif"') do ( rem // Copy the `*` part from file `*_B4.tif` to all the other files: script.exe "%%~a\%%~b_B4.tif" "%%~a\%%~b_B3.tif" "%%~a\%%~b_B2.tif" "%%~a\%%~b_output.tif" ) ) 的名称部分*自己包含*_B4.tif,则方法会变得有点复杂:

_
相关问题