Read lines from a file into a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Read lines from a file into a variable

#1 Post by ©opy[it]®ight » 09 Aug 2012 11:50

Mod edit: Create a new thread next time, rather than reopen an old one.
©opy[it]®ight edit: i'm sorry! :wink:


Hi all.

I've been reading through the posts looking for a way/solution to store each line into its own variable,
but all that is happening is that the first SET /P reads/puts the complete file content into %LOCAL_VER% :S

Code: Select all

IF EXIST launcher.cir (
  < launcher.cir (
  SET /P LOCAL_VER=
  SET /P UPDATE_NAG=
  )
)
ECHO %LOCAL_VER%
ECHO %UPDATE_NAG%


I really like the idea of reading out a config file that holds multiple settings (one on each line),
but i have no clue how to do that.

Hopefully one of you is willing to help, it would make me really happy and my life more easy :mrgreen: :wink:

Thanks!
Last edited by ©opy[it]®ight on 09 Aug 2012 16:52, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: New technic: set /p can read multiple lines from a file

#2 Post by foxidrive » 09 Aug 2012 12:28

©opy[it]®ight wrote:I really like the idea of reading out a config file that holds multiple settings (one on each line),
but i have no clue how to do that.


Can you rephrase that part?

This will read a file into an array of variables - the normal limits apply.

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in (file.txt) do (
set /a c+=1
set x[!c!]=%%a
)
set x
pause

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Read lines from a file into a variable

#3 Post by ©opy[it]®ight » 09 Aug 2012 12:59

Sorry for necroposting that topic ;-)

Rephrase coming up:

http://www.dostips.com/forum/viewtopic.php?p=9593#p9593

My understanding of the example linked above was that it should be possible to read each line and put each line's result (output) into its own variable, instead of putting the whole file content into one variable, only.

If i can achieve this with your example code than i really appreciate it, but i initially thought i could achieve this with the code in my previous post ;-)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Read lines from a file into a variable

#4 Post by aGerman » 09 Aug 2012 14:38

Your code works fine for me. Open your .cir file with a HEX editor and check whether or not the line break is a Windows <CR><LF> line break.

Regards
aGerman

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Read lines from a file into a variable

#5 Post by ©opy[it]®ight » 09 Aug 2012 15:15

aGerman wrote:Your code works fine for me. Open your .cir file with a HEX editor and check whether or not the line break is a Windows <CR><LF> line break.

Regards
aGerman


Hmm, i only see a linefeed (0A)! :o
I'm on/under W7 and created the launcher.cig file using Notepad++.

I believe it has to do something with syntax highlighting/language settings.

Unbelievable how this could happen, i literally searched all over the internet for hours looking for a solution (facepalm)

Thank you so much aGerman (danke sehr), i'll now test it with the correct settings :)

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Read lines from a file into a variable

#6 Post by Liviu » 09 Aug 2012 15:26

@aGerman, nice guessing.

@©opy[it]®ight, you may want to check http://stackoverflow.com/questions/8195839/choose-newline-character-in-notepad. As long as your text files stay inside NPP you won't notice, since it detects and honors the existing EOLs, but as soon as you leave NPP (such as opening in Notepad, or running as a batch) it's expected that the text files use DOS style CR-LF lines endings.

Liviu

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Read lines from a file into a variable

#7 Post by ©opy[it]®ight » 09 Aug 2012 16:26

Liviu wrote:@aGerman, nice guessing.

@©opy[it]®ight, you may want to check http://stackoverflow.com/questions/8195839/choose-newline-character-in-notepad. As long as your text files stay inside NPP you won't notice, since it detects and honors the existing EOLs, but as soon as you leave NPP (such as opening in Notepad, or running as a batch) it's expected that the text files use DOS style CR-LF lines endings.

Liviu


It works, and thanks! :-)

ps: if you allow me (off-topic): if i would want to have a more feature rich/flexible scripting language, would AutoIT be a good choice?
(i just want to start expanding my capabilities and learn/try something new)

Again thanks for the help!

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Read lines from a file into a variable

#8 Post by ©opy[it]®ight » 09 Aug 2012 18:05

ahh.. it works, but the value of the second variable starts/begins with a carriage return:

Image

What is the best or easiest way to strip/filter this out?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read lines from a file into a variable

#9 Post by foxidrive » 09 Aug 2012 18:20

Process the file to correct the line endings, with a utility.
There are some CMD methods but it depends on what format the file is in.

You can try this:

@echo off
for /f "delims=" %%a in (file.txt) do cmd /c >>file2.txt echo %%a

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Read lines from a file into a variable

#10 Post by ©opy[it]®ight » 09 Aug 2012 19:07

hmm, you're right (again). I envy you(r skills) :P

I'm also suprised by the fact how many people still use batch, at present :)

Best regards

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: New technic: set /p can read multiple lines from a file

#11 Post by Liviu » 09 Aug 2012 23:59

foxidrive wrote:This will read a file into an array of variables - the normal limits apply.

Code: Select all

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in (file.txt) do (
set /a c+=1
set x[!c!]=%%a
)
set x
pause
FWIW the above appears to work with LF Unix-style line endings, at least under my XP. The OP's "set /p" does not, however.

©opy[it]®ight wrote:I'm also suprised by the fact how many people still use batch, at present
1. Comes for free with Windows.
2. Every bit as entertaining as Solitaire, and way more challenging.
3. Arguably useful, even.

;-) Liviu

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Read lines from a file into a variable

#12 Post by Squashman » 10 Aug 2012 05:44

©opy[it]®ight wrote:ps: if you allow me (off-topic): if i would want to have a more feature rich/flexible scripting language, would AutoIT be a good choice?
(i just want to start expanding my capabilities and learn/try something new)

Again thanks for the help!

AutoIt has its place in the scripting world. Definitely good for automating the GUI.

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: Read lines from a file into a variable

#13 Post by ©opy[it]®ight » 15 Aug 2012 05:52

Thanks all! Now everything works the way i want 8)

Post Reply