简体   繁体   English

Windows批处理脚本中如何调用IF-ELSE IF条件?

[英]How to call IF - ELSE IF condition in a Windows batch script?

We have hundreds of *.pdf files in a folder. 一个文件夹中有数百个* .pdf文件。 Each *.pdf will have naming convention as ABC_100_filename.pdf or ABC_200_filename.pdf or ABC_300_filename.pdf . 每个* .pdf的命名约定为ABC_100_filename.pdfABC_200_filename.pdfABC_300_filename.pdf

In the batch script we call *.exe file as follows: 在批处理脚本中,我们按以下方式调用* .exe文件:

call test.exe 100  filename
call test.exe 200  filename
call test.exe 300  filename

As there is no IF condition to validate the file, we incorrectly load the files with incorrect id. 由于没有IF条件可以验证文件,因此我们错误地加载了ID不正确的文件。

Expected result: test.exe 100 should be called for file ABC_100_Testfile.pdf . 预期的结果:应该为文件ABC_100_Testfile.pdf调用test.exe 100

We mistakenly call test.exe 200 instead of this file execution. 我们错误地调用test.exe 200而不是此文件执行。

How to write an IF - ELSE IF condition to call the .exe file in a batch script. 如何编写IF-ELSE IF条件以在批处理脚本中调用.exe文件。

if %str1:~0,7%==ABC_100 (call test.exe 100 filename)
pause
if %str2%==ABC_200 (call test.exe 200 filename)
  • There is no need to call an .exe (only other batch files to return lateron) 无需call .exe(仅其他批处理文件可以稍后返回)
  • provided the number to pass to test.exe is enclosed in _ use a parsing for. 如果要传递给test.exe的数字包含在_使用解析。

@Echo off
For /f "tokens=1-2* delims=_" %%A in (
    'Dir /B/A-D "*_*_*.pdf"'
) Do test.exe %%B "%%A_%%B_%%C"

A file name with a fixed structure delimiting the elements with distinct chars can easily be parsed with a for /f (see ss64.com/nt/for_f ) distributing the delimited tokens to adjacent for meta variables. 可以使用for / f(请参阅ss64.com/nt/for_f )轻松解析具有固定结构的文件名,这些文件用不同的字符分隔元素,这些元素将分隔的标记分配给相邻的meta变量。

file name    : ABC_100_filename.pdf
delims       :    _   _
tokens       :  1 _ 2 _ * (rest)
for variable : %%A %%B %%C

You need a for loop with delayed variable expansion . 您需要一个带有延迟变量扩展的for循环。 This will be necessary every time a loop body needs to substitute variables other than that defined by the header. 每当循环主体需要替换标头定义的变量以外的其他变量时,这将是必要的。

SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%i in ('dir /b *.pdf') do (set pdf=%%~ni & set str=!pdf:~4,3! & set fname=!pdf:~8! & test.exe !str! !fname!)

I'm not sure why you require to call the executable but I think you get the idea. 我不确定为什么您需要call可执行文件,但我想您知道了。

Multiline version: 多行版本:

SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%i in ('dir /b *.pdf') do (
  set pdf=%%~ni
  set str=!pdf:~4,3!
  set fname=!pdf:~8!
  test.exe !str! !fname!
)

Now some step by step explanation: 现在逐步解释一下:

As delayed expansion is off by default we first need to turn it on in line 1. The for loop will run the command in quotes, here dir /b *.pdf . 由于默认情况下延迟扩展是关闭的,因此我们首先需要在第1行将其打开。for循环将在引号中运行命令,此处为dir /b *.pdf For each line of output it will set %i to its value, unroll the loop body by substituting its values and executing it. 对于输出的每一行,它将%i设置为其值,通过替换其值并执行来展开循环体 This is the place where we need the delayed expansion. 这是我们需要延迟扩张的地方。

Let's say the first line is ABC_100_foo.pdf , so is the value of %i . 假设第一行是ABC_100_foo.pdf%i的值也是。 For this line the loop body now looks like this: 对于此行,循环主体现在如下所示:

  set pdf=ABC_100_foo
  set str=!pdf:~4,3!
  set fname=!pdf:~8!
  test.exe !str! !fname!

The expression %%~ni has been replaced with the file name without extension. 表达式%%~ni已替换为没有扩展名的文件名。 The next two statements extract the number 100 and the file name foo from the variable pdf, and finally the executable is called. 接下来的两个语句从变量pdf中提取数字100和文件名foo ,最后调用可执行文件。 Now, for the lines remaining in the output, the loop body will be unrolled and executed again with different values in %i. 现在,对于输出中剩余的行,将展开循环主体并以%i中的不同值再次执行该循环主体。

Also note that when run in a file you need to put %%i , when run at the prompt you put %i . 另请注意,在文件中运行时,需要放置%%i ,在提示符下运行时,需要放置%i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM