I am attempting to set each line of a CSV file to its own variable.
Trying to set a variable to the value of %%a, in line of the for, creates an empty variable.
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% ))
Sample data from .csv (from comments)
"name"
"Access Control Assistance Operators"
"Account Operators"
"Administrators"
"Allowed RODC Password Replication Group"
"Backup Operators"
This piece running has an output of
"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.
The double quoted text output is the desired string grabbed from the CSV, but yet when echoing the variable that has been set to the output.. it remains empty.
I've tried different formatting & call set/set.
I seem to only be able to get the correct output when the for loop has finished, but of course I only receive the last line of the file.
CodePudding user response:
@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
Result
"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 DELAYEDEXPANSION link
set "gp!ai!=!gpa!"
could be set "gp!ai!=%%a"- See
set /?
from the prompt for documentation aboutset /a
(or many examples on SO)