locked
How to prevent multiple quotes when adding chr(34) to a string RRS feed

  • Question

  • User-1335950151 posted

    When trying to build a string to executing a program using Shell(PathName, WindowType) I need to add a quote (") around the program path/name since it contains spaces in the path otherwise the shell command cannot find the file because it sees a space in the path as the end of the program name. However when I add chr(34) to the beginning and end of the string the shell command still cannot find the file because it contains multiple quotes.

    Example code:

    	Sub RunProgram(ByRef TheError as String)
    	    TheError = ""
    	    Dim WindowType as Integer = AppWinStyle.NormalFocus
    	    Dim ThePath as String = "C:\The Program Path\"	
    	    Dim TheFile as String = "TheProgram.exe"
    	    Dim CommandString as String = chr(34) & ThePath & TheFile & "?Name=Test & chr(34) & " -GetType:False" 
    	    ShellSync(CommandString, TheError, WindowType)
    	End Sub
    
            Public Shared Sub ShellSync(ByVal PathName As String, ErrorString as String, WindowTyp As Integer) 
                Dim lngPid As Long = 0
                Dim lngHandle As Long = 0
                Dim lngExitCode As Long = 0
                Dim TheError As String = ""
                ErrorString = ""
                Try
                    lngPid = Shell(PathName, WindowTyp)
                    If lngPid <> 0 Then
                        lngHandle = OpenProcess(SYNCHRONIZE Or PROCESS_QUERY_INFORMATION, False, lngPid)
                        If lngHandle <> 0 Then
                            WaitForSingleObject(lngHandle, INFINITE)
                            If GetExitCodeProcess(lngHandle, lngExitCode) <> 0 Then
                                ErrorString = ""
                                CloseHandle(lngHandle)
                            Else
                                CloseHandle(lngHandle)
                                ErrorString = "Failed to retrieve exit code, error " + CType(Err.LastDllError, String)
                                Err.Raise(&H8004AA00, TheError)
                            End If
                        Else
                            ErrorString = "Failed to open child process"
                            Err.Raise(&H8004AA01, "ShellSync", TheError)
                        End If
                    Else
                        ErrorString = "Failed to Shell child process"
                        Err.Raise(&H8004AA02, "ShellSync", TheError)
                    End If
                Catch ex As Exception
                   ErrorString = ex.Message
                End Try
            End Sub

    When the shell execution fails and I print the string in the immediate window the command string contains two quotes (") on each end of the program name like this:

    """C:\The Program Path\TheProgram.exe?Name=Test"" -GetType:False"

    it shows a quote around the string (") and two quotes ("") on each end of the program name instead of a quote (") around the string and a quote (") at each end of the program name like this:

    ""C:\The Program Path\TheProgram.exe?Name=Test" -GetType:False"

    The above format is what is the shell command needs, otherwise the shell command sees the multiple quotes and fails to find the program. I have tried stripping the extra quote from the beginning and end after building the string but it then goes back to not finding the program again because it sees a space before the actual end of the path/filename. I have also tried using a single quote (') at each end but that fails also. Can anyone help me figure out how to do this correctly? I am stumped!

    Thanks for any assistance you can give!

    PS. I have also copied the string from the immediate window, pasted it into a command prompt widow, and tried to run it but it also failed to find the program. I then pasted the string into notepad., removed the extra quotes, copied and pasted the edited version into a command prompt window, and it ran with any problems. However, the Shell command runs a program okay if there are no spaces in the program path ... go figure!

    Thursday, December 8, 2016 11:09 AM

Answers

  • User753101303 posted

    Here it is expected to fail. You have quotes at the beginning of ThePath and add again quotes when creating CommandString.

    Ah it seems the problem was perhaps at the end rather than really in the path. Try :

    Dim ThePath As String="C:\Program files (x86]\etc..." and

    CommandString="""" & ThePath & TheFile & """ -GetType:False"

    ie you enclose just the full path with double quotes.

    Edit: the strange thing is that your original code seems ok. Could it be that you removed additional quotes when changing the actual names ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 8, 2016 2:14 PM

All replies

  • User753101303 posted

    Hi,

    IMO the problem is really that this program is not found. The debugger shows the value from a programmer perspective ie Dim s="a" will be show as "a" (ie the actual value is shown enclosed within quotes that are not really part of the value)

    So I believe your code is correctn that seeing those extra " characters is expected when using the debugger and the the problem is really that the program is not found. You could give a try as System.IO.File.Exists maybe to check if this program is really found.

    Thursday, December 8, 2016 11:52 AM
  • User-6180675 posted

    Try with the below code in your RunProgram subprocedure

    Dim ThePath as String = """C:\The Program Path\"	
    Dim TheFile as String = "TheProgram.exe"
    
    'I have used "" to escape double quotes " as per convention in VB
    
    Dim CommandString as String = """" & ThePath & TheFile & "?Name=Test" & """" & " -GetType:False""" 
    
    

    It gave me the below output

    ""C:\The Program Path\TheProgram.exe?Name=Test" -GetType:False"

    If you want to remove the leading and trailing quotes then use the below code

    Dim CommandString as String = ThePath & TheFile & "?Name=Test" & """" & " -GetType:False"

    then the result will be

    "C:\The Program Path\TheProgram.exe?Name=Test" -GetType:False

    Let me know if it works for you

    Thursday, December 8, 2016 12:05 PM
  • User-1335950151 posted

    Thanks for your suggestion PatriceSc. I have that check (system.io.file.exists(Program) ... see example from actual code below) in my original code but not in the example I posted ... the path and program in the example do not actually exist, they are just there to show what is being done. The Shell command works for any program that does not have a space in the path but not for programs that contain a space in the path. For example, the ShellSync() routines in the example will run C:\Windows\Notapad.exe but not C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe, both which actually exist on the machine. The only difference is the spaces in the path. 

                If System.IO.File.Exists(DeployPath) Then
                    ShellSync(Chr(34) + DeployPath + Chr(34) + " " + CommandStr, AppWinStyle.Hide)
                End If
    

    Thursday, December 8, 2016 12:14 PM
  • User-6180675 posted

    Thanks for your suggestion PatriceSc. I have that check (system.io.file.exists(Program) ... see example from actual code below) in my original code but not in the example I posted ... the path and program in the example do not actually exist, they are just there to show what is being done. The Shell command works for any program that does not have a space in the path but not for programs that contain a space in the path. For example, the ShellSync() routines in the example will run C:\Windows\Notapad.exe but not C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe, both which actually exist on the machine. The only difference is the spaces in the path. 

                If System.IO.File.Exists(DeployPath) Then
                    ShellSync(Chr(34) + DeployPath + Chr(34) + " " + CommandStr, AppWinStyle.Hide)
                End If
    

    Can you try with the code I provided , it should resolve the issue for space

    Thursday, December 8, 2016 12:25 PM
  • User-1335950151 posted

    Thanks  you Nilishere. I will start editing code per your example now to see what happens, but from you examples it looks like you may have solved the problem

    Thursday, December 8, 2016 12:26 PM
  • User-1335950151 posted

    Hmmm ... it did not work for me Nilishere ... here's an image of runtime code I pasted into a project ... for some reason I am not getting the same output as you and it is still failing for paths with spaces (see image)

    PS. I am using Visual Studio 2015. Do you know if there is some setting that address this?

    Thursday, December 8, 2016 1:19 PM
  • User753101303 posted

    Here it is expected to fail. You have quotes at the beginning of ThePath and add again quotes when creating CommandString.

    Ah it seems the problem was perhaps at the end rather than really in the path. Try :

    Dim ThePath As String="C:\Program files (x86]\etc..." and

    CommandString="""" & ThePath & TheFile & """ -GetType:False"

    ie you enclose just the full path with double quotes.

    Edit: the strange thing is that your original code seems ok. Could it be that you removed additional quotes when changing the actual names ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 8, 2016 2:14 PM