0

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.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
  • Plwase edit-in a sample of your `.csv` file. – Magoo Dec 21 '22 at 23:56
  • This is a sample of the csv file. `"name" "Access Control Assistance Operators" "Account Operators" "Administrators" "Allowed RODC Password Replication Group" "Backup Operators"` I don't think it is the CSV file, as I can assign the final value of the %%a after the for loop has run. – SquishyKitty Dec 22 '22 at 00:01
  • The sample data you have posted does not yield the output you report. Please `edit` your post to show us sample data that will provide the report. Obfuscate names or other sensitive data – Magoo Dec 22 '22 at 00:10

1 Answers1

0
@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" to retain the quotes in the assigned value, or set "gp!ai!=%%~a" to remove the quotes from the assigned value.
  • See set /? from the prompt for documentation about set /a (or many examples on SO)
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • It work beautifully, but I have a piece of data with the & symbol in it, but seem to lose it in the `set "gp!ai!=%%a"` The piece of the CSV file is similar to: `"NAME & USER"` It seems to just skip this entire line. I should of provided a similar sample of this in the first place, but did not realize it would be an issue. – SquishyKitty Dec 22 '22 at 02:12
  • I added the `"NAME & USER"` line to my test file, and it worked perfectly. Note that using `set "gp!ai!=%%~a"` would remove the quotes from the value assigned. This makes the value more usable as inconveniently-placed quotes do not thenneed to be removed. – Magoo Dec 22 '22 at 08:56