Jump to content

Quote (speech mark) handling in IniRead


Recommended Posts

I am parsing INI files for entries used to launch other processes. A stripped-down example of such a file might be:

[global]
command=wscript.exe
commandline="c:\some folder\with spaces\myscript.vbs"

I'm using IniRead to grab these values like so:

$commandName = IniRead("test.ini", "global", "command", "")
$commandParams = IniRead("test.ini", "global", "commandline", "")
$fullCommand = $commandName & " " & $commandParams
ConsoleWrite($fullCommand)

However IniRead is stripping the quotes from the value, so the output of the script is:

wscript.exe c:\some folder\with spaces\myscript.vbs

In the full script this fails because wscript.exe tries to load a script called "c:\some". If, however, I have a value that doesn't start and end with a quote, the path is quoted as expected. An example of an INI file that would work:

[global]
command=wscript.exe
commandline="c:\some folder\with spaces\myscript.vbs" scriptinput.txt

The output when using this as input is:

wscript.exe "c:\some folder\with spaces\myscript.vbs" scriptinput.txt

Is this expected behaviour? If so, does this mean I'm going to have to write my own INI parser to work around this behaviour? (Note that the INI files are from an old system, which is why I can't just alter the input file format).

Link to comment
Share on other sites

I'm thinking it might be easier to change this...

$commandName = IniRead("test.ini", "global", "command", "")
$commandParams = IniRead("test.ini", "global", "commandline", "")
$fullCommand = $commandName & ' "' & $commandParams & '"'
ConsoleWrite($fullCommand)
The problem here is that my second example gets mangled to:

wscript.exe ""c:\some folder\with spaces\myscript.vbs" scriptinput.txt"

This also won't launch. I need to cope with entries that are completely enclosed in quotes as well as those that are not.

Link to comment
Share on other sites

$commandName = IniRead("test.ini", "global", "command", "")
$commandParams = IniRead("test.ini", "global", "commandline", "")

$fullCommand = '"' & $commandName &'" ' & '"' &$commandParams & '"'
ConsoleWrite($fullCommand)

Link to comment
Share on other sites

$commandName = IniRead("test.ini", "global", "command", "")
$commandParams = IniRead("test.ini", "global", "commandline", "")

$fullCommand = '"' & $commandName &'" ' & '"' &$commandParams & '"'
ConsoleWrite($fullCommand)
Suffers from the same problem as the suggestion by Valuater, unfortunately. Thanks anyway.

Btw, the font you are looking for (the Common Tasks pane font) is 8pt Tahoma Bold with anti-aliasing disabled.

Link to comment
Share on other sites

Can you add single quotes around the values when you write them to the ini file, regardless of whether they have double quotes or not

[global]

command=wscript.exe

commandline='"c:\some folder\with spaces\myscript.vbs"'

Link to comment
Share on other sites

Can you add single quotes around the values when you write them to the ini file, regardless of whether they have double quotes or not

The INI files are historical files supplied from elsewhere. For various reasons it is not a good idea for us to modify these.

Edited by TheFluffyOne
Link to comment
Share on other sites

Would IniReadSection have the same problem?

William

from: http://en.wikipedia.org/wiki/INI_file

Quoted values

Some implementations allow values to be quoted, typically using double quotes and/or apostrophes. This allows for explicit declaration of whitespace, and/or for quoting of special characters (equals, semicolon, etc.). The standard Windows function GetPrivateProfileString supports this, and will remove quotation marks that surround the values.

That's why I suggested the single quotes when writing the ini values

I think the final solution is going to involve RegExp on the values when they're read: if they have a space in them but don't have quotes then add the quotes? Or some such...

Link to comment
Share on other sites

The problem here is that my second example gets mangled to:

wscript.exe ""c:\some folder\with spaces\myscript.vbs" scriptinput.txt"

This also won't launch. I need to cope with entries that are completely enclosed in quotes as well as those that are not.

@TheFluffyOne

In your ini for the second example

[global]
command=wscript.exe
commandline="c:\some folder\with spaces\myscript.vbs" scriptinput.txt

does the parameter "scriptinput.txt" ever have a space in it, like "script input.txt" or does it ever have a full path as well, like "c:\some other folder\scriptinput.txt" ?

Edit: Answered one of my own questions and changed it to get clarification.

Edited by ResNullius
Link to comment
Share on other sites

does the parameter "scriptinput.txt" ever have a space in it, like "script input.txt" or does it ever have a full path as well, like "c:\some other folder\scriptinput.txt" ?

Yes, in some cases the path will have spaces and when this happens it will be quoted like so:

[global]
command=wscript.exe
commandline="c:\some folder\with spaces\myscript.vbs" "c:\Program Files\Noddy Program\fixme.txt"
Link to comment
Share on other sites

Yes, in some cases the path will have spaces and when this happens it will be quoted like so:

[global]
command=wscript.exe
commandline="c:\some folder\with spaces\myscript.vbs" "c:\Program Files\Noddy Program\fixme.txt"
Can you post an ini that has all possible combinations of entries as you would encounter them:

ie: quoted paths, unquoted paths, unquoted with spaces in the "scriptinput.txt" parameter, etc.

It will help immensely in developing a solution that works for all instances;

rather than figure one out and then find out it doesn't work for another example.

Also, are all your paths when referenced using actual drive letters:

"C:" "D:", etc, or do some reference network paths "\\Server\folder with space\myscript.vbs"?

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
 Share

  • Recently Browsing   0 members

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