Page 1 of 1
Read lines from a file into a variable
Posted: 09 Aug 2012 11:50
by ©opy[it]®ight
Mod edit: Create a new thread next time, rather than reopen an old one.©opy[it]®ight edit: i'm sorry!
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
Thanks!
Re: New technic: set /p can read multiple lines from a file
Posted: 09 Aug 2012 12:28
by foxidrive
©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
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 12:59
by ©opy[it]®ight
Sorry for necroposting that topic
Rephrase coming up:
http://www.dostips.com/forum/viewtopic.php?p=9593#p9593My 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
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 14:38
by aGerman
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
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 15:15
by ©opy[it]®ight
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)!
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
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 15:26
by Liviu
@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
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 16:26
by ©opy[it]®ight
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!
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 18:05
by ©opy[it]®ight
ahh.. it works, but the value of the second variable starts/begins with a carriage return:
What is the best or easiest way to strip/filter this out?
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 18:20
by foxidrive
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
Re: Read lines from a file into a variable
Posted: 09 Aug 2012 19:07
by ©opy[it]®ight
hmm, you're right (again). I envy you(r skills)
I'm also suprised by the fact how many people still use batch, at present
Best regards
Re: New technic: set /p can read multiple lines from a file
Posted: 09 Aug 2012 23:59
by Liviu
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
Re: Read lines from a file into a variable
Posted: 10 Aug 2012 05:44
by Squashman
©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.
Re: Read lines from a file into a variable
Posted: 15 Aug 2012 05:52
by ©opy[it]®ight
Thanks all! Now everything works the way i want