Computer Hope
Software => Computer programming => Topic started by: HypercamJ on June 17, 2008, 09:12:16 AM
-
Is there a way to import settings from an .ini file to use in a batch file? I looked it up on Google (http://www.Google.com/) but I didn't see anything I was looking for.
-
Import settings to a batch file? What do you mean?
Do you have anything you can show us now? (or at least a step by step of what you are planning to do)
-
Since some .exe programs I've seen come with .ini files to load settings I was wondering if you could do the same with .bat files. Example: This (http://dailycupoftech.com/usb-drive-menu-system/) uses an .ini file to change load settings when it is executed.
-
This uses an .ini file to change load settings when it is executed
Couldn't figure out where an ini file came into play with this web page. Could you show us some data from your ini file? Most ini files contain equivalent values for keywords. By getting creative with the set command, you might be able to set variables to be used by your batch file.
Let us know. 8)
-
This is what the .ini would look like:
[Settings]
%PrNm%=Program name*
%Auth%=Author's Name
%PrLc%=Program location
*(Assuming what it would look like.)
I'm trying to find away to have the batch file read this to get the information needed.
-
Still not certain in what context you want this information. This little snippet will produce environment variables.
@echo off
for /f "tokens=1-2 delims==%%" %%v in (test.ini) do (
if not .%%w equ . set %%v=%%w
)
This will produce three variables (PrNm, Auth, and PrLc) that you can use in your batch code as %PrNm%, %Auth%, and %PrLc%
Is this where you're going with this?
8)
-
Kind of, but would they load the information about them from the .ini file?
-
Kind of, but would they load the information about them from the .ini file?
I'm lost. ??? You can extract the data from an ini file and make that data available to a batch file. One way is through the environment (previous post).
What does your batch file look like and why does it need an ini file? What are you trying to do?
8)
-
I tested it and it works how I would like it. Thanks so much! What I was trying to do was have the batch memorize things. Two other questions:
1. Does this take all the variables out of test.ini or just three?
@echo off
for /f "tokens=1-2 delims==%%" %%v in (test.ini) do (
if not .%%w equ . set %%v=%%w
)
2. And are there any other ways to call information (Like from a regular .txt file.) into the batch?
-
1. Does this take all the variables out of test.ini or just three?
It takes all the variables except the heading lines (ie. [Settings]). Note: the delims= clause is specific to your data layout and the if statement eliminates the heading lines.
2. And are there any other ways to call information (Like from a regular .txt file.) into the batch?
The for command with the /f switch will read a file line by line. You can apply any logic within the do loop. The file label of the input file is in parenthesis. This can be any text file, but not proprietary file types like databases or spreadsheets.
You can also use the set /p var=<filename command to bring one line from a file into your batch code.
8)
-
You can also use the set /p var=<filename command to bring one line from a file into your batch code.
So does set /p var=<filename just bring the first line in as %VAR% or does it ask you to select a line?
-
So does set /p var=<filename just bring the first line in as %VAR% or does it ask you to select a line?
Only the first line and there is no prompt. Don't see this much, but for low-volume input it can be practical and eliminates prompts at the console.
Using that form of the set statement within a loop retrieves the same first line repetitively. Sort of like Ground Hog Day ;D
-
So about the other command with the token you were talking about, is there a way to make it retrieve only one variable (with an added command) or no?
-
If you know which parameter you want, something like this would work:
@echo on
set line=0
for /f "tokens=1-2 delims==%%" %%v in (test.ini) do (
if %%v==Auth set %%v=%%w
)
You have other tools installed with Windows, specifically VBScript and JScript. This may be the time to drag them out. 8)
-
You have other tools installed with Windows, specifically VBScript and JScript. This may be the time to drag them out. 8)
Drag the out? As in learning how to import from/to them or what?
-
Figure of speech. Both are already installed with the operating system. This is a good place to learn about VBScript (http://www.microsoft.com/technet/scriptcenter/default.mspx). Everything from tools, examples and articles.
You can also find a file on your system named script56.chm. This is a compiled help file which serves as a reference for both VBScript and JScript.
You can also post here should you have any problems. 8)
-
Could you upload script56? I don't have it on my computer. :( (Searched everywhere!)
-
I don't have it on my computer.
That's odd. What OS are you running?
You can download it here (http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en)
Good luck. 8)
-
Windows XP Home edition. Probably outdated or it only comes with XP Professional. idk...Thanks for the link anyways! Now I might be able to become a vb noob. :P
-
hypercam, you need to stop asking question after question, and start doing some independent study.
-
You'd probably be better off trying visual basic with some of the ideas you have. You can download visual basic express for free here. http://www.microsoft.com/express/vb/default.aspx
And a good book about it: http://www.microsoft.com/MSPress/books/12202.aspx
-
Is there a way to import settings from an .ini file to use in a batch file? I looked it up on Google (http://www.Google.com/) but I didn't see anything I was looking for.
use a programming language. eg Python. Sample ini file
[Settings]
%PrNm%=Program name
%Auth%=Author's Name
%PrLc%=Program location
[Drivers]
a = abc.dll
b = efg.ocx
code
import ConfigParser
config = ConfigParser.ConfigParser()
config.read("file")
print "Program Name ", config.get("Settings", "%PrNm%")
print "Program Location", config.get("Settings", "%PrLc%"),
# dump entire config file
for section in config.sections():
print section
for option in config.options(section):
print " ", option, "=", config.get(section, option)
output:
c:\test> test.py
Program Name Program name
Program Location Program location Drivers
a = abc.dll
b = efg.ocx
Settings
%auth% = Author's Name
%prlc% = Program location
%prnm% = Program name