0

We have hundreds of *.pdf files in a folder. Each *.pdf will have naming convention as ABC_100_filename.pdf or ABC_200_filename.pdf or ABC_300_filename.pdf.

In the batch script we call *.exe file as follows:

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.

Expected result: test.exe 100 should be called for file ABC_100_Testfile.pdf.

We mistakenly call test.exe 200 instead of this file execution.

How to write an IF - ELSE IF condition to call the .exe file in a batch script.

if %str1:~0,7%==ABC_100 (call test.exe 100 filename)
pause
if %str2%==ABC_200 (call test.exe 200 filename)
Mofi
  • 46,139
  • 17
  • 80
  • 143
goofyui
  • 3,362
  • 20
  • 72
  • 128
  • Why use the "old" windows batch script? Consider the alternative MS tech. called Powershell: https://learn.microsoft.com/en-us/powershell/scripting/powershell-scripting?view=powershell-5.1 – NoChance Aug 20 '17 at 17:56

2 Answers2

5
  • There is no need to call an .exe (only other batch files to return lateron)
  • provided the number to pass to test.exe is enclosed in _ use a parsing for.

@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.

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

You need a for loop with delayed variable expansion. 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.

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. For each line of output it will set %i to its value, unroll the loop body by substituting its values and executing it. 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. 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. The next two statements extract the number 100 and the file name foo from the variable pdf, and finally the executable is called. Now, for the lines remaining in the output, the loop body will be unrolled and executed again with different values in %i.

Also note that when run in a file you need to put %%i, when run at the prompt you put %i.

yacc
  • 2,915
  • 4
  • 19
  • 33