Chiri Vulpes
Chiri Vulpes

Reputation: 488

Getting a for loop working

I'm trying to split apart the system path variable with batch. This is what I have:

@echo off
setlocal EnableDelayedExpansion
set npath=%path%
set i=0
echo %npath% > temp
for /f "delims=;" %%a in (temp) do (
    echo [!i!]: %%a
    set /a i=!i!+1
)

When it runs, however, it only runs the loop a single time. Instead of my expected output, a list of the directories in PATH, it just outputs a single one:

stupid lazy batch

What's happening? Am I doing something wrong? I've tried using path as a string, I've tried changing the number of tokens, I've tried like everything. Do I just not understand how for loops work in batch?

Upvotes: 1

Views: 57

Answers (1)

npocmaka
npocmaka

Reputation: 57242

try to iterate the items in the path with plain FOR loop (and you need to set quotes to prevent space collisions):

@echo off

setlocal enableDelayedExpansion
set "_path="!path:;=" "!""
rem echo %_path%
set i=0
for %%a in (%_path%) do (
    echo [!i!]: %%~a
    set /a i=!i!+1
)
endlocal

Upvotes: 1

Related Questions