0

I have a csv file such as below

Name,Test User1  
Name2,TstUser Two  
Name3,Test User Three

I need to extract the Test User1 in to a Variable in a batch file,and other variables similarly to different variables.

tukan
  • 17,050
  • 1
  • 20
  • 48
kpk1
  • 7
  • 5

1 Answers1

0
@echo off
setlocal
for /f "tokens=1,* delims=," %%a in (file.csv) do set "_%%a=%%b"
echo your variables are:
set_

Output with your example data:

your variables are:
_Name=Test User1
_Name2=TstUser Two
_Name3=Test User Three

Edit to get a single line/variable only:

@echo off
setlocal
for /f "tokens=2 delims=," %%a in ('type file.csv^|findstr /bi "Name,"') do set "Name=%%a"
echo %Name%
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • My requirement is to get just the **Test User1** in to `%%a` – kpk1 Apr 21 '20 at 12:54
  • Sorry, I understood `and other variables similarly to different variables` wrong then. See my edit (I left the original code, because it may help future searchers) – Stephan Apr 21 '20 at 13:02
  • `for /f "tokens=2 delims=," %%a in ('type file.csv^|findstr /bi "Name,"') do set "Name=%%a"` This solution did not help me as i only get 'Test' as the output and not **Test User1 ** – kpk1 Apr 21 '20 at 13:41
  • That can only happen, if you either have `...delims=, "` or no delims at all (relying on the standard delimiters). `"tokens=2 delims=,"` correctly gives `Test User1`. – Stephan Apr 21 '20 at 13:48
  • Sorry My bad , your solution is absolutely works for me.Thank you – kpk1 Apr 21 '20 at 13:56