• 0

[problem] Using VB6 to call dos program


Question

ok im trying to run FC.exe (file checker, sees if there are any differences in files)

here is my code:

[in the general declerations]Const SYNCHRONIZE = &H100000 'contants for cmdShell

Const INFINITE = &HFFFF

[the function itself]Private Sub cmdShell(Command As String)

'i didnt write any of this, copied from RCRS version1

Dim lPid As Long

Dim lHnd As Long

Dim lRet As Long

lPid = Shell(Command, vbHide) 'get Processor ID number of command shell

Debug.Print Command

If lPid <> 0 Then

lHnd = OpenProcess(SYNCHRONIZE, 0, lPid) 'Get a handle to the shelled process.

If lHnd <> 0 Then 'If successful, wait for the

lRet = WaitForSingleObject(lHnd, INFINITE) ' application to end.

CloseHandle (lHnd) 'Close the handle.

End If

'MsgBox "Just terminated.", vbInformation, "Shelled Application"

End If

End Sub

[the call to the function]'now check out the files and pipe it to a change file

Call cmdShell("cmd /c C:\winnt\system32\fc.exe " & App.Path & "\new.txt " & App.Path & "\old.txt /N /W > " & App.Path & "\changes.txt")

when i run the program, fc doesnt do anything, it never write the changes.txt file! I dont get any errors at all, just nothing happens. What do you suggest would be the problem?

thanks!

steve

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

Perhaps you could place your command line in a batch file and execute the batch file instead of performing a direct call.

Alternatively, a worthwhile try would be to specify the full path and file name for cmd.

The following works for me without a problem.

Shell "C:\windows\system32\cmd.exe /C C:\windows\system32\fc.exe C:\test.txt C:\test2.txt > C:\result.txt"

Link to comment
Share on other sites

  • 0

It worked for me. Are you running windows 2000? The only reason I ask is that you have your windows directory as winnt. I haven't seen that in a while. :) Your system32 directory should be specified in the environment variables, so you shouldn't need to specify the full path to fc.exe.

Link to comment
Share on other sites

  • 0

Yes i am running windows 2000. I made a variable called cmd that contains the whole string command i want "C:\fc.exe C:\new.txt C:\old.txt /N /W > changes.txt" and it STILL doesnt save anything to changes.txt.

I can however get it to work if i actually run it from a command window, but that doesnt help my program :)

Would it be easier if i saved the output to a variable instaed of a text file? I'm not sure how i would go about doing that either.

Link to comment
Share on other sites

  • 0

Another thought. You are using the app.path variable. Does this path include spaces? If so, you need to have quotes around your path/filenames.

Link to comment
Share on other sites

  • 0

Very strange.

this worked for me.

"cmd /c fc.exe " &amp; App.Path &amp; "\new.txt " &amp; App.Path &amp; "\old.txt /N /W &gt; " &amp; App.Path &amp; "\changes.txt"

The only thing I can figure is that your files don't exist in the directory specified, but I'm not 100%.

Link to comment
Share on other sites

  • 0

That could be true, but from what i have read about the redirect operator '>', if the file does not exist then it will create it....

i am going through the debugging right now and seeing if all the PID and stuff work out.. back in 10 :)

Link to comment
Share on other sites

  • 0

I got it to work finally, not sure what the problem was, but i ended up with this

Call cmdShell("cmd /c C:\winnt\system32\fc.exe " & NewConfigName & " " & OldConfigName & " /N /W > " & Router_Array(i) & ".chg")

whatever works and makes my boss happy, i guess :D

this program is going to be the death of me, word to the wise, internships suck/rock.

Link to comment
Share on other sites

  • 0

The real problem was that app.path returns the FULL path including the final '\', so it turned out i was using two \\s in a row :(

Link to comment
Share on other sites

  • 0

That is why I don't use app.path like that.

This is why I do the following:

dim strAppPath as string

strAppPath=app.path

if(right(strAppPath,1)<>"\") then

strAppPath=strAppPath & "\"

end if

Link to comment
Share on other sites

  • 0

Thats MS for you :)

Another question, is it possible to turn on line numbering with VB6? I keep getting hits on MSDN about .NET insetad

Link to comment
Share on other sites

  • 0

why that huge cmdShell routine? Does Vb's shell use ntdvm (NT DOS Virtual Machine) like QB or something? (i know i had to use cmd /c for qb.)

Link to comment
Share on other sites

  • 0

The routine doesn't just launch an external program. It get the handle for the process and waits for the process to complete before continuing.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.