-3

This is a batch script to run httpserver in every drive path if drive exists to list its files. in this script set /a %port% is not working.

@echo off
set /a port=8080
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%i: (
cd /d %%i:\
start SimpleHTTPServer.exe %port%
set /a port+=1
)
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112

1 Answers1

1

This has to do with how environment variables are expanded inside of blocks in a batch file. To override this behavior, use setlocal enabledelayedexpansion and use ! instead of % to reference the environment variable.

@echo off
setlocal enabledelayedexpansion
set /a port=8080
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%i: (
cd /d %%i:\
start SimpleHTTPServer.exe !port!
set /a port+=1
)
Anon Coward
  • 9,784
  • 3
  • 26
  • 37