Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I m developing a windows application that will use pdf2text pilot software which supports command line. In this application user needs to specify location of the pdf file. I m able to open cmd but could not pass commands to it or somehow my commands are not getting executed.

VB.NET
Imports System
Imports System.IO
Imports System.Diagnostics.Process
Imports System.Diagnostics.ProcessStartInfo

Public Class EDCS

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim dlgrslt As DialogResult = OpenFileDialog1.ShowDialog()
        Dim fnames As String() = OpenFileDialog1.FileNames
        Dim txtfnames As String

        For i = 0 To fnames.Length - 1
            If TextBox1.Text = "" Then
                TextBox1.Text = fnames(i)
            Else
                TextBox1.Text = TextBox1.Text + " / " + fnames(i)
            End If
            txtfnames = fnames(i).Replace(".pdf", ".txt")
            File.Create(txtfnames).Dispose()

            Dim convertcommand As String = "textextract """ & fnames(i) & """ /to """ & txtfnames & """"
            
            '/c will exit cmd and /k will keep it open
            'Shell("cmd.exe /c textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"")

            'SendKeys.Send(convertcommand)
            'SendKeys.Send("{ENTER}")
            
            Dim p As New Process
            p.StartInfo.FileName = "cmd.exe"
            'p.StartInfo.WorkingDirectory = "C:\Program Files (x86)\Two Pilots\PDF2Text Pilot"
            p.StartInfo.Arguments = "textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt""
            p.StartInfo.UseShellExecute() = False
            p.StartInfo.RedirectStandardInput = True
            p.StartInfo.RedirectStandardOutput = True
            p.Start()
            

            'Dim process As New Process()
            'process.StartInfo.FileName = "cmd.exe "
            'process.StartInfo.Verb = "runas"
            'process.StartInfo.UseShellExecute = False
            'process.StartInfo.RedirectStandardInput = True
            'process.StartInfo.RedirectStandardOutput = True
            'process.Start()

            'process.StandardInput.WriteLine("textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"")
            'process.StandardInput.WriteLine("exit")
            'process.Close()

                  Next
    End Sub
End Class


OS: Windows 7
vb.net developer

Thanks in Advance
Posted

1 solution

The worst thing you are doing is using CMD.EXE. I don't know why so many people makes this mistake, suppose this is a diversion of "user thinking" (non-engineering). You absolutely don't need it.
Say, you have "textextract.exe". Remove "texteatract" from p.StartInfo.Arguments. Make sure the arguments are correct. Make sure "textextract.exe" is in the right path. Instead of line with "CMD.EXE" write
C#
p.StartInfo.FileName = "textextract.exe"


That's all.

It's so strange that you expected that your application should work with you Arguments value. It should be pretty obvious that it's wrong. Actually, there is a way to use CMD.EXE with the application, but I don't even discuss it. Doing it would be totally insane. Your problem is already solved.

—SA
 
Share this answer
 
Comments
AD89 24-Dec-15 1:18am    
Sir Sergey Alexandrovich, Command line Syntax given by pdf2text pilot software is
textextract "C:\My PDFs" /to "C:\text"
I tried your solution but doesn't work

Dim p As New Process
p.StartInfo.FileName = "textextract .exe"
p.StartInfo.WorkingDirectory = "C:\Program Files (x86)\Two Pilots\PDF2Text Pilot"
p.StartInfo.Arguments = convertcommand '''''''''''"textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt""
p.StartInfo.UseShellExecute() = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.Start()
Sergey Alexandrovich Kryukov 24-Dec-15 2:27am    
Arguments should be arguments, without textextract.
—SA
AD89 24-Dec-15 2:32am    
I tried this

Dim p As New Process
p.StartInfo.FileName = "textextract .exe"
p.StartInfo.WorkingDirectory = "C:\Program Files (x86)\Two Pilots\PDF2Text Pilot"
p.StartInfo.Arguments = ""C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt""
p.StartInfo.UseShellExecute() = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.Start()


doesn't works
AD89 24-Dec-15 1:21am    
If i open cmd and just type
textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"

then the pdf file gets converted to text file successfully no matter what dir it is in cmd
Sergey Alexandrovich Kryukov 24-Dec-15 2:28am    
And..?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900