Sign in to follow this  
Followers 0
digitalexpl0it

Help with Nested For loop

5 posts in this topic

#1 ·  Posted (edited)

Hello,

 

I am trying to updated a autoit app that moves files from one location to another. What Ia m trying to do is exclude specified sub directories from being moved/copied or files within the sub-folders

As of now the code doesn't want to execute the nested for loop 

 

Global $szDrive, $szDir, $szFName, $szExt
Global $File = "*"

func File_mover($Src,$File,$Dst)
    ; GET DIRECTORY EXCLUDES
    ; ######################
    
    ; Create blank 2 dem array
    Local $ExArray[0][1]
    ; load ini file
    Local Const $sFilePath = "exclude.ini"
    ; Check to see if ini exists
    Local $iFileExists = FileExists($sFilePath)
    ; If the INI file is not found, output error message
    If not $iFileExists Then
        msgbox(0,"Oh NO!", $sFilePath & " not found!")
    endif
    ; Read ini file
    Local $aArray = IniReadSection($sFilePath, "test")
    ; Start the array loop
    If Not @error Then
        for $i = 1 to $aArray[0][0]
            ; Add Ini values into array
            _ArrayAdd($ExArray, $aArray[$i][1])
        next
    endif
    ; Display array
    ;_ArrayDisplay($ExArray, "test Label")


    ProgressOn("Moving Scanned File(s)", "Moving scans into citrix...", "0%")

    $aFiles = _FileListToArray3($Src, $File, 1, 1, 0, 0)

    For $i = 1 To $aFiles[0]
        call("_PathSplit",$aFiles[$i], $szDrive, $szDir, $szFName, $szExt)
        $SrcFile = $Src & "\" & $szDrive & $szDir & $szFName & $szExt
        $DstFile = $Dst & "\" & $szDir & $szFName & $szExt

        $NumFiles = DirGetSize($Src,1)
        ; If File Exsists copy and rename file
        If FileExists($DstFile) Then
            $DstFile = call("_IfIdenticalIncrement", $SrcFile, $DstFile)
            If $DstFile <> "" Then
                For $ii = $NumFiles[1] To 100 Step 10
                    ProgressSet($ii, $ii & "%","Moving Files...")
                    ; Move Scans
                    
                    ; List Dir in srouce dir
                    $dirEx = _FileListToArray($Src,"*",2)
                    If UBound($dirEx) > 1 Then
                        ; Look in the dir list array
                        For $dir In $dirEx
                            ; Find and compaire dir in exclude array
                            For $exclude In $ExArray
                                If ($dir = $exclude) Then
                                    ; if exclude dir found go to top loop and skip
                                    ContinueLoop 2
                                EndIf
                            Next
                            FileMove($SrcFile, $DstFile, 8)
                        Next
                    endif
                    
                    Sleep(100)
                Next
            EndIf
            ; File doesnt exists so copy the file over
        Else
                For $ii = $NumFiles[1] To 100 Step 10
                    ProgressSet($ii, $ii & "%","Moving Files...")
                    ; Move Scans
                    
                    ; List Dir in srouce dir
                    $dirEx = _FileListToArray($Src,"*",2)
                    If UBound($dirEx) > 1 Then
                        ; Look in the dir list array
                        For $dir In $dirEx
                            ; Find and compaire dir in exclude array
                            For $exclude In $ExArray
                                If ($dir = $exclude) Then
                                    ; if exclude dir found go to top loop and skip
                                    ContinueLoop 2
                                EndIf
                            Next
                            FileMove($SrcFile, $DstFile, 8)
                        Next
                    endif   
                    Sleep(100)
                next
        EndIf
        
    ProgressSet(100, "Scans Moved...Successfully!", "Done!")
    sleep(2000)
    ProgressOff()
    Next
endfunc

Func _IfIdenticalIncrement($vSrcFile, $vDstFile)
    Local $Count = 0
; Get the modified date of the source file.
    $ScrVer = FileGetTime($vSrcFile, 0, 1)

; To get all elemnt of the distination file (in plan to add incremental number).
    call("_PathSplit",$vDstFile, $szDrive, $szDir, $szFName, $szExt)

; Loop to increment the name of the file.
    While FileExists($vDstFile)
        $Count += 1
        $vDstFile = $szDrive & $szDir & $szFName & "(" & $Count & ")" & $szExt
    WEnd

; If file(1), it assume that is the first copy.
    If $Count = 1 Then
        Return $vDstFile
; If file(x-1) is identical then assume the file is already duplicated.
    ElseIf $ScrVer = FileGetTime($szDrive & $szDir & $szFName & "(" & $Count - 1 & ")" & $szExt, 0, 1) Then
        Return ""
; Else assume it is a new version.
    Else
        Return $vDstFile
    EndIf
EndFunc  ;==>_IfIdenticalIncrement

 

 

This part is skipped and not sure why

 

For $exclude In $ExArray
                                If ($dir = $exclude) Then
                                    ; if exclude dir found go to top loop and skip
                                    ContinueLoop 2
                                EndIf
                            Next

INI file is this

 

[test]
EXCLUDE1=test1
EXCLUDE2=test2
EXCLUDE3=test3

 

Edited by digitalexpl0it

Share this post


Link to post
Share on other sites



doh, maybe thats why it skips it. now to figure out how to exclude sub directories per ini file

Share this post


Link to post
Share on other sites

that was it, I ended up tweaking the ini file to look like this

[test1]
[test2]
[test3]

 

which is fine since they will be directory names

then changed the following code to

 

; Read ini file
    Local $aArray = IniReadSectionNames($sFilePath)
    ; Start the array loop
    If Not @error Then
        for $i = 1 to $aArray[0]
            ; Add Ini values into array
            _ArrayAdd($ExArray, $aArray[$i])
        next
    endif

thanks for your help

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

  • Similar Content

    • RC86
      By RC86
      Afternoon!
      I have a requirement to return both a string and an array from a function so as a result I put them both into an array and returned that.  I can access them in their entirety after returning them but then I can't seem to access the array elements after this.  Should I be able to or is there a prettier way?
      #include <Array.au3> ;Memory info returned as a string and an array $memoryInfo = _getMemoryInfo() msgbox(0,"Memory Info",$memoryInfo[0]) _ArrayDisplay($memoryInfo[1],"Memory as an Array") Local $newArray[7] $memoryInfo[1] = $newArray msgbox(0,"Test element",$newArray[0]) _ArrayDisplay($newArray) Func _getMemoryInfo() Local $newArray[7] Local $array = MemGetStats() $newArray[0] = $array[0] ;% of memory in use $newArray[1] = Round($array[1]/1024 * 0.001,2) ;Total physical RAM $newArray[2] = Round($array[2]/1024 * 0.001,2) ;Availaible physical RAM $newArray[3] = Round($array[3]/1024 * 0.001,2) ;Total pagefile $newArray[4] = Round($array[4]/1024 * 0.001,2) ;Available pagefile $newArray[5] = Round($array[5]/1024 * 0.001,2) ;Total virtual $newArray[6] = Round($array[6]/1024 * 0.001,2) ;Available virtual $memoryUsage = $newArray[1] - $newarray[2] $pagefileUsage = $newArray[3] - $newarray[4] ;Output/Return Local $returnArray[2] $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB " $returnArray[1] = $newArray return $returnArray EndFunc A bit messy but hopefully it's understandable what I'm trying to achieve.
      Thanks!
    • usmiv4o
      By usmiv4o
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.2.4.3 Author: usmiv4o Script Function: AutoIt script to check if files in directory are changed. It is usefull for security contra-inteligense measures. Function Name: LoadTripwireDB() Description: Loads database (text file tripwire.txt) and compare files in /test folder for changes. compares Hash (MD5) checksums. If they are not the same starts Initial() Function Name: Initial() Description: Checks directory and makes index of files and their MD5 checksums in text file (tripwire.txt) Function Name: Hush() Description: Checks file and returns its MD5 checksum. Requirement(s): Windows XP Return Value(s): On Success - Returns true. Files are the same as before. On Failure - return false. Example: LoadTripwireDB() #ce ---------------------------------------------------------------------------- #include <Crypt.au3> #include <File.au3> #include <Array.au3> $sDir = @ScriptDir & "\Test" $sFilePath = @ScriptDir & "\tripwire.txt" Func Hush(ByRef $sFile) $sRead = FileOpen( $sFile) $dHash = _Crypt_HashData($sRead, $CALG_MD5) ; Create a hash of the text entered. ConsoleWrite("Hash of file " & $sFile & " is " & $dHash & @CRLF) EndFunc ;ConsoleWrite("Files in Dir are " & $aScriptDir[0] & @CRLF) ;$sFilePath = @ScriptDir & "\Examples.txt" ;_FileWriteFromArray($sFilePath, $aScriptDir, 1) ;_ArrayDisplay($aScriptDir, "1D display") Func Initial() $aScriptDir = _FileListToArray($sDir) for $i = 1 To UBound($aScriptDir) - 1 $dHash = _Crypt_HashData($i, $CALG_MD5) ;ConsoleWrite("File " & $aScriptDir[$i] & " is " & $dHash & @CRLF) ConsoleWrite($aScriptDir[$i] & ":" & $dHash & @CRLF) ;Hush($aScriptDir[$i]) ;FileWrite $hFileOpen = FileOpen($sFilePath, $FO_APPEND) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") EndIf FileWrite($hFileOpen, $aScriptDir[$i] & ":" & $dHash & @CRLF) Next EndFunc Func Monitor() $aScriptDir = _FileListToArray($sDir) for $i = 1 To UBound($aScriptDir) - 1 Next EndFunc Func LoadTripwireDB() $comparison_ok = false $dArray = _FileListToArray($sDir) ;directory $dArray0 = UBound($dArray) - 1 $fArray = FileReadToArray($sFilePath) ;file $fArray0 = UBound($fArray) ;_ArrayDisplay($dArray, "files array") if $dArray0 = $fArray0 Then ; are file same as recorded in txt file? ;ConsoleWrite("files in monitoring dir: " & $dArray[0] & " = file recorded: " & $fArray0 & @CRLF & $fArray[0]& @CRLF) for $i = 1 To UBound($dArray) - 1 ;ConsoleWrite("i = " & $i & @CRLF) $dHash = _Crypt_HashData($i, $CALG_MD5) ;binary ;$dHash = BinaryToString($dHash) $ffhash = StringSplit( $fArray[$i-1],":") $fhash = $ffhash[2] ;ConsoleWrite("IsBinary $dHash " & IsBinary($dHash) & @CRLF) if $dHash = $fhash Then ;if compared hashes are equal ;ConsoleWrite($fhash & ":" & $dHash & " equal" & @CRLF) ;ConsoleWrite("File: " & $fhash & @CRLF & "Directory: " & $dHash & @CRLF & "equal: yes " & @CRLF) Else ;if compared hashes are not equal ;ConsoleWrite("File: " & $fhash & @CRLF & "Directory: " & $dHash & @CRLF & "equal: not " & @CRLF) ;MsgBox(0,"hash md5",$fhash & ":" & $dHash & " not equal") EndIf Next ;ConsoleWrite("hashes are equal" & @CRLF) $comparison_ok = true Else ConsoleWrite("number of files in monitoring dir are not same as recorded" & @CRLF) ConsoleWrite("directory: " & $dArray[0] &":"& "files: " & UBound($fArray) - 1 & @CRLF) EndIf Return $comparison_ok EndFunc #main if LoadTripwireDB() = true Then ConsoleWrite(" hashes are equal " & @CRLF) ElseIf LoadTripwireDB() <> true Then ConsoleWrite(" hashes are not equal " & @CRLF) ConsoleWrite(" hashes are not equal " & @CRLF) Initial() EndIf  
      tripwire.au3
      tripwire.txt
    • Nareshm
      By Nareshm
      How to repeat my script when my control class does not match ?
       
      My Script : winActivate ( " PS :: Version - 2.2.0.0  - [PS Bill]")                   ControlClick ( "PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]")                   ControlSend(" PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]", "{enter}")                   WinActive ("Verification Number Entry ::")                   ControlClick("Next >", "","[NAME:btnNext]") ;if   >>>> Control <<<<  does not match repeat my script, ;if match exit script  

      Class:    WindowsForm734a
      Instance:    15
      ClassnameNN:    WindowsForm34a15
      Name:    BillPrint
      Advanced (Class):    [NAME:BillPrint]
      ID:    7875
      Text:    
      Position:    39, 310
      Size:    1221, 306
      ControlClick Coords:    745, 20
      Style:    0x56010000
      ExStyle:    0x00000000
      Handle:    0x00000000000C0468
    • SkysLastChance
      By SkysLastChance
      I am not sure why I am getting the this error on my second pass of the code.
      1 - $oWorkbook is not an object or not a workbook object
      Any help or advice on my code appreciated. 
      #include <Excel.au3> #include <Array.au3> #include <MsgBoxConstants.au3> Global $sExcelFile1 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsm)") Global $sExcelFile2 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "Excel Sheet (*.xlsx;*.xls)|All (*.*)") Global $vRow = 2 If FileExists($sExcelFile2) Then Global $oExcel2 = _Excel_Open () $oExcel2 = _Excel_BookOpen($oExcel2,$sExcelFile2) EndIF If FileExists($sExcelFile1) Then Global $oExcel1 = _Excel_Open () $oExcel1 = _Excel_BookOpen($oExcel1,$sExcelFile1,Default,Default,"2007") EndIF $oRead = _Excel_RangeRead ($oExcel2,"Untitled","A2",3) $oFind = _Excel_RangeFind ($oExcel1,$oRead,"E4:FD92",Default,$xlWhole) $Clip = _ArrayToClip($oFind,"",0,0,"",2,2) Send("{ScrollLock Off}") $hWnd = WinWait("[CLASS:XLMAIN]") ControlSend($hWnd, "", "", ("^g")) WinWait("[CLASS:bosa_sdm_XL9]") ; Go To ControlSend($hWnd, "", "", ("^v")) ControlSend($hWnd, "", "", ("{Enter}")) ControlSend($hWnD, "", "", "{Down " & $vRow & "}") Do $oTime = _Excel_RangeRead ($oExcel2,"Untitled","B2",3) If @error Then Exit MsgBox(0, "Error", "Error" & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox(0,"Test",$oTime) IF $oTime = "7:10:00 AM" Then $oCalls1 = _Excel_RangeRead ($oExcel2,Default,"C" & $vRow,3) $oCalls2 = _Excel_RangeRead ($oExcel2,Default,"D" & $vRow,3) ControlSend($hWnd, "", "", $oCalls1) ControlSend($hWnd, "", "", ("{RIGHT}")) ControlSend($hWnd, "", "", $oCalls2) $vRow = $vRow + 1 ContinueLoop Else $vRow = $vRow + 1 EndIf Until $vRow = 4 1.xlsm
      2.xlsx
    • nacerbaaziz
      By nacerbaaziz
      hellow guys
      I am a blind young man
      I started a project to make an audio player
      with recorder
      I ended the player and i did not know how to recorder
      This player is Compatible with the blind and visually impaired and ordinary users
      Unfortunately I do not see so I could not merge images and icons
      So I hope you will be able to help me in this matter
      I will bring you the open source project and please help me to add images, icons and recorder, the project is requires easy access in order to be compatible with the screen readers, Please take this into account.
      the software is organized so the recorder is In a special  menu that Contains stop and start and pause and save.
      The same is true for player
      Please help
      And greetings to all
      easy player.zip