Solved

Run Dos Program and Pass Arguments

Posted on 2002-05-09
22
410 Views
Last Modified: 2007-12-19
Hi,
I want to run a command program by passing arguments via a textbox etc., then execute the program from VB. I use some of the utilities from www.systeminternals.com and I want to create a gui to run them. Thanks,
d
0
Comment
Question by:dingo11
  • 9
  • 8
  • 5
22 Comments
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
you can use shell to run a command.

This will run whatever command is in Text1 and send the results of the command to a file.

Private Sub Command1_Click()
Dim cm As String
cm = "command.com /c " & Text1.Text & ">C:\tempfile.txt"
Shell cm, vbNormalFocus
End Sub
0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
to run a file try this.

Private Sub Command1_Click()
Dim cm As String
cm = "command.com /c " & Text1.Text
Shell cm, vbNormalFocus
End Sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
well, try with:
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation
As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal
nShowCmd As Long) As Long

dim ret as long

ret=shellexecute(me.hwnd,"open",pathto_your_exe_here,"paremeters(if any otherwise vbnullstring)",vbnullstring,vbnormal)
0
 

Author Comment

by:dingo11
Comment Utility
guys,
I thought I sent this yesterday but no,
Here is one of the programs by Mark Russinovich, who I e-mailed about the programs usage.

sdelete [-p passes] [-s] [-q] <file or directory>
-p passes      Specifies number of overwrite passes
-s             Recurse subdirectories
-q             Don't print errors (quiet)

======================================
vin,
I only want to enter the arguments via the textboxes.

richie,
ret returned a 42? and didn't delete a thing. What if you want to leave out an argument? Eg a file does need recursion. I guess Mark will answer that if he he gets back to me.

---------------
    Dim ret As Long
    Dim sCommand, sFiletoDelete As String

    sCommand = "C:\Documents and Settings\Administrator\Desktop\SecureDelete\SDELETE.EXE"
    sFiletoDelete = "C:\Documents and Settings\Administrator\Desktop\SecureDelete\test.txt"
   
    ret = ShellExecute(Me.hwnd, "open", sCommand, "1  sFiletoDelete", vbNullString, vbNormal)
    MsgBox ret

0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 85 total points
Comment Utility
Maybe you can do something like this:

Private Sub Command1_Click()
Dim sCommand, sFiletoDelete As String
Dim sCm, mResp As String
sCommand = "C:\Documents and Settings\Administrator\Desktop\SecureDelete\SDELETE.EXE"
sFiletoDelete = "C:\Documents and Settings\Administrator\Desktop\SecureDelete\test.txt"
If Text1.Text = "" Then
    mResp = MsgBox("No arguments entered!", vbYesNoCancel, "Do you want to launch without arguments?")
    If mResp <> vbYes Then GoTo skpIt
    sCm = sCommand & Space(1) & sFiletoDelete
    Shell sCm, vbNormalFocus
    GoTo skpIt
End If
sCm = sCommand & Space(1) & Text1.Text & Space(1) & sFiletoDelete
Shell sCm, vbNormalFocus
skpIt:
End Sub
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
if i am not wrong (i don't know that prog):


   Dim ret As Long
   Dim sCommand, sFiletoDelete As String

   sCommand = "C:\Documents and Settings\Administrator\Desktop\SecureDelete\SDELETE.EXE"
   sFiletoDelete = "C:\Documents and Settings\Administrator\Desktop\SecureDelete\test.txt"
   
   ret = ShellExecute(Me.hwnd, "open", sCommand, "1 " &   chr$(34) & sFiletoDelete chr$(34), vbNullString, vbNormal)
   MsgBox ret
0
 

Author Comment

by:dingo11
Comment Utility
richie
That did not do it.

I don't know if guys have command utilities but I am quite anxious to have a gui to use them.
d
0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
I just tried my last comment with sdelete.exe and it worked fine.

I put a textbox called Text1 and a command button Called Command1 on a form and then I put the arguments in Text1
and clicked the button and the file was deleted.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
wath is the 1 in the shellexecute argument?
0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
Richie has a point,shouldn't it be "-p 1" ? And why did you include the sFiletoDelete string inside the quotes with the argument?
0
 

Author Comment

by:dingo11
Comment Utility
Vin,
I think Richie is asking you what is Space(1) for.

incl the sfiletodelete was a mistake
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
I'm sure Richie knows what Space(1) is for. He is asking you what the "1" is in the shellexecute argument.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
then, shellex...
should be
ret = ShellExecute(Me.hwnd, "open", sCommand, chr$(34) & sFiletoDelete & chr$(34), vbNullString, vbNormal)
?
0
 

Author Comment

by:dingo11
Comment Utility
Yes I guess he does... thats why I am asking and you two are answering.

the first argument in sdelete is passes, the default is 1.
0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
if 1 is the default and you are not using any of the other arguments such as -q or -z,then you do not need to specify the "1". But wasn't the whole point of the question how to pass arguments specified in a Textbox?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
so,
ret = ShellExecute(Me.hwnd, "open", sCommand, chr$(34) & text1.text & chr$(34), vbNullString, vbNormal)
??
or

dim sh as object
set sh =createobject("wscript.shell")
sh.run sCommand & " " & chr$(34) & text1.text & chr$(34)
??

run command of wscript is like if you write command line in Start, Execute
0
 

Author Comment

by:dingo11
Comment Utility
vinny,
I just ran the program at the prompt. -p etc are part of the argument. Leaving out -q displays results. I cannot seem to follow leaving out -s for folders.

Tone is not easily conveyed it print - you seem annoyed?

I guess the point is sometimes depending upon the situation and/or program, I will use this for other programs besides sdelete I may or may not want to run a parameter.
d
0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
Im not sure what you mean by the -s for folders.I just tried -s and it deleted the folder and all files and subfolders within.Is that what you are looking to do?

I was just asking a question,Im not annoyed at all.
Just trying to help.

Vinny

0
 

Author Comment

by:dingo11
Comment Utility
vinny
-s seems a mandatory argument to delete a folder
sdelete -p 3 -q Folder_A
doesn't work
sdelete -p 3 -q test.txt
works

d

0
 

Author Comment

by:dingo11
Comment Utility
vinny
-s seems a mandatory argument to delete a folder
sdelete -p 3 -q Folder_A
doesn't work regarless if it has contents or not
d
0
 
LVL 28

Expert Comment

by:vinnyd79
Comment Utility
d,
Yes,it does seem -s is mandatory argument for sdelete to delete a folder.
0
 

Author Comment

by:dingo11
Comment Utility
vinny,
I wasn't paying attention to detail. This worked perfectly. Thanks,
d
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now