Solved

Run Dos Program and Pass Arguments

Posted on 2002-05-09
22
436 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
ID: 6999670
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
ID: 6999678
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
ID: 6999916
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dingo11
ID: 7001171
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
ID: 7001244
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
ID: 7001280
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
ID: 7001682
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
ID: 7001735
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
ID: 7001759
wath is the 1 in the shellexecute argument?
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 7001780
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
ID: 7001812
Vin,
I think Richie is asking you what is Space(1) for.

incl the sfiletodelete was a mistake
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 7001825
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
ID: 7001871
then, shellex...
should be
ret = ShellExecute(Me.hwnd, "open", sCommand, chr$(34) & sFiletoDelete & chr$(34), vbNullString, vbNormal)
?
0
 

Author Comment

by:dingo11
ID: 7001872
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
ID: 7001897
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
ID: 7001943
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
ID: 7001958
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
ID: 7002019
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
ID: 7002059
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
ID: 7002061
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
ID: 7002114
d,
Yes,it does seem -s is mandatory argument for sdelete to delete a folder.
0
 

Author Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

840 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