Page 1 of 1

[SOLVED] Turning data from a file to variables

Posted: 13 Dec 2014 07:48
by Pineapplesoft
EDIT:

Here is a configuration file for my batch:

color=1
refreshtime=3
title=test

Here is my code that picks up the right parts;

Code: Select all

for /f "tokens=2 delims==" %%a in (console.settings) do ( set data=%%a
)


Now if I want to turn each line into a different variable, what code should I use?

Re: Turning data from a file to variables

Posted: 13 Dec 2014 09:23
by Squashman
You might want to add a brief description of the entire task you are trying to solve. The answer to your question is so open ended it could be programmed several different ways and it may or may not work for you. Obfuscating your input also usually leads to an inaccurate script being written.

Re: Turning data from a file to variables

Posted: 13 Dec 2014 19:23
by josephf
Pineapplesoft wrote:Let's say that we have a following text file:

Code: Select all

X
XX
XXX


How to turn these into different variables?


Code: Select all

@echo off

call :showVars
call :makeTextFile
call :loadVariables
call :showVars
echo.&pause&exit

:loadVariables
   echo.&echo.Loading text file into variables....&echo.
   <"%~dp0\variables.txt" (
      set /p testvar1=
      set /p testvar2=
      set /p testvar3=
      set /p testvar4=
      set /p testvar5=
      set /p testvar6=
      )
goto :eof
   
:makeTextFile
   >"%~dp0\variables.txt" (
      echo.I am testing spaces
      echo.61
      echo.^&^/^\^|
      echo.parentheses^(^)
      echo.?question.period space
      echo.1000000000000000000
      )
goto :eof

:showVars
   echo.&echo.testvar variables currently....
   echo.&set testvar
goto :eof

Re: Turning data from a file to variables

Posted: 15 Dec 2014 06:01
by foxidrive
Pineapplesoft wrote:Now if I want to turn each line into a different variable


Give examples of what you mean - it's not clear to someone else reading what you have written.

Re: Turning data from a file to variables

Posted: 15 Dec 2014 06:26
by Pineapplesoft
foxidrive wrote:
Pineapplesoft wrote:Now if I want to turn each line into a different variable


Give examples of what you mean - it's not clear to someone else reading what you have written.


Like line 1=%color%
line 2=%refreshtime%
line 3=%title%

Re: Turning data from a file to variables

Posted: 15 Dec 2014 06:28
by ShadowThief
conf.txt

Code: Select all

conf_color=1
conf_refreshtime=3
conf_title=test


read_conf.bat

Code: Select all

@echo off
for /f "delims=" %%A in (conf.txt) do set "%%A"


I changed the names of some of your variables because color and title are already batch commands.

Re: Turning data from a file to variables

Posted: 15 Dec 2014 06:44
by dbenham
ShadowThief wrote:I changed the names of some of your variables because color and title are already batch commands.
:? There is no harm in having a variable name the same as a command.

The following will create variables named "color", "refreshtime", and "title":

Code: Select all

for /f  "delims=" %%A in (console.settings) do set "%%A"


Dave Benham