For Batch in batch language

时间:2015-01-04 23:33:25

标签: csv batch-file

所以我有一个类似这样的CSV文件:

ComputerName,UserID,192.168.0.xx
ComputerName,UserID,192.168.0.xx

我正在尝试编写一个批处理循环脚本,它将读取脚本中的IP并ping它们,这是我到目前为止所做的:

setlocal Disable DelayedExpansion

for /f "tokens=1,2,3 delims=" %%a in (test.csv) do(

Set line=%%c
ping %line%

)

设置变量有效,但似乎我无法从批处理文件中读取任何内容,因为我收到的错误是:

%%a was unexpected at this time 

我想知道如何才能解决这个问题或者我在这里做错了什么

2 个答案:

答案 0 :(得分:0)

delims=位意味着没有分隔符,因此整个字符串被视为一条完整的行,而不是像你想要的那样在逗号上拆分。将其更改为

setlocal EnableDelayedExpansion

for /f "tokens=1,2,3 delims=," %%a in (test.csv) do (
    Set line=%%c
    ping !line!
)

答案 1 :(得分:0)

有一些问题,但你非常接近。我已更新您的脚本并注意到更正。

REM You want to _Enable_ delayed expansion. This is a single word.
setlocal EnableDelayedExpansion

REM Your delimiter needs to be a ,
for /f "tokens=1,2,3 delims=," %%a in (test.csv) do (
    Set line=%%c
    REM You need to access the line value enclosed in !'s since it is delayed expansion.
    ping !line!
)

或者,您可以从命令提示符处执行此操作(不需要批处理文件):

FOR /F "tokens=1,2,3 delims=," %a in (test.csv) do PING %c