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
*** --------------- ** Example 1, using Run/! command 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
** Example 3, using Windows Script ** Provided by: Ramani ** WSH must exist on client computer 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
** The function Function GetCommander(tlFilename) Local lcDir, lcCmd ** Use COMSPEC instead of WINDIR. Corrected by: Ramani 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