Nov 21, 2009

Performing Dos Operations From Visual Basic - The easiest and most powerful way to do

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > .NET (VB, C# & J#)

Performing Dos Operations From Visual Basic - The easiest and most powerful way to do

CaptainRon
Performing DOS Operations from Visual Basic

By: Abhishek Chatterjee

Language: Visual Basic 6.0 and below

Difficulty: Beginner

Does your project need to perform some DOS based or Command line operations? Although there are many techniques to do the same, but performing tasks like calling the DIR DOS command to list the contents of the directory or calling the Move command to move a folder, and the like, can't be done as smoothly by the built in functions of VB or the Windows API.

The technique that I introduce to you is extremely simple, doesn't require any high level programming knowledge of Windows API or anything else. All you need to know to be able to work smoothly is to know File handling using VB. Although I do introduce you to some basic File I/O concepts, its suggested that you read further about it.

The Technique:

Whenever you needed to do a DOS task repeatedly, what did you do? Create a shell program (Batch file) and run it. Well that is exactly what we are about to do here. All those of you who know File I/O well, are already on track of introducing DOS operations into their projects. Just follow these steps:

* Start your project and create a new function/subroutine that will create a batch file and execute it.
* Creating a batch file is simple. Create a variable called strFileText which will hold the contents of the batch file.
* Suppose you want to use the move command to move a folder from a specified location to another. So the function declaration will look something like this:

Public Function MoveFolder(strSource as String, strDestination as String)

Now generate a DOS command from the parameters passed above and store in the buffer variable declared above called strFileText.

strFileText = "move " & strSource & " " & strDestination

Now write the file to disk by using File I/O as shown below.

* fnum = FreeFile() 'Assign a free file no. to fnum
* Open app.path & "\dos.bat" For Output As fnum 'Let the .bat file be in the same location as your .exe
* Print #fnum, strFileText 'Write the contents of strFileText to the batch file.
* Close #fnum


Now when the Batch file is written to disk, all we need to do is call it.

Shell( App.Path & "\dos.bat", vbHide)

The batch file will execute without showing itself on the screen and exit after its done.
Since this example was a very basic one, anyone who knows about Dos and Batch files, its no big deal. You can create a fully customized Batch file by controlling the contents of strFileText.

Now, suppose you run a DOS program, and you want to see what was the output, or let your software know about the output, you must do the good old piping tactic.

This command will store the output in a text file:

C:\>DIR *.* > output.txt

The complete C Drive directory listing is stored in a file called output.txt. All you need to do is open it using File I/O and read the contents.

To give input to a program do the following:

Store the sequential key inputs in a text file. Then issue the input pipe as follows:

C:\>flames.exe < input.txt

So by now many of you would have already made plans to make GUI interfaces to some XP console commands... eh?
If someones tries things out, please make GUI's for CHKDSK and the NET commands. (I am too lazy to spend my weekends on these)

 

 

 


Comment/Reply (w/o sign-up)

FeedBacker
Very much usefull
Performing Dos Operations From Visual Basic

This is what I searching for since 3 days.

Thanks

-Krishna S

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for performing, dos, operations, visual, basic, easiest, powerful
advertisement



Performing Dos Operations From Visual Basic - The easiest and most powerful way to do

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com