Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run Dos Program and Pass Arguments

Posted on 2002-05-09
22
Medium Priority
?
454 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 340 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

721 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