Computer Hope

Software => Computer programming => Topic started by: HypercamJ on June 17, 2008, 09:12:16 AM

Title: .INI with .BAT
Post 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.
Title: Re: .INI with .BAT
Post by: Carbon Dudeoxide on June 17, 2008, 09:19:39 AM
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)
Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 09:22:25 AM
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.
Title: Re: .INI with .BAT
Post by: Sidewinder on June 17, 2008, 12:23:16 PM
Quote
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)
Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 01:54:38 PM
This is what the .ini would look like:
Code: [Select]
[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.
Title: Re: .INI with .BAT
Post by: Sidewinder on June 17, 2008, 02:49:57 PM
Still not certain in what context you want this information. This little snippet will produce environment variables.

Code: [Select]
@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)
Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 02:51:29 PM
Kind of, but would they load the information about them from the .ini file?
Title: Re: .INI with .BAT
Post by: Sidewinder on June 17, 2008, 03:03:50 PM
Quote
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)

Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 03:12:27 PM
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?
Code: [Select]
@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?



Title: Re: .INI with .BAT
Post by: Sidewinder on June 17, 2008, 03:23:45 PM
Quote
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.

Quote
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)
Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 03:32:49 PM
Quote
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?
Title: Re: .INI with .BAT
Post by: Sidewinder on June 17, 2008, 03:50:21 PM
Quote
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
Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 03:56:23 PM
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?
Title: Re: .INI with .BAT
Post by: Sidewinder on June 17, 2008, 04:58:47 PM
If you know which parameter you want, something like this would work:

Code: [Select]
@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)
Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 05:33:40 PM
Quote
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?
Title: Re: .INI with .BAT
Post by: Sidewinder on June 17, 2008, 06:05:15 PM
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)
Title: Re: .INI with .BAT
Post by: HypercamJ on June 17, 2008, 11:07:00 PM
Could you upload script56? I don't have it on my computer.  :( (Searched everywhere!)
Title: Re: .INI with .BAT
Post by: Sidewinder on June 18, 2008, 03:11:49 AM
Quote
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)
Title: Re: .INI with .BAT
Post by: HypercamJ on June 18, 2008, 10:13:47 AM
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
Title: Re: .INI with .BAT
Post by: Dias de verano on June 20, 2008, 12:29:46 AM
hypercam, you need to stop asking question after question, and start doing some independent study.
Title: Re: .INI with .BAT
Post by: michaewlewis on June 20, 2008, 01:41:11 PM
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
Title: Re: .INI with .BAT
Post by: ghostdog74 on June 20, 2008, 09:49:26 PM
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
Code: [Select]
[Settings]
%PrNm%=Program name
%Auth%=Author's Name
%PrLc%=Program location

[Drivers]
a = abc.dll
b = efg.ocx
code
Code: [Select]
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:
Code: [Select]
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