1

What I want to do is, I have a text file (List.txt) and through batch script I want to read (line by line) the text file and save the line in some variable for later use. Following the the batch script that I am trying, but don't know the reason why isn't it working?

@echo off
set _filePath= List.txt

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%_filePath%) do  (
set _var = %%a
echo !_var!
)

List.txt file has:

abc|def
1234|defg
abcde|98745

and the output is:-

ECHO is off
ECHO is off
ECHO is off

what I want is:

abc|def
1234|defg
abcde|98745

Can someone help me out with it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
swati
  • 11
  • 2
  • 2
    Duplicate of [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](http://stackoverflow.com/questions/26386697/why-is-no-string-output-with-echo-var-after-using-set-var-text-on-comman) Use `set _var=%%a` without the spaces around the equal sign and your batch code will work. – Mofi Dec 07 '14 at 18:29
  • `set _filePath= List.txt` is also not correct as your list file most likely does not start with a space character in file name. But this mistake is corrected automatically. – Mofi Dec 07 '14 at 18:37

2 Answers2

1
set _var = %%a

Simple problem - set is sensitive to Spaces on both sides of the =.

Use set "var=value"

which excludes trailing spaces from the value assigned.

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

Although the comments are correct, the author hasn't closed the question, so perhaps needs more detail. There is still also another fault in the batch script for dealing with blank lines in the file (which causes the echo to give the wrong output). The fixed version of the batch file would be:

@echo off
set _filePath=List.txt
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%_filePath%) do  (
    set _var=%%a
    echo;!_var!
)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129