Jump to content
xisto Community
Sign in to follow this  
CaptainRon

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

Recommended Posts

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)

Share this post


Link to post
Share on other sites

Very much usefull

Performing Dos Operations From Visual Basic

 

This is what I searching for since 3 days.

 

Thanks

 

-Krishna S

Share this post


Link to post
Share on other sites
Batch File / GuiPerforming Dos Operations From Visual Basic

Hi,

I have a fairly simple batch file (it allows someone to make a numbered choice, and depending on the choice selected it will copy contents of a folder into another folder). I am looking to put this into the form of a gui, so that someone can select a radio button for the choice (as opposed to typing in a number), and also so I can give it a nice background. This also have to be easily edited (just in case). 

I am looking for guidance on how to make this happen, as I am not a programmer, and do not have the cash flow to contract this out. Any advice would be appreciated.

Mike

-question by Mike

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.