Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run DOS commands from inside my program ?? 1

Status
Not open for further replies.

rahmanjan

Programmer
Apr 13, 2003
180
AU
hi all,

Is it possible to rub a DOS command from my program. Eg i want to run:

Xcopy c:\xx c:\yy /s /y /c

1-is it possible at all?
2-Can i pass parrameters eg instead of c:\xx it might be any variable.

any other alternatives?

regards
 
is it essential to use dos to copy the file??

can u not use:-

filecopy statement

or with fso you can use:-

copyfile

or

copy

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
In c, you can use "execute"!

In vb, you can use winapi!
 
yup API's


and


might be a little overkill though!!!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
you can use shell to execute DOS commands. Good luck
 
In VB6, I think you can use the "Run" command.

Run "Xcopy c:\xx c:\yy /s /y /c"

or something similar.....(check out help)
 
Geez, going around the Matterhorn.

The simplest approach to execute a DOS command from within VB, with parameters, is the first build your command line as a string.
Code:
FromFile = "c:\xx"  ' or other directory
ToFile = "c:\yy"    ' where it needs to go
CopyParams = "/s /y /c"  ' specific options
CmdLine "xcopy " & FromFile & " " & ToFile & " " & CopyParams
Then pass that string to the Shell function.
Code:
Shell CmdLine, vbNormalFocus ' or other Window setting
If the file you want to "execute" is not an executable file (.exe, .com, or .bat), then you need to investigate the ShellExecute API.


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
External DOS command like LABEL and XCOPY work fine with Shell statement. To execute an internal DOS command like COPY or DEL, you need to invoke command.com with /c switch following that internal command. For example;

Shell "COMMAND.COM /C COPY C:\AUTOEXEC.BAT C:\AUTOEXEC.BAK"

will make a backup copy of autoexec.bat.

However, it is recommended to stick to VB internal commands and avoid the use of DOS commands for these simple file operations. If you need to copy folders including subfolders, look for SHFileOperation API.
 
How can I get the IP adress of my pc from the dos command ipconfig into a textbox in a program?
 
It is possible to redirect the output of IPCONFIG.EXE to a file through console programming but there are other simple ways to obtain the IP address of your PC.
Simplest way is to use Winsock control and look into LocalIP property.
There are also other ways to get the IP address through API. You can search this forum to find some solutions.

 
is there a way to recieve return code when I use the dos commands from the VB ??
 
I think, this will do the job.
___
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const SYNCHRONIZE = &H100000
Const PROCESS_QUERY_INFORMATION = &H400
Const INFINITE = -1&

Private Sub Form_Load()
Dim ProcessId As Long, hProcess As Long, ExitCode As Long
ProcessId = Shell("notepad.exe", vbNormalFocus)
hProcess = OpenProcess(SYNCHRONIZE Or PROCESS_QUERY_INFORMATION, 0, ProcessId)
'Stop here until the program finishes
'otherwise GetExitCodeProcess will fail.
WaitForSingleObject hProcess, INFINITE
GetExitCodeProcess hProcess, ExitCode
CloseHandle hProcess
MsgBox "Exit code = " & ExitCode
End
End Sub
___
Microsoft say that the SYNCHRONIZE flag is specified with OpenProcess function to use the return handle with wait functions(WaitForSingleObject). This flags works only with Windows NT systems. So if you want to run this code on Windows 98 then you must execute the program with CreateProcess API to obtain the process handle directly. This handle will be used with both the functions, i.e. WaitForSingleObject and GetExitCodeProcess.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top