Jump to content

Read ini file using batch


Recommended Posts

You don't even need to use find if you only have one line under a single section heading.

Type this single line into a console window to see what I mean:

FOR /F "USEBACKQ EOL=[ TOKENS=*" %# IN ("X:\PATH TO\DIRECTORY\FOLDERSTROE.INI") DO @ECHO=%#

Obviously you'll need to change the path accordingly.

Link to comment
Share on other sites


You don't even need to use find if you only have one line under a single section heading.

Type this single line into a console window to see what I mean:

FOR /F "USEBACKQ EOL=[ TOKENS=*" %# IN ("X:\PATH TO\DIRECTORY\FOLDERSTROE.INI") DO @ECHO=%#

Obviously you'll need to change the path accordingly.

Yep :thumbup

But, since we are going to read a very simple file, made of two lines, of which we are interested in the second only, we can also skip the first one.

@allen2

Of course it is presumed that no Cabfolder1 environment variable is present, please note how your remark also applies to the original "value1" or, for that matter, for any variable name, theoretically you need to do error checking EVERY time you define a variable to see if it is already defined or use a LOCAL statement (or use XSET for an opposite behaviour), etc., etc.

@echo off
SETLOCAL
FOR /F "USEBACKQ skip=1 tokens=1,2 delims==" %%A IN ("folderstore.ini") DO IF NOT DEFINED %%A SET "%%A=%%B"&IF NOT DEFINED %%A SET /P %%A=Path to your cabfolder:

the above won't touch a pre-existing variable, but obviously IF the variable is already defined, it won't be of ANY use.

This might be more effective ;) (just an idea):

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET #a_h1ghl1_1mprob@bl3_v@r1@bl3_n@m3=NO
FOR /F "USEBACKQ skip=1 tokens=1,2 delims==" %%A IN ("folderstore.ini") DO (
IF DEFINED %%A (
ECHO Dear demented USER, the variable %%A IS ALREADY DEFINED!
SET %%A
ECHO.
SET /P #a_h1ghl1_1mprob@bl3_v@r1@bl3_n@m3=Type YES - CAPITAL LETTERS - to change it's value to "%%B" or press [ENTER] to leave it alone:
) ELSE (
SET #a_h1ghl1_1mprob@bl3_v@r1@bl3_n@m3=YES
)
IF "!#a_h1ghl1_1mprob@bl3_v@r1@bl3_n@m3!"=="YES" SET "%%A=%%B"&IF NOT DEFINED %%A SET /P %%A=Path to your cabfolder:
)

jaclaz

Link to comment
Share on other sites

FOR /F "TOKENS=1* DELIMS==" %%# IN (
'FIND "Cabfolder1"^<FOLDERSTORE.INI 2^>NUL') DO SET "VAL1=%%$"

Hi, Yzöwl

I was trying to understand your script in post#6 and was wondering what does it means when you use %%#

and %%$

If I tried replacing with normal variable like

%%A for %%# and

%%B for %%$

surprising it returns correctly the result.

And looking at the ascii table,

the next character after # is $,

just like next character after A is B.

Is there other special consideration when you use # and $ as variable?

Will simple character like A and B achieve exactly the same result?

So I can generally use special character as variable, just like you did?

Link to comment
Share on other sites

Just for the record:

http://www.robvanderwoude.com/clevertricks.php

scroll down until you find "Carlos M. found a way to increase the number of variables available in a FOR /F loop" that explains partially the thingy.

The real issue :ph34r: wich I can see with using the additional # and $ is that the NEXT characters are %, &, etc. that would need to be escaped, so they are a very nice choice if you only have 2 tokens, but they may become a problem if you need 3 or more. :unsure:

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...