How to insert a quote \" character in string

Find and share HowTos to various installations / configurations!
7 posts • Page 1 of 1
nmnogueira
Posts:125
Joined: Thu May 05, 2011 12:59 pm

How to insert a quote \" character in string

Post by nmnogueira »

Hi,

I need to include a quote " character in a string, in order to open a file which file name may or not have spaces. I already tried:

Code: Select all

    cmd = "start /b "+fileName;
    system(cmd);
    // result string: "start /b filename"
    // does not work when file name has spaces

    cmd = "start /b \\""+fileName+"\\"";
    system(cmd);
    // result string: "start /b \\"filename\\""
    // does not work

    cmd = "start /b """+fileName+"""";
    system(cmd);
    // result string: "start /b filename"
    // does not work

    char quote = 34;
    cmd = "start /b "+quote+fileName+quote;
    system(cmd);
    // result string: "start /b \\"filename\\""
    // does not work
Any suggestions?

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: How to insert a quote \" character in string

Post by mkoller »

Why does your second solution not work ? What happens ?
For me this seems to be the correct approach.
Note also that system() internally prepends "cmd /c"

kilianvp
Posts:443
Joined: Fri Jan 16, 2015 10:29 am

Re: How to insert a quote \" character in string

Post by kilianvp »

try

Code: Select all

strreplace(fileName, "/", "\\\\");
and for example:

Code: Select all

system("start explorer.exe \\"" + fileName+"\\"");
should work

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: How to insert a quote \" character in string

Post by mkoller »

Hint: instead the strreplace() one can use the CTRL function: makeNativePath() ... nicer and works in a platform neutral way

nmnogueira
Posts:125
Joined: Thu May 05, 2011 12:59 pm

Re: How to insert a quote \" character in string

Post by nmnogueira »

Martin Koller wrote:
Why does your second solution not work ? What happens ?
For me this seems to be the correct approach.
Note also that system() internally prepends "cmd /c"
It does not work because the result string is "start /b \\"filename\\"", with the backslash before the quotes.

Kilian von Pflugk wrote:
try
Code:
strreplace(fileName, "/", "\\\\");


and for example:

Code:
system("start explorer.exe \\"" + fileName+"\\"");


should work
This does not work as well. The problem are the spaces, not the slashes in the path

dbindernagel
Posts:161
Joined: Mon Feb 23, 2015 1:34 pm

Re: How to insert a quote \" character in string

Post by dbindernagel »

Your second solution should not have the backslashes in the result.
Are you maybe testing the result with a Debug command and the log viewer?
In the log viewer there will be slashes added but these are not really in the string. It's just for the output (got me confused some time ago, too).

So with Debug and Log Viewer it will show like:

Code: Select all

WCCOAui1:2018.01.18 10:38:07.765["start /b \\"Test 123\\""]
But the content of the string will be:

Code: Select all

start /b "Test 123"

nmnogueira
Posts:125
Joined: Thu May 05, 2011 12:59 pm

Re: How to insert a quote \" character in string

Post by nmnogueira »

Dennis Bindernagel wrote:
Your second solution should not have the backslashes in the result.
Are you maybe testing the result with a Debug command and the log viewer?
In the log viewer there will be slashes added but these are not really in the string. It's just for the output (got me confused some time ago, too).

So with Debug and Log Viewer it will show like:

Code: Select all

WCCOAui1:2018.01.18 10:38:07.765["start /b \\"Test 123\\""]
But the content of the string will be:

Code: Select all

start /b "Test 123"
You are right, I just noticed that as well. I converted to a blob, and only the quote character appears in the string.

I just managed to solve my problem. The problem was with the start function, which expects the following syntax:
START "title" [/D path] [options] "command" [parameters]

If I change my original code to:

Code: Select all

cmd = "start /b \\"\\" \\""+fileName+"\\"";
system(cmd);
it opens the file correctly. It was missing the first parameter (title). Strangely when I didn't use the quotes it worked correctly, as long as there were no spaces in the path.

Thank you all!

7 posts • Page 1 of 1