Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Performing Dos Operations From Visual Basic, The easiest and most powerful way to do
CaptainRon
post Sep 13 2005, 06:05 PM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 238
Joined: 9-September 05
Member No.: 8,400



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)
Go to the top of the page
 
+Quote Post
iGuest
post Jan 14 2008, 07:23 AM
Post #2


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Very much usefull
Performing Dos Operations From Visual Basic

This is what I searching for since 3 days.

Thanks

-Krishna S
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 21st August 2008 - 09:14 PM