Showing posts with label read. Show all posts
Showing posts with label read. Show all posts

Saturday, 16 August 2008

Batch programming tip #16 (part 2): getting property values from an ini file

This is just a slight variation on the previous post on working with *.ini files.
Here we only retrieve property values but not sections or property names.

@echo off
setlocal enabledelayedexpansion

:init_vars
set ini_file=my.ini
goto show_values

:show_values
for /F "tokens=1,2 delims==" %%L in (%ini_file%) do (
if not [%%M]==[] (
echo Value: %%M
)
)
goto eof

:eof
echo Press any key to quit
pause > NUL


Simple :-).

Thoughts?

Monday, 11 August 2008

Batch programming tip #16: Reading a property from an INI file

You are going to start thinking batch an I are having a love-hate relationship but there was an interesting question on computing.net recently. Someone asked how to get a property from an INI file and/or print things to an ini file or text file.

This was my effort at it.

File: - my.ini -

[provider]
name=ETH
organization=Stardust

[file]
server=192.168.254.42 ; use IP address
port=8080
file=wonderful.txt


The aim of the following batch is to retrieve the server IP address.
To do this we read all the lines of the ini file (that is each first "token" of every line, but because the property=value entities don't contain any default delimiters the first token will actually be the property=value pair on each line).

Then we try matching the beginning of the line with our property name.
If it matches, bingo, get the remaining string value and output it to ini/txt.

File: - ini_bat.bat -

@echo off
setlocal enabledelayedexpansion

:init_vars
:: this is our ini file path
set ini_file=my.ini
:: this is the key for which we want to retrieve a value
set key=server
:: this is the length of our key word (e.g. 'server')
set keylen=6
:: this is the index at which the equal sign should be
set /a eqsign=%keylen%+1
:: tmp dir name (should be empty)
set tmp_dir=tmp000
:: make temp dir
if not exist %tmp_dir% mkdir %tmp_dir%
goto get_key

:get_key
set tmp_file=%tmp_dir%\tmp.txt
:: this read the first token of every line, shouldn't be spaces in the ini file properties
for /F %%L in (%ini_file%) do (
set line=%%L
set linestart=!line:~0,%keylen%!

if !linestart!==%key% (
set value=!line:~%eqsign%!
goto output_value
)
)

:: this will erase existing %tmp_dir%\new.ini and %tmp_dir%\new.txt files

:output_value
echo Outputting value to ini file
echo server=%value% > %tmp_dir%\new.ini
echo Outputting value to text file
echo The value is: %value% > %tmp_dir%\new.txt
goto eof

:eof
echo Press any key to quit
pause > NUL
goto blackhole

:blackhole


And there you have it!

Some obvious improvements could be:
- setting the key dynamically (this also means computing the key length on the fly).
- including adequate behaviour if the property definitions include spaces
(e.g. server = 192.0.2.42)

Thoughts?

Tuesday, 15 July 2008

Batch programming tip#08 (part 3): Reading from a file - Looping

This post will bring an end (I think) to this thread about file reading using batch.

To use the loop command on a file we simply add /F in our FOR loop construct like so:

@echo off

:init_vars
if exist file1.txt goto action
echo Oops. file1.txt doesn't exist. Please create it or change file name.
goto eof

:action
for /F %%l in (file1.txt) do (
echo Content: %%l
)
goto eof

:eof
echo Press any key to close window...
pause > nul
goto blackhole

:blackhole

This will probably work with most configuration files you might use. There is one additional parameter that's really useful though, especially if your lines contain spaces. The FOR loop will by default only return the first token up to a delimiter character. The default delimiter characters are space and tab. This means the script above will only return the first word of any line containing spaces.

To avoid this (usually unwanted) behaviour, use the delims parameter and set it to empty as in the following code:

@echo off

:init_vars
if exist file1.txt goto action
echo Oops. file1.txt doesn't exist. Please create it or change file name.
goto eof

:action
for /F "delims=" %%l in (file1.txt) do (
echo Content: %%l
)
goto eof

:eof
echo Press any key to close window...
pause > nul
goto blackhole

:blackhole


Notice how we have added to our for construct:
for /F "delims=" %%l in (file1.txt) do

You can set "delims=" to whatever you want in effect, you could for instance use comma instead: "delims=,".

Well, I think that's it, you can of course take file reading much further if you want. In that case, you might want to check Rob van der Woude's comprehensive page about NT FOR syntax. Enjoy!

More shortly about how to loop through directories.

Thoughts in the meantime?

Monday, 14 July 2008

Batch programming tip#08 (part 2): Reading from a file - Looping

So, let's see how we can loop through information.
The following script is the same as the previous but I have added a loop construct which will loop through our file_content variable (the first line of the file).

@echo off

:init_vars
if exist file1.txt goto action
echo Oops. file1.txt doesn't exist. Please create it or change file name.
goto eof

:action
set /P file_content=<file1.txt
for %%l in (%file_content%) do (
echo Content: %%l
)
goto eof

:eof
echo Press any key to close window...
pause > nul
goto blackhole

:blackhole

The loop construct is simple: for %%varname in (%var%) do ( ..[action]..)
The parentheses around the %var% are not in there just for form, you really need to remember them or your batch will crash.

To test it try using a file that contains a first line with spaces in it (e.g. The quick brown fox jumps over the lazy dog) and you will see that each word of the first line of the file will appear on a line of its own.

Excellent, the next step will be to loop through all the lines in a file.

Thoughts in the meantime?

Sunday, 13 July 2008

Batch programming tip#08: Reading from a file

Reading from file can sometimes be really useful. For example, you could imagine one process was to output lots of file names to a given file (let's call it file1.txt) and then that our batch would jump in there and retrieve those file names and print up whatever is in them (obviously in a real-world case scenario, you would want to handle the files and do something with them, e.g. archive them to a different location or whatever).

To read content from a file, we set a variable using /P and the < sign after the usual = sign.

The following code does this.

@echo off

:init_vars
set /P file_content=<file1.txt
goto action

:action
echo File content: %file_content%
goto eof

:eof
echo Press any key to close window...
pause > nul
goto blackhole

:blackhole

Note that if file1.txt doesn't exist an error message will show and the variable %file_content% will be empty.

Hey presto, we got something out of the file. This is great... except: we are only getting hold of the first line.

In the two following posts, I will explain the loop construct and how to use it to read all the lines from the file.

Thoughts in the meantime?
Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory