0

I was trying to open a folder on every lab computer using a batch script. The computers are labeled 01,02,03-18. I didn't think there was a way to convert the number from 1 to 01 so I used an if statement. But I am getting an error that says 9 was unexpected at this time

@echo off
setlocal enabledelayedexpansion
SET "z=0"
SET "n=9"
for /L %%x in (1,1,18) do (
  SET v=%%x
  IF %v% LEQ %n%
  (
    SET num=%z%%v%
  ) ELSE (
    SET num=%v%
  )
  start "" "\\lab-!num!\
  pause
)
qw3n
  • 6,236
  • 6
  • 33
  • 62

2 Answers2

2

You have problems with the placement of th parenthesis (see here), and an inconsistent usage of the delayed expansion (you are using !num! but not !v!, the two variables that change inside the code block) but the code can be simplified by including the padding in the values of the for loop

for /l %%x in (1001, 1, 1018) do (
    set "num=%%x"
    start "" "\\lab-!num:~-2!\share\folder"
)
Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Very clever I wish I thought of that. My knowledge of batch is spotty, and I never can remember how to deal with variables. – qw3n Sep 16 '14 at 17:52
1

When delayed expansion is enabled, use exclamation marks inside for loops. And don't forget that open parenthesis must be on the same line as the if

@echo off
setlocal enabledelayedexpansion
SET "z=0"
SET "n=9"
for /L %%x in (1,1,3) do (
  SET v=%%x
  IF !v! LEQ !n! (
    SET num=!z!!v!
  ) ELSE (
    SET num=!v!
  )
  start \\lab-!num!\
  pause
)
Rafael
  • 3,042
  • 3
  • 20
  • 36