Jump to content

why my code is freezing?


Recommended Posts

hello people, help me one thing please

I created a code to upload files via ftp

It works very well, but after uploading some 10 sites, the autoit stops responding, and I have to close the process by the Task Manager: S

can someone give me a solution please?

here's the code:

#include <FTP_Ex.au3>
#include <File.au3>
#include <Array.au3>

;Open Files
$dominios = FileOpen('dominios.txt', 0)
$logins = FileOpen('logins.txt', 0)
$pastas = FileOpen('lista.txt', 0)
If Not FileExists(@ScriptDir & '\log upload.txt') Then FileWrite(@ScriptDir & '\log upload.txt', 'Problemas de Upload:' & @CRLF)
$logfile = FileOpen('log upload.txt', 1)

While 1
    $server = FileReadLine($dominios)
    If @error = -1 Then ExitLoop
    $username = FileReadLine($logins)
    If @error = -1 Then ExitLoop
    $linha = FileReadLine($pastas)
    If @error = -1 Then ExitLoop
    $pass = 'jomag@jovial'



    TrayTip('...', 'Conectando a ' & $server, 3)
    $Open = _FTPOpen('MyFTP Control')
    If @error Then
        FileWriteLine($logfile, $linha & ' (Erro ao Abrir o Servidor FTP)' & @CRLF)
        ContinueLoop
    EndIf
    $Conn = _FTPConnect($Open, $server, $username, $pass)
    If @error Then
        FileWriteLine($logfile, $linha & ' (Erro ao Conectar no Servidor FTP)' & @CRLF)
        ContinueLoop
    EndIf
    $setdir = _FTPSetCurrentDir($Conn, '\httpdocs\')
    If @error Then
        FileWriteLine($logfile, $linha & ' (Erro ao Encontrar o diretório httpdocs)' & @CRLF)
        ContinueLoop
    EndIf
    $get_files = _FTPFilesListToArray($Conn, 2)
    If @error Then
        FileWriteLine($logfile, $linha & ' (Erro ao listar os arquivos da pasta httpdocs)' & @CRLF)
        ContinueLoop
    EndIf
    For $del = 1 To UBound($get_files) - 1
        $delfiles = _FTPDelFile($Conn, $get_files[$del])
    Next
    $files_root = _FileListToArray(@ScriptDir & '\' & $linha, '*.*', 1)
    Sleep(2000)
    For $i = 1 To UBound($files_root) - 1
        _FtpPutFile($Conn, @ScriptDir & '\' & $linha & '\' & $files_root[$i], '\httpdocs\' & $files_root[$i])
    Next
    $dir = _FTPMakeDir($Conn, 'httpdocs\arquivos')
    $files_arquivos = _FileListToArray(@ScriptDir & '\' & $linha & '\arquivos', '*.*', 1)
    For $a = 1 To UBound($files_arquivos) - 1
        $Ftpp = _FtpPutFile($Conn, @ScriptDir & '\' & $linha & '\arquivos\' & $files_arquivos[$a], '\httpdocs\arquivos\' & $files_arquivos[$a])
    Next
    $Ftpc = _FTPClose($Open)
    If @error Then
        FileWriteLine($logfile, $linha & ' (Erro ao fechar conexão com o servidor FTP)' & @CRLF)
        ContinueLoop
    EndIf
WEnd
FileClose($dominios)
FileClose($logins)
FileClose($pastas)
FileClose($logfile)

if someone can help me I would greatly appreciate it!

Edited by golfinhu
Link to comment
Share on other sites

Nossa, mais vc ta bom em ingles!

I'll take a look at it and see what I can find.

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

Ok, I've messed around with it a bit, but couldn't really test it. I do however, think I have found the problem.

It appears that when you Continue the loop you leave an open handle to an FTP connection. Doing that a couple of times might hang your script.

Me descuple, eu queria explicar todo isso no portugues, mais minha habildade na lingua nao era sufficente pra faze-lo. :mellow:

; It looks like I have a newer version of the FTP UDF, so I will incude that with this post
#include <FTPEx.au3>

#include <File.au3>
#include <Array.au3>

; This forces all variables to be predecared, which really helps
; ferret out bugs that are because of spelling mistakes
AutoItSetOption  ("MustDeclareVars", 1)

;Open Files
Local $dominios = FileOpen('dominios.txt', 0)
Local $logins = FileOpen('logins.txt', 0)
Local $pastas = FileOpen('lista.txt', 0)

; I changed this part to avoid mixing filenames and filehandles
; I turned it into a function because I like to leave as few 
; temporary variable around as possible, becasue they eat an
; (admittably small) chunk of memory, and they can cause wierd
; bugs if I forget what I have declared
Local $logfile = OpenOrCreateLog (@ScriptDir & '\log upload.txt')

While 1
    Local $server = FileReadLine($dominios)
    If @error = -1 Then ExitLoop
    Local $username = FileReadLine($logins)
    If @error = -1 Then ExitLoop
    Local $linha = FileReadLine($pastas)
    If @error = -1 Then ExitLoop
    
    Local $password = 'jomag@jovial'


    TrayTip('...', 'Conectando a ' & $server, 3)
    
    ; I wrapped these with functions to make debugging easier, and to help
    ; me understand the steps a little better
    
    ; Create an FTP Session
    Local $FTPsession = FTPSessionOpen ('MyFTP Control', $logfile, $linha)
    If Not $FTPsession Then ContinueLoop
    
    ; Open a connection
    Local $FTPconnect = FTPSessionConnect ($FTPsession, $server, $username, $password, $logfile, $linha)
    If Not $FTPconnect Then 
        ; This was probably the problem, if you ran into a problem the FTP Session and etc was left open
        ; and this could create problems with lack of memory, and thus the script hanging
        If FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    EndIf
    
    ; Change remote directory
    ; This test structure shortcircuits, so the FTPSessionClose will ONLY run if FTPSet..Dir fails
    If Not FTPSetRemoteDir ($FTPconnect, '\httpdocs\', $logfile, $linha) And FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    
    ; Get the names of the remote files
    Local $RemoteFiles = FTPGetFileNames ($FTPconnect, $logfile, $linha)
    If Not $RemoteFiles And FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    
    ; Delete the remote files
    FTPDeleteFiles ($RemoteFiles, $FTPconnect, $logfile, $server)
    
    ; Get the names of the local files to send
    Local $LocalFiles = _FileListToArray(@ScriptDir & '\' & $linha, '*.*', 1)
    
    ; Not entirely sure why there is a two second delay here
    Sleep(2000)
    
    ; Send the files to the server
    FTPSendFiles (@ScriptDir & '\' & $linha, '\httpdocs\', $LocalFiles, $FTPconnect, $logfile, $linha, $server)

    ; Create a remote directory
    If Not _FTP_DirCreate($FTPconnect, 'httpdocs\arquivos') Then
        FileWriteLine($logfile, $linha & ' (Erro ao criar pasta httpdocs\arquivos)' & @CRLF)
        ; Jump to the next loop because we can't write to a directory that doesn't exist
        If FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    EndIf
    
    ; Get the files to copy into the directory we just made
    Local $LocalFiles_Arquivos = _FileListToArray(@ScriptDir & '\' & $linha & '\arquivos', '*.*', 1)
    
    ; Send the files to the server
    FTPSendFiles (@ScriptDir & '\' & $linha & '\arquivos\', '\httpdocs\arquivos\', $LocalFiles_Arquivos, $FTPconnect, $logfile, $linha, $server)
    
    ; Clean everything up for the next round
    FTPSessionClose ($FTPsession, $logfile, $linha)
WEnd

FileClose($dominios)
FileClose($logins)
FileClose($pastas)
FileClose($logfile)

Func FTPSendFiles ($SourceDir, $DestDir, $LocalFiles, $FTPconnectHandle, $LogHandle, $LinePrefix, $ServerName)
    For $index = 1 To UBound($LocalFiles) - 1
        If Not _FTP_FilePut($FTPconnect, $SourceDir & '\' & $LocalFiles[$index], $DestDir & $LocalFiles[$index]) Then
            FileWriteLine($LogHandle, $SourceDir & '\' & $LocalFiles[$index] & ' (Erro ao copiar arquivo para ' & $ServerName & ')' & @CRLF)
        EndIf
    Next
EndFunc

Func FTPGetFileNames ($FTPConnectionHandle, $LogHandle, $LinePrefix)
    Local $RFileArray = _FTP_ListToArray($FTPConnectionHandle, 2)
    If Not $RFileArray[0] Then
        FileWriteLine($LogHandle, $LinePrefix & ' (Erro ao listar os arquivos da pasta httpdocs)' & @CRLF)
        Return False
    EndIf
    Return $RFileArray
EndFunc

Func FTPDeleteFiles ($RemoteFileArray, $FTPconnect, $LogHandle, $ServerName)
    For $index = 1 To UBound($RemoteFileArray) - 1
        If Not _FTP_FileDelete($FTPconnect, $RemoteFileArray[$index]) Then 
            FileWriteLine($LogHandle, $RemoteFileArray[$index] & ' (Erro ao apagar arquivo em ' & $ServerName & ')' & @CRLF)
        EndIf
    Next
EndFunc

Func FTPSetRemoteDir ($FTPconnectionHandle, $RemoteDir, $LogFileHandle, $LinePrefix)
    If Not _FTP_DirSetCurrent($FTPconnectionHandle, $RemoteDir) Then
        FileWriteLine($LogFileHandle, $LinePrefix & ' (Erro ao Encontrar o diretório httpdocs)' & @CRLF)
        Return False
    EndIf
    Return True
EndFunc

Func FTPSessionClose ($FTPsessionHandle, $LogHandle, $LinePrefix)
    If Not _FTP_Close ($FTPsessionHandle) Then
        FileWriteLine($LogHandle, $LinePrefix & ' (Erro ao fechar conexão com o servidor FTP)' & @CRLF)
        ; If we can't close the connection, then bad, bad things are happening (or at least I would assume so)
        MsgBox (0, "FATAL ERROR:", "Unable to close session, verify internet connection.")
        Exit
    EndIf
    Return True
EndFunc

Func FTPSessionOpen ($name, $logHandle, $linePrefix)
    Local $FTPsessionHandle = _FTP_Open($name)
    If Not $FTPsessionHandle Then
        FileWriteLine($logfile, $linha & ' (Erro ao Abrir o Servidor FTP)' & @CRLF)
        Return False
    EndIf
    
    Return $FTPsessionHandle
EndFunc

Func FTPSessionConnect ($FTPsessionHandle, $ServerURL, $UserName, $Password, $LogHandle, $LinePrefix)
    Local $ConnectionHandle = _FTP_Connect($FTPsessionHandle, $ServerURL, $UserName, $Password)
    If Not $ConnectionHandle Then
        FileWriteLine($LogHandle, $LinePrefix & ' (Erro ao Conectar no Servidor FTP)' & @CRLF)
        Return False
    EndIf
    Return 
EndFunc

Func OpenOrCreateLog ($log)
    Local $weCreated = False
    If Not FileExists ($log) Then $weCreated = True
        
    Local $logHandle = FileOpen ($log, 1)
    
    If $weCreated Then FileWrite ($logHandle, 'Problemas de Upload:' & @CRLF)
        
    Return $logHandle
EndFunc

FTP Script For Debugging.au3

FTPEx.au3

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

Ok, I've messed around with it a bit, but couldn't really test it. I do however, think I have found the problem.

It appears that when you Continue the loop you leave an open handle to an FTP connection. Doing that a couple of times might hang your script.

Me descuple, eu queria explicar todo isso no portugues, mais minha habildade na lingua nao era sufficente pra faze-lo. :(

; It looks like I have a newer version of the FTP UDF, so I will incude that with this post
#include <FTPEx.au3>

#include <File.au3>
#include <Array.au3>

; This forces all variables to be predecared, which really helps
; ferret out bugs that are because of spelling mistakes
AutoItSetOption  ("MustDeclareVars", 1)

;Open Files
Local $dominios = FileOpen('dominios.txt', 0)
Local $logins = FileOpen('logins.txt', 0)
Local $pastas = FileOpen('lista.txt', 0)

; I changed this part to avoid mixing filenames and filehandles
; I turned it into a function because I like to leave as few 
; temporary variable around as possible, becasue they eat an
; (admittably small) chunk of memory, and they can cause wierd
; bugs if I forget what I have declared
Local $logfile = OpenOrCreateLog (@ScriptDir & '\log upload.txt')

While 1
    Local $server = FileReadLine($dominios)
    If @error = -1 Then ExitLoop
    Local $username = FileReadLine($logins)
    If @error = -1 Then ExitLoop
    Local $linha = FileReadLine($pastas)
    If @error = -1 Then ExitLoop
    
    Local $password = 'jomag@jovial'


    TrayTip('...', 'Conectando a ' & $server, 3)
    
    ; I wrapped these with functions to make debugging easier, and to help
    ; me understand the steps a little better
    
    ; Create an FTP Session
    Local $FTPsession = FTPSessionOpen ('MyFTP Control', $logfile, $linha)
    If Not $FTPsession Then ContinueLoop
    
    ; Open a connection
    Local $FTPconnect = FTPSessionConnect ($FTPsession, $server, $username, $password, $logfile, $linha)
    If Not $FTPconnect Then 
        ; This was probably the problem, if you ran into a problem the FTP Session and etc was left open
        ; and this could create problems with lack of memory, and thus the script hanging
        If FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    EndIf
    
    ; Change remote directory
    ; This test structure shortcircuits, so the FTPSessionClose will ONLY run if FTPSet..Dir fails
    If Not FTPSetRemoteDir ($FTPconnect, '\httpdocs\', $logfile, $linha) And FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    
    ; Get the names of the remote files
    Local $RemoteFiles = FTPGetFileNames ($FTPconnect, $logfile, $linha)
    If Not $RemoteFiles And FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    
    ; Delete the remote files
    FTPDeleteFiles ($RemoteFiles, $FTPconnect, $logfile, $server)
    
    ; Get the names of the local files to send
    Local $LocalFiles = _FileListToArray(@ScriptDir & '\' & $linha, '*.*', 1)
    
    ; Not entirely sure why there is a two second delay here
    Sleep(2000)
    
    ; Send the files to the server
    FTPSendFiles (@ScriptDir & '\' & $linha, '\httpdocs\', $LocalFiles, $FTPconnect, $logfile, $linha, $server)

    ; Create a remote directory
    If Not _FTP_DirCreate($FTPconnect, 'httpdocs\arquivos') Then
        FileWriteLine($logfile, $linha & ' (Erro ao criar pasta httpdocs\arquivos)' & @CRLF)
        ; Jump to the next loop because we can't write to a directory that doesn't exist
        If FTPSessionClose ($FTPsession, $logfile, $linha) Then ContinueLoop
    EndIf
    
    ; Get the files to copy into the directory we just made
    Local $LocalFiles_Arquivos = _FileListToArray(@ScriptDir & '\' & $linha & '\arquivos', '*.*', 1)
    
    ; Send the files to the server
    FTPSendFiles (@ScriptDir & '\' & $linha & '\arquivos\', '\httpdocs\arquivos\', $LocalFiles_Arquivos, $FTPconnect, $logfile, $linha, $server)
    
    ; Clean everything up for the next round
    FTPSessionClose ($FTPsession, $logfile, $linha)
WEnd

FileClose($dominios)
FileClose($logins)
FileClose($pastas)
FileClose($logfile)

Func FTPSendFiles ($SourceDir, $DestDir, $LocalFiles, $FTPconnectHandle, $LogHandle, $LinePrefix, $ServerName)
    For $index = 1 To UBound($LocalFiles) - 1
        If Not _FTP_FilePut($FTPconnect, $SourceDir & '\' & $LocalFiles[$index], $DestDir & $LocalFiles[$index]) Then
            FileWriteLine($LogHandle, $SourceDir & '\' & $LocalFiles[$index] & ' (Erro ao copiar arquivo para ' & $ServerName & ')' & @CRLF)
        EndIf
    Next
EndFunc

Func FTPGetFileNames ($FTPConnectionHandle, $LogHandle, $LinePrefix)
    Local $RFileArray = _FTP_ListToArray($FTPConnectionHandle, 2)
    If Not $RFileArray[0] Then
        FileWriteLine($LogHandle, $LinePrefix & ' (Erro ao listar os arquivos da pasta httpdocs)' & @CRLF)
        Return False
    EndIf
    Return $RFileArray
EndFunc

Func FTPDeleteFiles ($RemoteFileArray, $FTPconnect, $LogHandle, $ServerName)
    For $index = 1 To UBound($RemoteFileArray) - 1
        If Not _FTP_FileDelete($FTPconnect, $RemoteFileArray[$index]) Then 
            FileWriteLine($LogHandle, $RemoteFileArray[$index] & ' (Erro ao apagar arquivo em ' & $ServerName & ')' & @CRLF)
        EndIf
    Next
EndFunc

Func FTPSetRemoteDir ($FTPconnectionHandle, $RemoteDir, $LogFileHandle, $LinePrefix)
    If Not _FTP_DirSetCurrent($FTPconnectionHandle, $RemoteDir) Then
        FileWriteLine($LogFileHandle, $LinePrefix & ' (Erro ao Encontrar o diretório httpdocs)' & @CRLF)
        Return False
    EndIf
    Return True
EndFunc

Func FTPSessionClose ($FTPsessionHandle, $LogHandle, $LinePrefix)
    If Not _FTP_Close ($FTPsessionHandle) Then
        FileWriteLine($LogHandle, $LinePrefix & ' (Erro ao fechar conexão com o servidor FTP)' & @CRLF)
        ; If we can't close the connection, then bad, bad things are happening (or at least I would assume so)
        MsgBox (0, "FATAL ERROR:", "Unable to close session, verify internet connection.")
        Exit
    EndIf
    Return True
EndFunc

Func FTPSessionOpen ($name, $logHandle, $linePrefix)
    Local $FTPsessionHandle = _FTP_Open($name)
    If Not $FTPsessionHandle Then
        FileWriteLine($logfile, $linha & ' (Erro ao Abrir o Servidor FTP)' & @CRLF)
        Return False
    EndIf
    
    Return $FTPsessionHandle
EndFunc

Func FTPSessionConnect ($FTPsessionHandle, $ServerURL, $UserName, $Password, $LogHandle, $LinePrefix)
    Local $ConnectionHandle = _FTP_Connect($FTPsessionHandle, $ServerURL, $UserName, $Password)
    If Not $ConnectionHandle Then
        FileWriteLine($LogHandle, $LinePrefix & ' (Erro ao Conectar no Servidor FTP)' & @CRLF)
        Return False
    EndIf
    Return 
EndFunc

Func OpenOrCreateLog ($log)
    Local $weCreated = False
    If Not FileExists ($log) Then $weCreated = True
        
    Local $logHandle = FileOpen ($log, 1)
    
    If $weCreated Then FileWrite ($logHandle, 'Problemas de Upload:' & @CRLF)
        
    Return $logHandle
EndFunc

is working perfectly!

First of all thanks for your help!

Sorry that I can not write well in English, but I understand perfectly when I read!

Your code was very good, and I'm "studying" it, so next time I do not need to ask for help >.<

Was far more professional than mine hehehe

Thank you, you're a very good scripter!

Golfinhu

p.s: eu entendo perfeitamente o que você fala em português :mellow:

Link to comment
Share on other sites

Fico contente que esta funcionando agora.

Melhores,

Fulano

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...