I have a batch file which reads and set values from a text file. But the text file contains tabs for different variables
Batch file command:
for /f "tokens=* delims=<TAB>" %%x in (input.txt) do set %%x
Text File(input.txt):
a=one b=two c=three d=four
But the variables are not being set properly.
Two points here:
The default delims=
value include the space and Tab as delimiters, so you don't have to include a delims=
option, unless you want to ignore the spaces as delimiters!
Your tokens=*
option define one token letter in your for
command ( %%x
in this case) that contain all tokens in the line. If you want to get four tokens you need to specify tokens=1-4
, start the tokens-letter with another one, and process each token accordingly:
.
for /f "tokens=1-4" %%a in (input.txt) do (
set "%%a" & set "%%b" & set "%%c" & set "%%d"
)
this works even, if your textfile has several lines (with different / unknown number of tokens):
@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%i in (input.txt) do (
set line=%%i
for %%x in ("!line: =","!") do set %%x
)
the trick is: by enclosing a string in quotes, delimiter chars are not treated as delimiters any more. So adding a quote at the beginning and end of the string AND replacing each TAB
with "quote comma quote" changes the string to:
"a=one","b=two","c=three","d=four"
which can happily be processed with for %%x ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.