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 Mike Lewis 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 program in background effectively

Tips -N- Tricks

How to run DOS program in background effectively

by  AirCon  Posted    (Edited  )
When we use RUN command with /N7, sometimes the program doesn't execute properly or not run at all. My suspect is, VFP execute the command and get back to VFP too fast, so it doesn't have enough time to create DOS environment.

In order to run DOS program running in background properly we can simply call DOS Commander (Command.com for Win9X/ME, Cmd.exe for NT/2K/XP) and let it do the job for our application, while we can continue our program.


Thanks to:
1. Jim Osieczonek (jimoo)
For encouraging me to make FAQ. So this is my first time :)

2. Ramani - (Subramanian.G)
For the correction and provided another example


*** ---------------[color blue]
[color green]** Example 1, using Run/! command[/color]
If file('test.txt')
Erase test.txt
endif
lcCmd = GetCommander() + ' ping localhost > test.txt'
Clear
? 'Using RUN command. Pinging localhost, wait...'
RUN /N7 &lcCmd
= inkey(8)
If file('test.txt')
lcString = FileToStr('test.txt')
? lcString
= inkey(0)
else
?? ' Failed!'
endif


[color green]** Example 2, using ShellExecute[/color]
If file('test.txt')
Erase test.txt
endif
Declare Integer ShellExecute in Shell32.dll ;
Long nhWnd, String cAction, String cFile, ;
String cParam, String cDirectory, Integer nShow
lcCmd = GetCommander(.T.)
lcParam = '/C ping localhost > test.txt'
Clear
? 'Using ShellExecute. Pinging localhost, wait...'
ShellExecute(0, Null, lcCmd, lcParam, Null, 0)
= inkey(8)
If file('test.txt')
lcString = FileToStr('test.txt')
? lcString
= inkey(0)
else
?? ' Failed!'
endif


[color green]** Example 3, using Windows Script
** Provided by: Ramani
** WSH must exist on client computer[/color]
If file('test.txt')
Erase test.txt
endif
loShell = CreateObject("wscript.shell")
lcCmd = GetCommander()
lcCmd = lcCmd + ' ping localhost > test.txt'
Clear
? 'Using Windows Script. Pinging localhost, wait...'
loShell.Run(lcCmd, 0, .f.)
= inkey(8)
If file('test.txt')
lcString = FileToStr('test.txt')
? lcString
= inkey(0)
else
?? ' Failed!'
endif


[color green]** The function[/color]
Function GetCommander(tlFilename)
Local lcDir, lcCmd
[color green]** Use COMSPEC instead of WINDIR. Corrected by: Ramani[/color]
lcDir = AddBS(JustPath(GetEnv('ComSpec')))
If tlFilename && get the filename only
If file(lcDir + 'Cmd.exe') && Is NT ?
lcCmd = 'Cmd.exe'
else
lcCmd = 'Command.com'
endif
else
If file(lcDir + 'Cmd.exe')
lcCmd = lcDir + 'Cmd.exe /C'
else
lcCmd = lcDir + 'Command.com /C'
endif
endif

Return lcCmd
EndFunc
[/color]
*** ---------------
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top