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

标签 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

有没有办法做到这一点?我无法让我的脚本围绕“转到下一行”或“一次处理一行”概念。

谢谢, 克里斯

最佳答案

你可以;

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

关于将 csv 加载到变量中的 Windows Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7256984/

相关文章:

python - 如何在整个目录中查找具有特定扩展名的文件,然后复制到另一台服务器

javascript - 在批处理文件中嵌入 JS 代码

java - Jenkins 构建不会在应该失败的时候失败

c# - 用c#开发一个窗口的服务,用于将4个不同位置的数据备份到一个位置?

c++ - Py ILM Base 无法在 Windows 上编译

python - 在 shell 脚本中读取用户输入

linux - Shell:从远程主机获取数据并执行一些其他命令

batch-file - 通过用零填充来批量重命名连续文件

windows - 是否可以运行 Docker Windows 容器并编辑注册表(regedit)?

windows - 如何隐藏触摸屏上的闪光灯?