tags:

views:

138

answers:

2

I am working on some batch file. I need to read name from some text file. Let me explain it

I have one file File.txt, which has entry like ”FirstName=John”. Now my batch file should read text ”John” from the file and I should be able store ”John” in some variable too.

But with following code, If I use “delims==”,I can get ”FirstName” text stored in some variable but not ”John”.

for /F “delims==” %%I in (File.txt) do set Title=%%I

echo %Title%

But Is there any way where I can get “John” from my File.txt and store it with in my For loop ?

+2  A: 
@echo off
setlocal
for /F "tokens=1,2 delims==" %%a in (File.txt) do set Title=%%b
echo %Title%

Does extract the first name value into Title... but only for the last line containing "Firstname"!

@echo off
setlocal
for /F "tokens=1,2 delims==" %%a in (File.txt) do (
    set t=%t% %%b
)
echo %t:Firstname=%

Does concatenate all first names found.

VonC
A: 

You problem is that "John" is the second token found, "Firstname" being the first token found. You have many options.

  • You can use "tokens=2" to skip the first token and get only the second token into variable %%I. No other variable is generated.
  • You can use variable %%J (which is automatically generated) which contains the second token. %%K would contain the 3rd token and so forth.
  • You can use "tokens=2*" to skip the first token and get all following tokens into variables I, J, K, etc.
Philibert Perusse