设置变量为CSV文件行问题



我试图将CSV文件的每行设置为自己的变量。

尝试将变量设置为%%a的值,在for语句的行中,将创建一个空变量。

for /f "usebackq tokens=1 delims=," %%a in ("%location1%%location2%autogroup.csv") do (if %%a=="name" (echo.) else (set "gpa=%%a" && set /A "ai=%ai%+1" && call set "gp%ai%=%gpa%" && echo %%a && echo %gpa% ))

.csv的样本数据(来自注释)

"name"
"Access Control Assistance Operators"
"Account Operators" 
"Administrators" 
"Allowed RODC Password Replication Group" 
"Backup Operators"

这段运行的输出为

"Access Control Assistance Operators"
ECHO is off.
"Account Operators"
ECHO is off.
"Administrators"
ECHO is off.
"Allowed RODC Password Replication Group"
ECHO is off.
"Backup Operators"
ECHO is off.

双引号文本输出是从CSV中抓取的所需字符串,但是当回显已设置为输出的变量时…

我试过不同的格式&叫/集合。

我似乎只有在for循环完成时才能得到正确的输出,但当然我只接收到文件的最后一行。

@ECHO OFF
SETLOCAL enabledelayedexpansion
for /f "usebackq tokens=1 delims=," %%a in ("q74882878.txt") do (
if %%a=="name" (echo.
) else (
set "gpa=%%a"
set /A ai+=1"
set "gp!ai!=!gpa!" 
echo %%a 
echo !gpa! 
)
)
SET gp
GOTO :EOF

结果

"Access Control Assistance Operators"
"Access Control Assistance Operators"
"Account Operators"
"Account Operators"
"Administrators"
"Administrators"
"Allowed RODC Password Replication Group"
"Allowed RODC Password Replication Group"
"Backup Operators"
"Backup Operators"
gp1="Access Control Assistance Operators"
gp2="Account Operators"
gp3="Administrators"
gp4="Allowed RODC Password Replication Group"
gp5="Backup Operators"
gpa="Backup Operators"

#1 FAQ:delayedexpansion

Stephan's delayeexpansion link

  • set "gp!ai!=!gpa!"可以是set "gp!ai!=%%a",保留赋值中的引号,也可以是set "gp!ai!=%%~a",去掉赋值中的引号。
  • 从提示符中查看set /?关于set /a的文档(或SO上的许多示例)

相关内容

  • 没有找到相关文章