Batch file read INI file from UNC path

1.2k views Asked by At

So, I've been working on a batch file to collect specific system information, I've run into a road block with opening an INI file that's on the network installation. So obtaining the path is:

for /f "tokens=2*" %%a in ('REG Query "HKCU\SOFTWARE\Zephyr Associates, Inc." /v StyleDir 2^>nul') do set "StyleDir=%%~b"
for /f "tokens=2*" %%a in ('REG Query "HKLM\SOFTWARE\Zephyr Associates, Inc." /v StyleDir 2^>nul') do set "StyleDir=%%~b"
for /f "tokens=2*" %%a in ('REG Query "HKLM\SOFTWARE\Wow6432Node\Zephyr Associates, Inc." /v StyleDir 2^>nul') do set "StyleDir=%%~b"
cd %StyleDir%

So in this scenario, let's say %StyleDir% is //Server/StyleDir/

Later on in the script we read the Style.ini file with the following:

for /f "tokens=2 delims==" %%a in ('findstr SQLiteHome style.ini') do set SQLiteHome=%%a
for /f "tokens=2 delims==" %%a in ('findstr Server style.ini') do set SQL=%%a
for /f "tokens=2 delims==" %%a in ('findstr DataHome style.ini') do set DataHome=%%a

At this point I get an error saying we're unable to read the Style.ini. Within the Style.ini I have the following:

[Default]
DataHome=C:\ProgramData\Zephyr\Data\
SQLiteHome=C:\ProgramData\Zephyr\Data\
[DataBaseList]
Tons of other lines I don't need to read Right now....

Later I populate a txt file that records the information. That script is as follows:

::Output
echo StyleDir: %StyleDir% >> SystemInformation.txt
echo SQLiteHome: %SQLiteHome% >> SystemInformation.txt
echo SQL Server: %SQL% >> SystemInformation.txt
echo DataHome: %DataHome% >> SystemInformation.txt

So is there a special way that I could get this info recorded from the INI file? I've had thoughts about temporarily mapping a network drive, but the problem with that is knowing what network drives are already mapped so that I don't break what's already there. I'm not even 100% sure that this has to do with the UNC path at all, I just know that when the INI is locally on C:\ that it can be read, but on the network it cannot. Any suggestions for what to try?

Another thing I've noticed is that I can open the Style.ini from a batch file just fine, regardless of the location. I just can't Read it for some reason.

2

There are 2 answers

0
Pyrometheous On BEST ANSWER

I figured it out, all you have to do is use

pushd \\server\dir

instead of

cd \\server\dir

when pointing to the path. Figures it would be something easy. I still get an error, but it'll proceed past it, which is fine by me :-)

3
Emacs User On

You said:

I just know that when the INI is locally on C:\ that it can be read, 
but on the network it cannot.

That's not true. You can read ini files with UNCs like this:

\\ServerName\directory\any.ini

The error may be somewhere else, such as unmatched quotes, authentication or missing file. Knowing what the exact error message you get would help debug the precise reason.

Expanded in response to complete error msg:

CMD does not support UNC paths 

Implies removing this line

cd %StyleDir%

in your batch file as you cannot cd unless you map to a drive letter first. Consult map /help for details. Or you can avoid cd'ing to that folder by fixing the findstr command to use the UNC directly, such as:

findstr stringToSearch \\full\UNC\path\to\file.ini

Which option you choose will depend on what is being done to the found strings. You mention you are populating those strings, but not where. If populating to a file on the remote server, use the drive map option. If populating locally, then use the UNC option.