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.

2
  • No i need to fetch the data with spaces Commented Apr 21, 2020 at 11:53
  • 2
    Then get rid of the space in the delims section of the accepted answer. Commented Apr 21, 2020 at 13:09

1 Answer 1

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%
Sign up to request clarification or add additional context in comments.

5 Comments

My requirement is to get just the Test User1 in to %%a
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)
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 **
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.
Sorry My bad , your solution is absolutely works for me.Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.