2

I am trying to use Windows built-in shell script to load this file:

hostname1,host_specific_file1
hostname2,host_specific_file2
hostname3,host_specific_file3
.
.
.

Like this:

for /f "tokens=1,2* delims=," %%i in (host-info.txt) do set clientName=%%i; set fileLoc=%%j

Which doesn't work but I want it to go like this:

:loop
For each line, Set the current_hostname=hostnamex and Set the current_file=host_specific_filex
And THEN
DO STUFF
Goto next line, Goto loop

Is there a method for doing this? I can't get my script wrapped around the "Goto next line" or "Handle one line at a time" concept.

Thanks, Chris

1
  • Have you considered using Powershell? There's a good Import-Csv command, and a nice For-Each-Object command that is meant for looping like this.
    – Jennifer S
    Aug 31, 2011 at 12:44

1 Answer 1

3

You can;

echo off
setlocal enabledelayedexpansion

for /f "tokens=1,2* delims=," %%i in (host-info.txt) do (
    set clientName=%%i
    set fileLoc=%%j
    call:handler
)
goto:eof

:handler
    echo client name is !clientName! location is !fileLoc!
goto:eof

Or Using %n notation;

echo off
setlocal enabledelayedexpansion

for /f "tokens=1,2* delims=," %%i in (host-info.txt) do call:handler %%i %%j
goto:eof

:handler
    echo client name is %1 location is %2 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.