Checking for DOS executions (1 Viewer)

ajetrumpet

Banned
Local time
Today, 05:57
Joined
Jun 22, 2007
Messages
5,638
all, I have this line of code:
PHP:
Shell "ftp -n -i -g -s:" & vPath & "\FtpComm.txt " & vFTPServ, vbhide
it executes a series of ftp commands in the DOS window using the txt file that is referenced. What I want is an indication of when the file has stopped executing and the DOS window is closed. That will tell me that my files are done uploading or downloading. Does anyone know if this is possible? Thanks!


the txt file uses the following code:
PHP:
vPath = "C:"
vFTPServ = Me.Combo1
vFile = Me.List27
fNum = FreeFile()

Open vPath & "\FtpComm.txt" For Output As #fNum
Print #1, "USER " & sFtpUserName ' your login
Print #1, sFtpPassword ' your password
Print #1, "put " & """" & CurDir & vFile & """"
Print #1, "close" ' close connection
Print #1, "quit" ' Quit ftp program
Close
 

ChrisO

Registered User.
Local time
Today, 21:57
Joined
Apr 30, 2003
Messages
3,202
Are you running that from Access?
Does your txt file wait till it is finished?

If so and all else fails why not have your code create a Done.txt file in the same directory when the process is finished?

Then before calling the txt file Kill the Done.txt file. You should then be able to loop with DoEvents and a Sleep 500 then pull out of loop after X loops are done or until the Done.txt file is created.
 

ajetrumpet

Banned
Local time
Today, 05:57
Joined
Jun 22, 2007
Messages
5,638
Are you running that from Access?
Does your txt file wait till it is finished?

If so and all else fails why not have your code create a Done.txt file in the same directory when the process is finished?

that's a great point Chris, and as a matter of fact I already tried that, but the problem lies in the way the VBA code executes the FTP stuff. It shells the file, but then goes immediately on to the next line of VBA coding.

You see, if I input your suggestion, the FTP code inside the txt would delete the file at the beginning, then kill it at the end, but if that is all done inside of the FTP commands in the text file it does me no good because the VBA module keeps running after the SHELL line. THAT is what I need to hault.

Your suggestion is a great one, but it doesn't do the job as I've already tried it. Is there no other way around this?


I guess in short, I can say that the VBA module can be completely gone through before the txt commands even log me in to my FTP server. that's how slow the shell is at carrying out the commands compared to the execution of the VBA after it.
 

ChrisO

Registered User.
Local time
Today, 21:57
Joined
Apr 30, 2003
Messages
3,202
>>It shells the file, but then goes immediately on to the next line of VBA coding.<<

Put the VBA in a loop waiting for the FTP code to create the Done.txt file.
 

ajetrumpet

Banned
Local time
Today, 05:57
Joined
Jun 22, 2007
Messages
5,638
>>It shells the file, but then goes immediately on to the next line of VBA coding.<<

Put the VBA in a loop waiting for the FTP code to create the Done.txt file.
Chris,

I have tried this, but I do not know how to create a txt file or directory on my local computer when I'm in the FTP prompt.

I tried to do in after printing the "!" character to get back to the CMD prompt side of it, but then the print commands in VBA do not work. assistance please?
 

ajetrumpet

Banned
Local time
Today, 05:57
Joined
Jun 22, 2007
Messages
5,638
chris,

here is the module in completion when I tried what you had said, but the print commands after getting back to the CMD via the "!" did not work, so I stopped trying this method.
PHP:
If MsgBox("This may take a few minutes" & vbCr & _
          "depending on the file size.  Continue?", vbYesNoCancel, "Confirm") = vbYes Then
          
Dim fso
Dim oFile
Dim CurDir As String
Dim CurRemoteDir As String
Dim vPath As String
Dim vFile As String
Dim vFTPServ As String
Dim fNum As Long

Screen.MousePointer = 11

Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.createtextfile("c:\inProgress.txt", True)
   oFile.writeline "contents"
   oFile.Close

vPath = "C:"
vFTPServ = Me.Combo1
CurDir = Me.Text26
CurRemoteDir = Me.Text1
vFile = Me.List27
fNum = FreeFile()

Open vPath & "\FtpComm.txt" For Output As #fNum
Print #1, "USER " & sFtpUserName ' your login
Print #1, sFtpPassword ' your password
Print #1, "dir > c:\inProgress.txt" 'CREATE THE PROGRESS FILE ON LOCAL MACHINE

If sFtpHostName <> Me.Combo1 Then
   Print #1, "cd " & Left( _
                     Right(CurRemoteDir, Len(CurRemoteDir) - InStr(CurRemoteDir, "/")), _
                     (Len(CurRemoteDir) - 1))
End If

Print #1, "put " & """" & CurDir & vFile & """"
Print #1, "!" 'GO BACK TO THE CMD PROMPT HERE
Print #1, "del c:\inProgress.txt"
Print #1, "exit"
Print #1, "close" ' close connection
Print #1, "quit" ' Quit ftp program
Close

Shell "ftp -n -i -g -s:" & vPath & "\FtpComm.txt " & vFTPServ, vbMaximizedFocus

While Dir("c:\inProgress.txt") <> ""
   DoEvents
Wend

Set fso = Nothing
Set oFile = Nothing

Screen.MousePointer = 0

End If
 

ChrisO

Registered User.
Local time
Today, 21:57
Joined
Apr 30, 2003
Messages
3,202
Slow down, I can't write that fast...
General flow but still Airware...


Kill vPath & "\Done.txt"

Shell "ftp -n -i -g -s:" & vPath & "\FtpComm.txt" & vFTPServ, vbHide

Do While Dir(vPath & "\Done.txt") = ""
DoEvents
Sleep 500
X = X + 1
If X > 10 Then Exit Do
Loop

If X > 10 Then
MsgBox "Operation timeout."
End If


You are not using fNum but let's assume the rest works.


vPath = "C:"
vFTPServ = Me.Combo1
vFile = Me.List27
fNum = FreeFile()

Open vPath & "\FtpComm.txt" For Output As #fNum
Print #1, "USER " & sFtpUserName ' your login
Print #1, sFtpPassword ' your password
Print #1, "put " & """" & CurDir & vFile & """"
Print #1, "close" ' close connection
Print #1, "quit" ' Quit ftp program
Close
Open vPath & "\Done.txt" For Output As #fNum
Print #1, "All done."
Close
 

ajetrumpet

Banned
Local time
Today, 05:57
Joined
Jun 22, 2007
Messages
5,638
i am going to try that out as well, but guess what? I got something that works great from a DOS guru. check this out:
PHP:
Option Explicit

Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

Private Type STARTUPINFO
cb As Long
lpReserved As Long
lpDesktop As Long
lpTitle As Long
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwflags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Byte
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type OVERLAPPED
    internal As Long
    internalHigh As Long
    offset As Long
    OffsetHigh As Long
    hEvent As Long
End Type

Private Const STARTF_USESHOWWINDOW = &H1
Private Const STARTF_USESTDHANDLES = &H100
Private Const SW_HIDE = 0
Private Const SW_SHOWDEFAULT As Long = 10

Private Const EM_SETSEL = &HB1
Private Const EM_REPLACESEL = &HC2
Public Declare Function CreatePipe Lib "kernel32" (phReadPipe As Long, phWritePipe As Long, lpPipeAttributes As SECURITY_ATTRIBUTES, ByVal nSize As Long) As Long
Private Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetThreadDesktop Lib "User32.dll" (ByVal dwThread As Long) As Long
Private Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByRef lpOverlapped As Any) As Long

Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Const WAIT_TIMEOUT As Long = 258&

Public Sub ExecWait(cmdLine As String, Optional ByVal WindowShowMode As Long = 10)
    Dim i%, t$
    Dim pa As SECURITY_ATTRIBUTES
    Dim pra As SECURITY_ATTRIBUTES
    Dim tra As SECURITY_ATTRIBUTES
    Dim pi As PROCESS_INFORMATION
    Dim sui As STARTUPINFO
    Dim hRead As Long
    Dim hWrite As Long
    Dim bRead As Long
   
    Dim lpBuffer As String, wholestr As String
 
    
 
        sui.cb = Len(sui)
        GetStartupInfo sui
        sui.dwflags = STARTF_USESHOWWINDOW Or STARTF_USESTDHANDLES
        sui.wShowWindow = WindowShowMode
        If CreateProcess(vbNullString, cmdLine, pra, tra, 1, 0, ByVal 0&, vbNullString, sui, pi) <> 0 Then
            'SetWindowText objTarget.hwnd, ""
            'If Not GetThreadDesktop(pi.hThread) = 0 Then
            'insert waitforsingleobject loop here...
            Dim rtret As Long
            Do
                rtret = WaitForSingleObject(pi.hProcess, 100)
                If rtret <> WAIT_TIMEOUT Then
                    Exit Do
                End If
                DoEvents
            
            Loop
           
        End If

End Sub
all you need to do is call the sub with the FTP command line argument I have in my function. Unbelievable how I got it. Thank you for the help too. I am going to see which way is faster.
 

ajetrumpet

Banned
Local time
Today, 05:57
Joined
Jun 22, 2007
Messages
5,638
chris,

your solution did the trick and the one that I picked up did it too. Yours is faster so I think I'm gonna go with it. thanks again man! =)
 

ChrisO

Registered User.
Local time
Today, 21:57
Joined
Apr 30, 2003
Messages
3,202
No problem Adam, but I’m a little surprised it actually worked. :)

Regards,
Chris.
 

Users who are viewing this thread

Top Bottom