2011-08-31 48 views
2

我想使用Windows自帶的shell腳本加載該文件:Windows shell腳本到CSV導入到變量

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 

有沒有這樣做的方法?我無法讓我的腳本纏繞在「轉到下一行」或「一次處理一行」的概念上。

謝謝, 克里斯

+0

您是否考慮過使用Powershell?有一個很好的Import-Csv命令,以及一個很好的For-Each-Object命令,可以像這樣循環。 –

回答

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