Read variables from a text file in a batch file using For /F command

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Read variables from a text file in a batch file using For /F command

Post by MigrationUser »

13 Nov 2010 01:35
Addcom

Am new here and have a problem which am finding it hard to solve.
Basically trying to read a unique string in a text file a append the value to a variables in my batch file.

The text file is as follows:
Mytext.txt
:Rem Filename: Mytext.txt
:Rem Description my variables are set from this file
Set EN-UK sfilename=M10.txt
Set EN-UK sCabfile=enCbafile.cab
Set FR-FR sfilename=french file .txt
Set FR-FR sCabfile=Cbafilefr-fr.cab
Set DE-DE sfilename=....
Set DE-DE sCabfile=
The batch file is as follows:
Mybatcfile.cmd

Code: Select all

@echo off & setLocal EnableDELAYedeXpansion
for /f "eol=: tokens=3 delims= " %%a in ('find /v ":" ^< C:\Mytext.txt  ^| find "EN-UK"') do (
%%a
I keep getting Env variable is not defined and the values are blank
I just want to read the lines without ":" at the begining and only line containing EN-UK and set sfilename, sCabfile as variables, the list can be 10 lines per language or reqion ie EN-US as well as other langs, this is just example.

Can also restructure the text file if it makes the solution easier
Any help will be appreciable., thanks in advance

----------------------------

#2 13 Nov 2010 14:57
RG


Addcom,
Here's one way:

Code: Select all

@echo off & setLocal
for /f "eol=: tokens=3 delims= " %%a in ('find "EN-UK" C:\Mytext.txt') do (
   echo.%%a
)
pause
endlocal 
Main problems were:
1. No closing paren (maybe you just left it off the post though)
2. %%a should have been echo.%%a
Notes:
1. You don't need to enabledelayedexpansion. I would avoid it if not needed because it causes major grief if there is an "!" in your text file.
2. You don't need the find /v ":" because you are already excluding the lines that start with ":" with the eol=:
3. I would reconsider the placcement of the text file rooted at C:. This may work on XP and older OS's. But when you get to Vista and newer with UAC turned on you will have to be granting admin permission to write those files.

Last edited by RG (13 Nov 2010 14:57)

Windows Shell Scripting and InstallShield

----------------------------

#3 13 Nov 2010 17:20
Addcom


Wonderful it worked, so please to see your answer and suggestion, couldn't wait to try it
I read some of your post and you doing a great service to the forum !!!

But I have another 2 questions to ask
Q1, if the return text after the equal sign has a space in the string it only returns the 1st word ie sTmeZone=GMT Standard Time
Q2 it echos sfilename=M10.txt, how do I change it to set sfilename as a variable and its value M10.txt
and also all sudsequent once in the ie sTmeZone=GMT Standard Time, etc ...

Thanks again in advance

----------------------------

#4 13 Nov 2010 18:06
RG


Sorry, I should have been a bit more careful.
Q1: to handle the space in my solution:

Code: Select all

@echo off & setLocal
for /f "eol=: tokens=3* delims= " %%a in ('find "EN-UK" C:\Mytext.txt') do (
   echo.%%a%%b
)
pause
endlocal
But my solution is not what you wanted. I think you want this Q2:
To use this you will need to remove the Set in your text file. Is that possible?

Code: Select all

@echo off & setLocal
for /f "tokens=*" %%a in ('find /v ":" C:\Mytext.txt ^| find "EN-UK"') do (
   call set "%%a" 
)
REM Next line is just to display results
set EN
pause
endlocal 
Windows Shell Scripting and InstallShield

----------------------------

#5 13 Nov 2010 19:22
Addcom


Thanks again, I did remove the "Set" in the text file and it is displaying the right result as follows
EN-UK Sfilename=M10.txt
EN-UK sTimezone=GMT Standard Time etc,,

which eactly as in text file, now how do I set Sfilename, sTimezone etc as a varaible a which also contain the correct values as in text file..

I can also structure the textfile as I want at this stage to get the batch file working
The idea about the script is only to add to text file when a new PC is introduced instead modiying the batch file.
thanks a lot, nearly there.

----------------------------

#6 13 Nov 2010 21:00
RG


I don't quite understand... do you have multiple text files (1 each for EN, FR, DE)?
Or were you going to change the batch file to use one of EN, FR, DE?
If so, could we pass in an argument to specify the desired EN-UK, FR-FR, DE-DE?
At any rate... I think this is what you want for now:

Code: Select all

@echo off & setLocal
for /f "tokens=1*" %%a in ('find /v ":" C:\Mytext.txt ^| find "EN-UK"') do (
   REM next line is just for display
   echo.language=%%a %%b
   call set "%%b"
)
REM Next 2 lines  are just to display results
set sfilename
set sCabfile
pause
endlocal
If you want to pass in an argument to specify the desired EN-UK, FR-FR, DE-DE, then just replace "EN-UK" with "%~1"

Windows Shell Scripting and InstallShield

----------------------------

#7 14 Nov 2010 18:08
Addcom


Thanks very much, it worked perfecty, much appreciated.
To answer the questions you posted
1. Originallyl I planned to create a file of EN-Uk and then changed it to per PC and the supported languages
2. now I have a file per Pc with all the langauges and pass the %sModule% and %SLANG% to the batch file
ie mybatch.cmd M11 EN-UK where %1 is the Module and %2 is the LANG
Thanks again been working all weekend to get my head around the For F loop etc
Seeing your answer was like a child waiting for his / her Xmass presents (lol)

Hope you had a nice weekend, thanks again

----------------------------

#8 19 Dec 2010 14:52
sarbjitsinghgill


I use following technique

Code: Select all

Set Database=GAMING
Set MailList=Run_Maillist.txt
Call :ReadMailList

:ReadMailList
           This sets esum ( email for summary), eall ( email for all) and eerr ( email for error condition) varibles to be used for mail out via blat.exe.

::----------------------------------------:ReadMailList
:ReadMailList
Set "esum=" & Set "eall=" & SET "eerr="
SetLocal EnableDelayedExpansion
for /f "tokens=1,2,*" %%x in (%MailList%) do (
 if /i "%%x"=="%DATABASE%" if /i "%%y"=="SUM" if "%%z" neq "" if "!esum!"=="" (Set "esum=%%z") else ( Set "esum=!esum!,%%z")
 if /i "%%x"=="%DATABASE%" if /i "%%y"=="ERR" if "%%z" neq "" if "!eerr!"=="" (Set "eerr=%%z") else ( Set "eerr=!eerr!,%%z")
 if /i "%%x"=="%DATABASE%" if /i "%%y"=="ALL" if "%%z" neq "" if "!eall!"=="" (Set "eall=%%z") else ( Set "eall=!eall!,%%z")
 )
EndLocal & Set "esum=%esum%" & Set "eerr=%eerr%" & Set "eall=%eall%"
exit /b
::----------::

Run_Maillist.txt

NOTE : SUM-Summary, ERR-Errors, ALL-All

GAMING    ERR    qatech.alarm@gmail.com
GAMING    SUM    dba@test.com
GAMING    SUM    manager@test.com
GAMING    SUM    qatech.pulse@gmail.com

POS    ERR    qatech.alarm@gmail.com
POS    SUM    dba@test.com
POS    SUM    manager@test.com

POS       ERR    tester@test.com,developer@test.com,rc_test@test.com,rc_testrim@test.com,testerrim@test.com
GAMING    ERR    tester@test.com,developer@test.com,rc_test@test.com,rc_testrim@test.com,testerrim@test.com

POS    ALL ssg.dba@test.com
GAMING ALL ssg.man@test.com
I am using this code very successfully and is very easy for my team members to handle varying values.

Pleas ask me if any question.

Last edited by sarbjitsinghgill (19 Dec 2010 14:59)

Sarbjit Singh Gill
IBM certified DBA, MQ Solution Developer
Post Reply