[SOLVED] Turning data from a file to variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Pineapplesoft
Posts: 2
Joined: 13 Dec 2014 07:43

[SOLVED] Turning data from a file to variables

#1 Post by Pineapplesoft » 13 Dec 2014 07:48

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?
Last edited by Pineapplesoft on 15 Dec 2014 06:53, edited 2 times in total.

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

Re: Turning data from a file to variables

#2 Post by Squashman » 13 Dec 2014 09:23

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.

josephf
Posts: 11
Joined: 01 Feb 2014 23:22

Re: Turning data from a file to variables

#3 Post by josephf » 13 Dec 2014 19:23

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

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

Re: Turning data from a file to variables

#4 Post by foxidrive » 15 Dec 2014 06:01

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.

Pineapplesoft
Posts: 2
Joined: 13 Dec 2014 07:43

Re: Turning data from a file to variables

#5 Post by Pineapplesoft » 15 Dec 2014 06:26

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%

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Turning data from a file to variables

#6 Post by ShadowThief » 15 Dec 2014 06:28

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.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Turning data from a file to variables

#7 Post by dbenham » 15 Dec 2014 06:44

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

Post Reply