Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem running a command when the path has spaces in its name.

Problem running a command when the path has spaces in its name.

Scheduled Pinned Locked Moved Visual Basic
helpjavaadobedebugging
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Mujica
    wrote on last edited by
    #1

    Backgroud: I'm trying to automate the printing of PDF files. I found that by invoking

    acrord32.exe /N /T pdfFile printerName

    on the command line you can print PDFs silently. Great. I'm trying to automate this in VB via

    Dim p As Process = New Process()
    Dim pi As ProcessStartInfo = New ProcessStartInfo()
    Dim command As String = Chr(34) & "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\acrord32.exe" & Chr(34) & " /N /T "

        Try
    
            pi.Arguments = String.Format("{0} {1} {2} {3}{4}{5}", " /C ", command, sFilename, Chr(34), sPrinter, Chr(34))
    
            pi.UseShellExecute = False
            pi.RedirectStandardError = True
            pi.RedirectStandardOutput = True
            pi.CreateNoWindow = True
    
            pi.WindowStyle = ProcessWindowStyle.Normal
    
            pi.FileName = "cmd.exe"
            p.StartInfo = pi
    
            p.Start()
    
            Do Until p.HasExited : Loop
    
            outp = p.StandardOutput.ReadToEnd
    
            Debug.Print(outp)
    
            outp = p.StandardError.ReadToEnd
    
            Debug.Print(outp)
    
        Catch ex As Exception
            Debug.Print(ex.Message)
    
        End Try
    

    The error message I get from StdErr is

    Quote:

    'C:\Program' is not recognized as an internal or external command, operable program or batch file.

    I've tried putting quotes around the command string, but it just doesn't work. Turning to the community for guidance. :java:

    D 1 Reply Last reply
    0
    • D David Mujica

      Backgroud: I'm trying to automate the printing of PDF files. I found that by invoking

      acrord32.exe /N /T pdfFile printerName

      on the command line you can print PDFs silently. Great. I'm trying to automate this in VB via

      Dim p As Process = New Process()
      Dim pi As ProcessStartInfo = New ProcessStartInfo()
      Dim command As String = Chr(34) & "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\acrord32.exe" & Chr(34) & " /N /T "

          Try
      
              pi.Arguments = String.Format("{0} {1} {2} {3}{4}{5}", " /C ", command, sFilename, Chr(34), sPrinter, Chr(34))
      
              pi.UseShellExecute = False
              pi.RedirectStandardError = True
              pi.RedirectStandardOutput = True
              pi.CreateNoWindow = True
      
              pi.WindowStyle = ProcessWindowStyle.Normal
      
              pi.FileName = "cmd.exe"
              p.StartInfo = pi
      
              p.Start()
      
              Do Until p.HasExited : Loop
      
              outp = p.StandardOutput.ReadToEnd
      
              Debug.Print(outp)
      
              outp = p.StandardError.ReadToEnd
      
              Debug.Print(outp)
      
          Catch ex As Exception
              Debug.Print(ex.Message)
      
          End Try
      

      The error message I get from StdErr is

      Quote:

      'C:\Program' is not recognized as an internal or external command, operable program or batch file.

      I've tried putting quotes around the command string, but it just doesn't work. Turning to the community for guidance. :java:

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You have to separate the command line options from the command you're trying to execute. For your "filename", you've got:

      "C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe" /N /T
      

      That doesn't work. Your filename and arguments must be specified separately:

      Dim command As String = """C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe"""
      Dim arguments As String = String.Format("/n /t ""{0}"" ""{1}"" ""{2}"" ""{3}""", pdfFilePath, printerName, driverName, portName)
      
      Dim process As New Process
      Dim pi As New ProcessStartInfo
      
      pi.Filename = command
      pi.Arguments = arguments
      process.StartInfo = pi
      process.Start
      

      Yes, the double quotes are there for a reason. It's shorter to type "" than it is to type Chr$(34) &.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You have to separate the command line options from the command you're trying to execute. For your "filename", you've got:

        "C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe" /N /T
        

        That doesn't work. Your filename and arguments must be specified separately:

        Dim command As String = """C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe"""
        Dim arguments As String = String.Format("/n /t ""{0}"" ""{1}"" ""{2}"" ""{3}""", pdfFilePath, printerName, driverName, portName)
        
        Dim process As New Process
        Dim pi As New ProcessStartInfo
        
        pi.Filename = command
        pi.Arguments = arguments
        process.StartInfo = pi
        process.Start
        

        Yes, the double quotes are there for a reason. It's shorter to type "" than it is to type Chr$(34) &.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        D Offline
        D Offline
        David Mujica
        wrote on last edited by
        #3

        Your suggestion worked perfectly. I was over-thinking the problem. I was trying to run cmd.exe and pass the acrord32.exe as a parameter. Your solution of running acrord32.exe directly worked. Thank you.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups