37Scheper
37Scheper

Reputation: 19

If statement inside For loop in batch script

I am developing a batch script to make changes to multiple servers that may or may not contain a particular subfolder under D:\apps\domain. There are multiple combinations of possible domain subfolders. My logic requires: if domain\folder1 exists then do command1 if domain\folder2 exists then do command2 and so on

I am using the following...

for /f "tokens=*" %%Z in ('dir /a:D /b "D:\apps\domain"') do (
if %%Z="PGBWAHD_NA" 
command1

but get syntax errors on first IF statement.
Error message .... =PGBWAHD_NA was unexpected at this time. D:\apps\hawk_schtasks>if /I %Z=PGBWAHD_NA The subfolder PGBWAHD_NA does exist, but the script errors and quits.

Upvotes: 0

Views: 162

Answers (1)

npocmaka
npocmaka

Reputation: 57332

you rather need:

for /f "tokens=*" %%Z in ('dir /a:D /b "D:\apps\domain"') do (
  if "%%~Z" == "PGBWAHD_NA" (
  command1
 )
)

in batch files valid comparisons are == , EQU , LSS , LEQ , GTR , GEQ and quotes are also evaluated.

Upvotes: 2

Related Questions