0

I have problem with batch. I want to create new dir and save file to it everytime batch is opened

This is sample of my code

SET i=0
FOR /L %%i IN (0,1,100) DO (
    IF NOT EXIST res\%%i (
        mkdir res\%%i
        GOTO run
    )
)
:run
start X.exe /stext res\%i%\X.txt

Creating folders works properly. I have problem with

start X.exe /stext res\%i%\X.txt

Thanks

tburton12
  • 87
  • 2
  • 9

1 Answers1

0

%i% and %%i are totally different variables. %i% is a regular batch variable, while %%i only exists within the for loop. Once you call goto run, it no longer exists.

You can store %%i in a variable and then call it using delayed expansion, because you're setting it inside of a code block (a set of parentheses).

@echo off
setlocal enabledelayedexpansion
SET i=0
FOR /L %%i IN (0,1,100) DO (
    IF NOT EXIST res\%%i (
        set i=%%i
        mkdir res\!i!
        GOTO run
    )
)
:run
start X.exe /stext res\!i!\X.txt
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • But delayedexpansion is only needed when you output the var `i` inside the code block, setting works. So you could use `mkdir res\%%i` and omit delayedexpansion. –  Jul 22 '17 at 23:47
  • @LotPings - In this particular example, yes. But I was trying to avoid them coming back with a later, unrelated question about why their variable isn't updating inside of a for loop. – SomethingDark Jul 23 '17 at 00:22