qw3n
qw3n

Reputation: 6334

How to open a series of folders numbered 01-18

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
)

Upvotes: 0

Views: 73

Answers (2)

MC ND
MC ND

Reputation: 70923

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"
)

Upvotes: 2

Rafael
Rafael

Reputation: 3112

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
)

Upvotes: 1

Related Questions