No announcement yet.

Quotes inside String

  • Filter
  • Time
  • Show
Clear All
new posts

  • Quotes inside String

    Hi there,
    I would like to include quotes inside a string. Not just double quotes, but single quotes inside double quotes.
    My intention is to run a powershell command - something like:
    get-WmiObject -Class Win32_Product -Filter "Name LIKE '%Visual C++%'"

    So I can pass this in to the File.Run() command as a String parameter - but of course it needs quotes around it to make it a string. At which point, the parser sees the extra set of quotes and fails. I've tried to replace the single quotes with \' and replace the double quotes with single, but the File.Run fails.

    My (failing) command is this: File.Run("powershell.exe","get-WmiObject -Class Win32_Product -Filter 'Name LIKE \'%Visual C++%\''| Out-File 'C:\\Temp\\COutput.txt' -NoNewLine -encoding ascii", "C:\\Temp", SW_SHOWNORMAL, true);

    For clarity, the \'' in the above command is actually
    \' followed by ',

    Is there any way for me to get this to work? I could encapsulate the String into a .ps1 file and then use the filename in File.Run() command, but then I have the problem of locating that file (which won't have been installed yet).

    Thanks in advance,

  • #2
    I tried to execute this command you posted, but I get the error
    'Out-File' is not recognized as an internal or external command, operable program or batch file.
    Often when I run into complicated single and double quotes nesting issues, I use the "[[" and "]]" string delimiters instead. So, in this case, something like
    File.Run("the-path-to-the-program.exe", [[ the really complicated string ]], "", SW_NORMAL, true);
    Dialog.Message("The command", [[ get-WmiObject -Class Win32_Product -Filter "Name LIKE '%Visual C++%'" | Out-File 'C:\Temp\COutput.txt' -NoNewLine -encoding ascii ]], MB_OK)
    Note: You do not need to escape the quotes using this method, as you are using a different delimiter. Also, backslashes do not need to be escaped. You will see this:

    Click image for larger version

Name:	SCRN-2022-02-16-01.png
Views:	167
Size:	3.0 KB
ID:	308216

    I do not know how to use the single and double quotes in this command, so I am not stating that this command will actually work, I am just showing how you can store the string containing the quotes so it can be passed as an argument, and leave the actual syntax to you.



    • #3
      Thanks, I'll try that.