Reading variables from a text file Using For /F command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Reading variables from a text file Using For /F command

#1 Post by Addcom » 06 Nov 2010 11:58

Am trying to read a known variables from a text file using For /F command and can't see how to set the variables in the batch file.

My batch file reads as follows.
set LANG=EN-UK

for /f "tokens=* delims= " %%a in ('find /v ":" ^< setsfile ^| find "%LANG%"') do (
%%a
)
set s

My Text file reads as follows
: Rem Set Disk file order reguired by oscdimg.exe
SET EN-UK sFileOrder=M11_EN-UK.txt
:
: Rem Set Disk volume label reguired by oscdimg.exe
SET EN-UK sDiskVolumeName=105P036_Rev00
:
: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P037_Rev00
SET EN-UK SFilename=105P036_Rev00
:
: Rem Disk media Kit number
SET EN-UK sKIT=105K036_Rev00_A

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-UK
SET EN-UK sWinpeLANG=en-us

: Rem Winpe Package Language, Lp file. English US is for all Engliah variants ie UK Engliah is EN-US
: Rem Don;t add the file extension ie "lp_en-us.cab" will be "lp_en-us"
SET EN-UK sWinpeLpCabFile=en-Us

:REM ==== : Rem Set Disk file order reguired by oscdimg.exe
SET De-DE sFileOrder=M11-DE.txt

: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P038_Rev00
SFilename=105P038

: Rem Set Disk volume label reguired by oscdimg.exe
SET De-DE sDiskVolumeName=105P038_Rev00

SET De-DE sKIT=105K038_Rev00

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-US
SET De-DE sWinpeLANG=De-DE
: END of txt file

I want the lines containing "Set" and the variables before the "=" sign and it's value and set the variables in batch file.
Note the value can contain spaces as "this is waht I want"
Am getting errors as sFilename not define or not a recognize command etc.
Do I need to define the variables before reading the txt file?
The variables are the same for all languages except i's value

Thanks in advance for your suggestions

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Reading variables from a text file Using For /F command

#2 Post by !k » 06 Nov 2010 12:21

set s
set e

or

Code: Select all

for /f "tokens=1,3* delims= " %%a in ('find /v ":" ^< setsfile ^| find "%LANG%"') do (
%%a %%b
)
set s

Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Re: Reading variables from a text file Using For /F command

#3 Post by Addcom » 07 Nov 2010 09:26

what does set e do and also if am looking for n variables, do I have to add %%a %%b %%c ...%%n?

for /f "tokens=1,3* delims= " %%a in ('find /v ":" ^< D:\WinpeDismCreate\NewFilename_M11.txt ^| find "%LANG%"') do (
%%a %%b %%c %%d %%e %%f %%g %%h
)
set s
echo %%a

I done the above and all I get is %a, %b ... %h as a return
what am I doing wrong? thanks again for the reply

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Reading variables from a text file Using For /F command

#4 Post by !k » 07 Nov 2010 09:46

What you want ?
EN-UK sFileOrder=M11_EN-UK.txt (1st variant)
or
sFileOrder=M11_EN-UK.txt (2nd variant)

Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Re: Reading variables from a text file Using For /F command

#5 Post by Addcom » 07 Nov 2010 13:14

I want every line that contains EN-UK but want to return sFileOrder sFileOrder=M11_EN-UK.txt (2nd variant)
So if there are say 10 lines containg EN-UK I want all 10
The variable s....=

Thanks again been tearing my hair out tring to resolve (lol)

Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Re: Reading variables from a text file Using For /F command

#6 Post by Addcom » 12 Nov 2010 16:31

!K
Am still waiting for your reply, really stuck

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: Reading variables from a text file Using For /F command

#7 Post by DosItHelp » 13 Nov 2010 12:12

Code: Select all

@echo off
set LANG=EN-UK

rem applying language settings
for /f "tokens=* delims=" %%a in ('findstr /i /b /c:"SET %LANG%" ^< setsfile') do (
   for /f "tokens=3,* delims== " %%x in ("%%a") do (
       echo.%%x        -         %%y
       set %%x=%%y
   )
)

rem accessing the resolved variables
echo.
echo sFileOrder is %sFileOrder%

Does this help?

Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Re: Reading variables from a text file Using For /F command

#8 Post by Addcom » 14 Nov 2010 04:18

sorry to say it hangs
I replace setsfile with the location of file and it hangs nver comes out of the loop
also tried set setsfile= D:\WinpeDismCreate\NewFilename_M11.txt
i
@echo off
set LANG=EN-UK
set setsfile= D:\WinpeDismCreate\NewFilename_M11.txt

rem applying language settings
for /f "tokens=* delims=" %%a in ('findstr /i /b /c:"SET %LANG%" ^< %setsfile%') do (
for /f "tokens=3,* delims== " %%x in ("%%a") do (
echo.%%x - %%y
set %%x=%%y
)
)

rem accessing the resolved variables
echo.
echo sFileOrder is %sFileOrder%

thanks for replying, your help is appreciated

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: Reading variables from a text file Using For /F command

#9 Post by DosItHelp » 14 Nov 2010 12:27

May be something in the text file is screwing up the parsing.

I tested this with the text you send in your first post and it worked.
Can you try same?

Optionally you can post the full NewFilename_M11.txt into the forum, so I can take a look.

Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Re: Reading variables from a text file Using For /F command

#10 Post by Addcom » 15 Nov 2010 17:28

Am awuful sorry, the error is in the text file.
I have remove all comments etc from the text file and it is working okay.
But I can see why, so I will have to add comments one by one to see the line causing the problem.
As am new to the forum I can't find a way to add attachment or upload files

so here is the file with comments

: REm Filename: Fileame.txt
: REm Description: This text file declares variavle to be used by the main script reads the hardwareModule list and sets the variable accordingly
: REm Description: Reads the varaible and sets it in the scripts
: REm Notes: Ensure the varable are added as below !!!
: REm Notes: Lines starting with Semicolon (:) are ignored, using for /F loop !!!
: REm Notes: Declare all varaible for all supported languages ie EN-UK, En-US, DE-DE etc
:
: REm Purpose: Help automate scripts without modifying the main script for new hardware
: REm Created By:
: REM Created Date: 28/10/2010
: Rem Revised Date:
: Rem Revised Notes:
:
: rem ===============================================================================================================
: rem ===============================================================================================================
:
: current supported languages are EN-UK, En-US, DE-DE, ES-ES, FR-FR, It-IT and JA-JP
: New languages can be added withou changing the main scripts.
: New File has to be named as the PC or laptop module name, variable used to define PC or laptop is "%SModel%"
:
: Input and Keynoard layout http://msdn.microsoft.com/en-us/goglobal/bb895996.aspx
: rem ================: REM M11 EN-UK info========================
:
: Rem Set Disk file order reguired by oscdimg.exe
SET EN-UK sFileOrder=M11_EN-UK.txt
:
: Rem Set Disk volume label reguired by oscdimg.exe
SET EN-UK sDiskVolumeName=105P036_Rev00
:
: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P037_Rev00
SET EN-UK SFilename=105P036_Rev00
:
: Rem Disk media Kit number
SET EN-UK sKIT=105K036_Rev00_A

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-UK
SET EN-UK sWinpeLANG=en-us

: Rem Winpe Package Language, Lp file. English US is for all Engliah variants ie UK Engliah is EN-US
: Rem Don;t add the file extension ie "lp_en-us.cab" will be "lp_en-us"
SET EN-UK sWinpeLpCabFile=en-Us

: Rem Winpe Keyboard Configuration, Specially important when using laptops
SET EN-UK sWinpeKeyboard=test

: Rem Winpe time zone, GMT for UK
SET EN-UK sTimeZone='GMT Standard Time'

: rem ================: REM M11 EN-US info========================
:
: Rem Set Disk file order reguired by oscdimg.exe
SET EN-US sFileOrder=M11-US.txt

: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P037_Rev00
SET EN-US SFilename=105P037_Rev00

: Rem Set Disk volume label reguired by oscdimg.exe
SET EN-US sDiskVolumeName=105P037_Rev00

: Rem Disk media Kit number
SET EN-US sKIT=105K037_Rev00

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-US
EN-US sWinpeLANG=en-US

: Rem Winpe Package Language, Lp file. English US is for all Engliah variants ie UK Engliah is EN-US
: Rem Don;t add the file extension ie "lp_en-us.cab" will be "lp_en-us"
EN-US sWinpeLpCabFile=lp_en-us

: Rem Winpe Keyboard Configuration, Specially important when using laptops
SET EN-US sWinpeKeyboard=0409:00000409

: Rem Winpe time zone, GMT for Us
EN-US sTimeZone="Pacific Standard Time"
:
: rem ================: REM M11 DE-DE info========================
:
: Rem Set Disk file order reguired by oscdimg.exe
SET De-DE sFileOrder=M11-DE.txt

: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P038_Rev00
SET De-DE SFilename=105P038

: Rem Set Disk volume label reguired by oscdimg.exe
SET De-DE sDiskVolumeName=105P038_Rev00

SET De-DE sKIT=105K038_Rev00

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-US
SET De-DE sWinpeLANG=De-DE

: Rem Winpe Package Language, Lp file. English US is for all Engliah variants ie UK Engliah is EN-US
: Rem Don;t add the file extension ie "lp_de-de.cab" will be "lp_de-de"
SET De-DE sWinpeLpCabFile=lp_de-de

: Rem Winpe Keyboard Configuration, Specially important when using laptops
SET De-DE sWinpeKeyboard=0407:00000407

: Rem Winpe time zone, for germany
SET De-DE sTimeZone="W. Europe Standard Time"
:
: rem ================:: REM M11 ES-ES info========================
:
: Rem Set Disk file order reguired by oscdimg.exe
SET ES-Es sFileOrder=M11-ES.txt

: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P039_Rev00
SET SFilename_M11_ES-ES=105P039_Rev00

: Rem Set Disk volume label reguired by oscdimg.exe
SET ES-Es sDiskVolumeName=105P039_Rev00

SET ES-Es sKIT=105K039_Rev00

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-US
ES-Es sWinpeLANG=ES-ES

: Rem Winpe Package Language, Spanish Lp file
: Rem Don;t add the file extension ie "lp_es-es.cab" will be "lp_es-es"
SET ES-Es sWinpeLpCabFile=lp.cab

: Rem Winpe Keyboard Configuration, Specially important when using laptops
SET ES-Es sWinpeKeyboard=040a:0000040a

: Rem Winpe time zone, for Spain
SET sTimeZone_M11_ES-ES="Romance Standard Time"
:
: rem ================: REM M11 FR-FR info========================
:
: Rem Set Disk file order reguired by oscdimg.exe
SET FR-FR sFileOrder=M11-FR.txt

: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P040_Rev00
SET FR-FR SFilename=105P040_Rev00O

: Rem Set Disk volume label reguired by oscdimg.exe
SET FR-FR sDiskVolumeName=105P040_Rev00

SET FR-FR sKIT=105K040_Rev00

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-US
SET FR-FR sWinpeLANG=De-DE

: Rem Winpe Package Language, French Lp file.
: Rem Don;t add the file extension ie "lp_fr-fr.cab" will be "lp_fr-fr"
SET FR-FR sWinpeLpCabFile=lp_fr-fr

: Rem Winpe Keyboard Configuration, Specially important when using laptops
SET FR-FR sWinpeKeyboard=040c:0000040c

: Rem Winpe time zone, for France
SET FR-FR sTimeZone="Romance Standard Time"

:
: rem ================: REM M11 IT-IT info========================
:
: Rem Set Disk file order reguired by oscdimg.exe
SET IT-IT sFileOrder=M11-It.txt

: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P041_Rev00
SET IT-IT SFilename=105P041_Rev00

: Rem Set Disk volume label reguired by oscdimg.exe
SET IT-IT sDiskVolumeName=105P041_Rev00

IT-IT sKIT=105K041_Rev00

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-US
SET IT-IT sWinpeLANG=IT-IT

: Rem Winpe Package Language, Italian Lp file.
: Rem Don;t add the file extension ie "lp_it-it.cab" will be "lp_it-it"
SET IT-IT sWinpeLpCabFile=lp_it-it.cab

: Rem Winpe Keyboard Configuration, Specially important when using laptops
SET IT-IT sWinpeKeyboard=0410:00000410

: Rem Winpe time zone, for Italy
SET IT-IT sTimeZone="W. Europe Standard Timee"

:
: rem ================: REM M11 JA-JP info========================
:
: Rem Set Disk file order reguired by oscdimg.exe
SET JA-JP sFileOrder=M11-JP.txt

: Rem ISO File names, don't use spaces in filename, reguired by oscdimg.exe
: REM This is the program name prefix for the iso ie 105P042_Rev00
SET JA-JP SFilename=105P042_Rev00

: Rem Set Disk volume label reguired by oscdimg.exe
SET JA-JP sDiskVolumeName=105P042_Rev00

SET JA-JP sKIT=105K042_Rev00

: Rem Winpe Language, English US is for all Engliah variants ie UK Engliah is EN-US
SET JA-JP sWinpeLANG=JA-JP

: Rem Winpe Package Language, Lp file. English US is for all Engliah variants ie UK Engliah is EN-US
: Rem Don;t add the file extension ie "lp_ja-jp.cab" will be "lp_ja-jp"
SET JA-JP sWinpeLpCabFile=lp_ja-jp

: Rem Winpe Keyboard Configuration, Specially important when using laptops
SET JA-JP sWinpeKeyboard=ja-jp

: Rem Winpe time zone, Tokyo for Japanese
SET JA-JP sTimeZone="Tokyo Standard Time"
:
: rem [================: End of Config File info========================]
: rem [=================================================================]
:
I will try all combinations to see what is making it hang
Anyway so happy its woking without the comments, I was trying to comment it for a template etc
thanks for your help, your help is appreciated

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: Reading variables from a text file Using For /F command

#11 Post by DosItHelp » 16 Nov 2010 01:14

I tried with the text you send and it looks fine on my box for EN-UK, as far I can tell.
What error do you get, for which language?

The findstr should ignore all comments so I can't see how the comments could cause a problem, but may be I'm missing something.

When filtering only the lines starting with SET then I see some suspicious lines:

SET EN-UK sFileOrder=M11_EN-UK.txt
SET EN-UK sDiskVolumeName=105P036_Rev00
SET EN-UK SFilename=105P036_Rev00
SET EN-UK sKIT=105K036_Rev00_A
SET EN-UK sWinpeLANG=en-us
SET EN-UK sWinpeLpCabFile=en-Us
SET EN-UK sWinpeKeyboard=test
SET EN-UK sTimeZone='GMT Standard Time' <-- single quotes, others use double quotes
SET EN-US sFileOrder=M11-US.txt
SET EN-US SFilename=105P037_Rev00
SET EN-US sDiskVolumeName=105P037_Rev00
SET EN-US sKIT=105K037_Rev00
SET EN-US sWinpeKeyboard=0409:00000409
SET De-DE sFileOrder=M11-DE.txt
SET De-DE SFilename=105P038
SET De-DE sDiskVolumeName=105P038_Rev00
SET De-DE sKIT=105K038_Rev00
SET De-DE sWinpeLANG=De-DE
SET De-DE sWinpeLpCabFile=lp_de-de
SET De-DE sWinpeKeyboard=0407:00000407
SET De-DE sTimeZone="W. Europe Standard Time"
SET ES-Es sFileOrder=M11-ES.txt
SET SFilename_M11_ES-ES=105P039_Rev00
SET ES-Es sDiskVolumeName=105P039_Rev00
SET ES-Es sKIT=105K039_Rev00
SET ES-Es sWinpeLpCabFile=lp.cab
SET ES-Es sWinpeKeyboard=040a:0000040a
SET sTimeZone_M11_ES-ES="Romance Standard Time"
SET FR-FR sFileOrder=M11-FR.txt
SET FR-FR SFilename=105P040_Rev00O
SET FR-FR sDiskVolumeName=105P040_Rev00
SET FR-FR sKIT=105K040_Rev00
SET FR-FR sWinpeLANG=De-DE
SET FR-FR sWinpeLpCabFile=lp_fr-fr
SET FR-FR sWinpeKeyboard=040c:0000040c
SET FR-FR sTimeZone="Romance Standard Time"
SET IT-IT sFileOrder=M11-It.txt
SET IT-IT SFilename=105P041_Rev00
SET IT-IT sDiskVolumeName=105P041_Rev00
SET IT-IT sWinpeLANG=IT-IT
SET IT-IT sWinpeLpCabFile=lp_it-it.cab
SET IT-IT sWinpeKeyboard=0410:00000410
SET IT-IT sTimeZone="W. Europe Standard Timee"
SET JA-JP sFileOrder=M11-JP.txt
SET JA-JP SFilename=105P042_Rev00
SET JA-JP sDiskVolumeName=105P042_Rev00
SET JA-JP sKIT=105K042_Rev00
SET JA-JP sWinpeLANG=JA-JP
SET JA-JP sWinpeLpCabFile=lp_ja-jp
SET JA-JP sWinpeKeyboard=ja-jp
SET JA-JP sTimeZone="Tokyo Standard Time"

Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Re: Reading variables from a text file Using For /F command

#12 Post by Addcom » 16 Nov 2010 16:09

problem solved, i found out if the last line of the text file contains a colon then the batch file hangs.

The supicious lines is a mistaake, copied from another file. well spotted
The quote signs is also a mistake, not required. Entered for debugging and fogot to remove it after

Thanks again for all the support you provide, you help me figure out a bit how the For /F loop works and much appreciated

Thanks again, stay blessed
Albert

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: Reading variables from a text file Using For /F command

#13 Post by DosItHelp » 16 Nov 2010 19:04

Interesting.
The problem can be fixed by "piping" the text file into the findstr command, instead of using "input redirection".

Code: Select all

@echo off
setlocal
set LANG=EN-UK

for /f "tokens=* delims=" %%a in ('type "setsfile"^|findstr /i /b /c:"SET %LANG%"') do (
   for /f "tokens=3,* delims== " %%x in ("%%a") do (
       echo.%%x        -         %%y
       set %%x=%%y
   )
)

rem accessing the resolved variables
echo.
echo sFileOrder is %sFileOrder%
:wink:

Addcom
Posts: 8
Joined: 06 Nov 2010 11:09

Re: Reading variables from a text file Using For /F command

#14 Post by Addcom » 19 Nov 2010 01:20

:)
Thanks, it worked, even with all sort of characters at the end of the file, ";@|
Even uncompleted statements like Set en-uk

I will use your last suggestion, so in the near future others can modify the batch file without locking up

Thanks again for all the support you given me, i do appreciate

Albert

SenHu
Posts: 19
Joined: 19 Mar 2009 14:57

Re: Reading variables from a text file Using For /F command

#15 Post by SenHu » 20 Nov 2010 08:45

Wow, people on this forum really work hard to solve problems diligently. That's just great. This is a very helpful forum. I thought I add my little bit of help.

When faced with text files that have special characters (commas, colons, quotes, double quotes, exclaimation marks, tabs, etc.) that are sometimes difficult to parse, here is an alternate approach one can take. Regular Expressions. These biterscripting commands will extract all SET variables and their values (regardless of whether they contain special characters).


Code: Select all

var string content                                            # Declare a string variable
cat "C:/path/to/text/file.txt" > $content              # Read input text into string variable
while ( { sen -r "^SET (A>Z)&\n^" $content } > 0 )   # Are there any variables left ?
    stex -r "SET (A>Z)&\n" $content                     # Extract and print the next variable


The regular expression I am using is SET (A>Z)&\n which means the word SET followed by a space, followed by one letter in the range (capital) A>Z, followed by ANYTHING (&), followed by a newline.

Post Reply