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

free web hosting
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)

 

 

 


Reply

iGuest
Very much usefull
Performing Dos Operations From Visual Basic

This is what I searching for since 3 days.

Thanks

-Krishna S

Reply


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*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. vba dos commands - 8.96 hr back. (1)
  2. visual basic batch query - 13.04 hr back. (1)
  3. dos variable vbs - 13.92 hr back. (1)
  4. create and run a batch file in vb6 - 15.08 hr back. (1)
  5. vb6 run dos - 16.65 hr back. (1)
  6. how to give commands to print a file using visual basic - 19.10 hr back. (1)
  7. dos operations - 30.99 hr back. (1)
  8. dos commands in vba - 30.99 hr back. (1)
  9. writing in dos with vb - 32.77 hr back. (1)
  10. create batch file in vb6 - 35.77 hr back. (1)
  11. run dos command in vb6 - 36.46 hr back. (1)
  12. dos command in vb6 - 41.02 hr back. (1)
  13. run dos command in visual basic - 60.92 hr back. (1)
  14. vb link with dos - 65.86 hr back. (1)
Similar Topics

Keywords : performing, dos, operations, visual, basic, easiest, powerful

  1. Lesson1 :introduction To Visual Basic
    (2)
  2. Text File Operations VB.NET
    (5)
    In this tutorial you will learn most of the operations you can use on a text file. They include
    finding if a file exists, opening/creating a file, reading/writing file, closing file, copying
    file, deleting file. You will need a form with two buttons on it. Use the names Step1 and Step2.
    First thing we are going to do is import system.IO. To do this go into the forms code view. At
    the very top add this line. CODE Imports system.IO This lets us use the file operations
    that we need for this tutorial. Next we need to check to see if a file exists. To ....

    1. Looking for performing, dos, operations, visual, basic, easiest, powerful

*RANDOM STUFF*





*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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE