Jump to content
Sign in to follow this  
TheFluffyOne

Quote (speech mark) handling in IniRead

Recommended Posts

TheFluffyOne

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).

Share this post


Link to post
Share on other sites
Pain

Try with single quote '"c:\some folder\with spaces\myscript.vbs"'

Share this post


Link to post
Share on other sites
Valuater

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)

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
TheFluffyOne

Try with single quote '"c:\some folder\with spaces\myscript.vbs"'

I don't understand how this would resolve the problem. Could you explain please?

Share this post


Link to post
Share on other sites
TheFluffyOne

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.

Share this post


Link to post
Share on other sites
Pain

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

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

Share this post


Link to post
Share on other sites
TheFluffyOne

$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.

Share this post


Link to post
Share on other sites
ResNullius

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"'

Share this post


Link to post
Share on other sites
TheFluffyOne

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

Share this post


Link to post
Share on other sites
youknowwho4eva

$result = stringtrimright($commandName,stringlen($commandName)-1)

if $result = '"' then

else

endif

fill in the blanks

Edit: forgot the -1

Edited by youknowwho4eva

Giggity

Share this post


Link to post
Share on other sites
saywell

Would IniReadSection have the same problem?

William

Share this post


Link to post
Share on other sites
ResNullius

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...

Share this post


Link to post
Share on other sites
ResNullius

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

Share this post


Link to post
Share on other sites
TheFluffyOne

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"

Share this post


Link to post
Share on other sites
ResNullius

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"?

Share this post


Link to post
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
Sign in to follow this  

×