Sign in to follow this  
Followers 0
strate

Need help error proofing

2 posts in this topic

I'm in the early steps of creating a script to automate the transfer of two files thru FTP. Instead of reading for when a file transfered incorrectly I'm reading for when it transferred correctly. Our FTP after a good transfer returns: 226 transfer complete. Basically I need the "226". At the same time since this is my first time doing something like this I have each step write to a file, then the next step will read from the file to ensure that it is the correct next step. I hope this makes sense. It "works" but I know it can be done better. The code in its whole is at the bottom of the post the section worth looking at though follows.

If StringInStr($currentRead, $Split[1]&"\>") <> 0 Then
                If StringInStr($currentRead, $Path) = 0 Then
                    If FileReadLine($StatusFile,1) = 'FTP Command Open' Then
                        FileDelete($StatusFile)
                        StdinWrite($foo,"cd " & $Path & @LF)
                        FileWrite($StatusFile,'Change Dir' & @CRLF)
                    EndIf
                EndIf
            EndIf
            If StringInStr($currentRead, $Path) <> 0 Then
                If FileReadLine($StatusFile,1) = 'Change Dir' Then
                    FileDelete($StatusFile)
                    StdinWrite($foo,'ftp -v -n qaddb2' & @LF)
                    FileWrite($StatusFile,'ftp -v -n qaddb2' & @CRLF)
                EndIf
            EndIf
            If StringInStr($currentRead, '220 ') <> 0 Then
                If FileReadLine($StatusFile,1) = 'ftp -v -n qaddb2' Then
                    FileDelete($StatusFile)
                    WinSetTitle($Command_Prompt& ' - ftp -v -n qaddb2', "", "FTP COMMAND")
                    WinActivate("FTP COMMAND")
                    WinWaitActive("FTP COMMAND")
                    StdinWrite($foo,'USER ' & $QAD_Login_Name & ' ' & $QAD_Password & @CRLF)
                    FileWrite($StatusFile,'Entered USER data' & @CRLF)
                EndIf
            EndIf

#include <Array.au3>

$QAD_Login_Name = 'USER'
$QAD_Password = 'PASSWORD'
$Command_Prompt = @ComSpec
$StatusFile = @TempDir & '\FTP.log'

;~ FileWrite($StatusFile,'Prompt Open' & @MON & '/' & @MDAY & '/' & @YEAR & '   ' & @HOUR & ':' & @MIN & ':' & @SEC & @CRLF)

$Path = ''
$Split = StringSplit($Command_Prompt,'\')
For $i = 2 To UBound($Split)-2
    If $i = UBound($Split)-2 Then
        $Path = $Path & $Split[$i]
    Else
        $Path = $Path & $Split[$i] & '\'
    EndIf
Next

FileDelete($StatusFile)
WinKill('FTP COMMAND')


If Not WinExists("FTP COMMAND") Then
    FileWrite($StatusFile,'FTP Command Open' & @CRLF)
    Global $foo = Run($Command_Prompt,$Split[1], @SW_SHOW,3)
    WinActivate($Command_Prompt)
    WinWaitActive($Command_Prompt)
    WinSetTitle($Command_Prompt, "", "FTP COMMAND")
    WinActivate("FTP COMMAND")
    WinWaitActive("FTP COMMAND")
EndIf

AdlibEnable('_FooMonitor')

While 1
    If $foo Then
        $charsWaiting = StdoutRead($foo, -1 ,1)
        If @error = -1 Then
            $ourProcess = 0
            MsgBox(0, "App Exited", "Process has exited...")
            ExitLoop
        EndIf
        If $charsWaiting Then
            $currentRead = StdoutRead($foo, -1 , 0)
            ConsoleWrite($currentRead)
            $Read = Stringsplit($currentRead,@LF)
            
            Dim $Transfer[3][3]
            $Transfer[1][1] = 'schedule.prn'
            $Transfer[1][2] = ''
            $Transfer[2][1] = 'export.txt'
            $Transfer[2][2] = ''

            If StringInStr($currentRead, $Split[1]&"\>") <> 0 Then
                If StringInStr($currentRead, $Path) = 0 Then
                    If FileReadLine($StatusFile,1) = 'FTP Command Open' Then
                        FileDelete($StatusFile)
                        StdinWrite($foo,"cd " & $Path & @LF)
                        FileWrite($StatusFile,'Change Dir' & @CRLF)
                    EndIf
                EndIf
            EndIf
            If StringInStr($currentRead, $Path) <> 0 Then
                If FileReadLine($StatusFile,1) = 'Change Dir' Then
                    FileDelete($StatusFile)
                    StdinWrite($foo,'ftp -v -n qaddb2' & @LF)
                    FileWrite($StatusFile,'ftp -v -n qaddb2' & @CRLF)
                EndIf
            EndIf
            If StringInStr($currentRead, '220 ') <> 0 Then
                If FileReadLine($StatusFile,1) = 'ftp -v -n qaddb2' Then
                    FileDelete($StatusFile)
                    WinSetTitle($Command_Prompt& ' - ftp -v -n qaddb2', "", "FTP COMMAND")
                    WinActivate("FTP COMMAND")
                    WinWaitActive("FTP COMMAND")
                    StdinWrite($foo,'USER ' & $QAD_Login_Name & ' ' & $QAD_Password & @CRLF)
                    FileWrite($StatusFile,'Entered USER data' & @CRLF)
                EndIf
            EndIf
            If StringInStr($currentRead, '230 ') <> 0 Then
                If FileReadLine($StatusFile,1) = 'Entered USER data' Then
                    FileDelete($StatusFile)
                    StdinWrite($foo,'dir' & @CRLF)
                    FileWrite($StatusFile,'FTP: Dir' & @CRLF)
                EndIf
            EndIf
            If StringInStr($currentRead, '226 ') <> 0 Then
                $TComplete = 1
                If FileReadLine($StatusFile,1) = 'FTP: Dir' Then
                    FileDelete($StatusFile)
                    FileWrite($StatusFile,'Get Transfer' & @CRLF)
                    _TransferFiles()
                    Exit
                EndIf
            EndIf
        EndIf
    EndIf
WEnd

While 1
    $line = StderrRead($foo)
    If @error = -1 Then ExitLoop
    MsgBox(0, "STDERR read:", $line)
Wend
Exit

Func _TransferFiles()
    While 1
        $charsWaiting = StdoutRead($foo, -1 ,1)
        If $charsWaiting Then
            $currentRead = StdoutRead($foo, -1 , 0)
            ConsoleWrite($currentRead)
        EndIf
        If StringInStr($charsWaiting, 'Local directory now C:\.') = 0 Then 
            If FileReadLine($StatusFile,1) = 'Get Transfer' Then
                FileDelete($StatusFile)
                StdinWrite($foo,'lcd c:\' & @CRLF)
                FileWrite($StatusFile,'Change FTP Dir' & @CRLF)
            EndIf
        EndIf
        For $i = 1 To 2     
            $charsWaiting = StdoutRead($foo, -1 ,1)
            FileDelete('C:\'&$Transfer[$i][1])
            $begin = TimerInit()
            While 1
                StdinWrite($foo,'get '& $Transfer[$i][1] & @CRLF)
                Sleep(1000)
                $charsWaiting = StdoutRead($foo, -1 ,1)
                If StringInStr($charsWaiting, '226 ') <> 0 Then
                    ConsoleWrite('Transfered Correctly: '& $Transfer[$i][1] & @CRLF)
                    ExitLoop
                EndIf
                $dif = TimerDiff($begin)
                If $dif = 60000 Then; 60000 = 1 min
                    MsgBox(0,'Notice','An error has occured while transfering: ' & $Transfer[$i][1] & @CRLF & 'please retry.')
;~                  ExitLoop(2) ????
                EndIf
            WEnd
            $charsWaiting = StdoutRead($foo, -1 ,1)
            If $i = 2 Then ExitLoop(2)
        Next
    WEnd
EndFunc

Func _FooMonitor()
    If Not ProcessExists($foo) Then
;~      MsgBox(0,'Notice','Foo no more!')
        Exit
    EndIf
EndFunc
Any help would be greatly appreciated.

INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...

Share this post


Link to post
Share on other sites



Is this correct then??


INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0