Windows Shell脚本将csv加载到变量中

时间:2011-08-31 12:37:18

标签: windows shell batch-file

我正在尝试使用Windows内置shell脚本来加载此文件:

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

像这样:

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

哪个不起作用,但我希望它像这样:

: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

有没有这样做的方法?我无法将我的脚本包裹在“转到下一行”或“一次处理一行”的概念中。

谢谢, 克里斯

1 个答案:

答案 0 :(得分:3)

你可以;

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

或使用%n表示法;

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 ...