Sign in to follow this  
Followers 0
DoubleMcLovin

[SOLVED] More dynamic array, or better method so I don't loose variables

4 posts in this topic

#1 ·  Posted (edited)

First, pardon me for the length of code I am asking people to cipher through here, and I apologize if this looks terrible I am still learning.

With this script, I am currently just trying to look at a directory, and analyze the file structure of it and print it to console in a format as follows

dir

-subdir

--subdir file

--subdir file

-file

dir

...

But the problem I am having is getting my arrays to behave a little more dynamically so that I can build a directory-tree without imposing a hard-coded limit to how deep the script can search.

Here is my entire script

;##########################################################################
;#Script:    FileCustodian
;#Author:    DoubleMcLovin
;#Date:        10-8-10
;#Purpose:    Monitors files and maintains naming schemes and integrity
;#Version:    1
;#
;##########################################################################

;~ NOTES:
;~ Delete is non functional
;~ Warning/Error tree is as follows:
;~     1k branch-Directory existance anf file status
;Options
Opt('MustDeclareVars', 1)
;Variable Declaration
Local $oSleep = 5     ;Declaration in minutes of interval between scans
Local $rmDIR = 1    ;removes empty directories, 1 for yes, 0 for no
Local $lDir = @ScriptDir & "/logs/"    ;log directory
Local $dTime = @MON & "/" & @MDAY & "/" & @YEAR & " " & @HOUR & ":" & @MIN & " "
;placeholders
Global $tCount, $tCounts, $dLevel, $dList, $tSleep,$aLevels, $dLevels, $lStep, $a
Global $sFile[1] = [0]
Global $sFiles[1] = [0]
Global $dList[1] = [0]
Local $ignore
If Not FileExists ($lDir) Then DirCreate ($lDir)
;Directories watched, seperated by a |
Local $oDir = "c:\system66\media\movies\|c:\system66\media\test\"
Local $aDir = StringSplit ($oDir,"|")
;Strings to be removed, seperated by a |
Local $oRemove = " |,|-|_|..|.-.|.-|-."
Local $aRemove = StringSplit ($oRemove,"|")
;Files to be ignored, sepereated by a |
Local $iFile = "list.db|Thumbs.db"
Local $iFiles = StringSplit ($iFile,"|")
Local $dLog = FileOpen ($lDir & "debug.log",43)
Local $cLog = FileOpen ($lDir & "change.log",43)
Local $fLog = FileOpen ($lDir & "list.db",74)

While 1
    For $a = 1 to $aDir[0]    ;cycles through watched dirs
        $tCount = 1    ;counts Directory structure
        ;Checks to see if Directory exists
        If Not FileExists ($aDir[$a]) Then
            ToolTip ("Warning: A folder in the 'Watch' list is non-existant. Please correct.")
            FileWriteLine ($dLog,$dTime & "Warning-1003: Directory does not exist. (" & $aDir[$a] & ")")
            $a += 1    ;force next dir in watch list
        EndIf
        ;Checks to see if the Directory is empty, if it isn't this will record all the files into the list.db file
        $dLevel = $aDir[$a]
        $dLevels = $dLevels & $dLevel & "|"
        $aLevels = StringSplit ($dLevels,"|")
        $lStep = 1
        FileWrite ($fLog,$aDir[$a] & @LF)
        ConsoleWrite ($aDir[$a] & @LF)
        dirCheck()
        $dLevels = ""
        ConsoleWrite ("The big next!")
    Next
    $tSleep = $oSleep * 60 * 10000
    ExitLoop
    Sleep ($tSleep)
WEnd

;function fields
;This function guides into subdirs and modifies $tCount
Func subDir()
    $aLevels = StringSplit ($dLevels,"|")
    For $i = 1 to $aLevels[0] - 1
        ConsoleWrite ($i & "--" & $aLevels[$i] & @LF)
    Next
    $dLevel = $dLevel & $sFiles[$lStep] & "\"
    ConsoleWrite ($tCounts & $sFiles[$lStep] & " line:59" & @LF)
    $tCount += 1
    If $lStep < $aLevels[0] Then $lStep += 1
    ;increase maximum array sizes
    ReDim $sFile[$lStep]
    ReDim $sFiles[$lStep]
    ReDim $dList[$lStep]
    dirCheck()
    $tCount -= 1
EndFunc

Func tCounter()
    ;increase maximum array sizes
    ReDim $sFile[$lStep]
    ReDim $sFiles[$lStep]
    ReDim $dList[$lStep]
    $tCounts = ""    ;reset
    For $i = 1 to $tCount
        $tCounts = $tCounts & "-"
    Next
EndFunc    

Func rmdCheck()
    ;increase maximum array sizes
    ReDim $sFile[$lStep + 1]
    ReDim $sFiles[$lStep + 1]
    ReDim $dList[$lStep + 1]
    MsgBox (0, "","lStep=" & $lStep & @LF & $aLevels[$lStep])
    FileChangeDir ($aLevels[$lStep])
    $sFile[$lStep] = FileFindFirstFile ("*")
    If $sFile[$lStep] = -1 Or @error Then    ;If the directory is empty
        If $rmDIR = 0 Then        ;delete mode is off
            FileWriteLine ($dLog,$dTime & "WARNING-1002: Directory is empty and RMDIR set to 0. (" & $aDir[$a] & ")")
        ElseIf $rmDIR = 1 Then    ;delete mode is on
            $dRmv = DirRemove ($aDir[$a])
            If $dRmv = 0 Then        ;delete failed
                FileWriteLine ($dLog,$dTime & "ERROR-1002: Error deleting directory. (" & $aDir[$a] & ")")
            ElseIf $dRmv = 1 Then    ;delete succeeded
                FileWriteLine ($cLog,$dTime & "Deleted Directory: " & $aDir[$a])
            Else                    ;unknown result of delete
                FileWriteLine ($dLog,$dTime & "ERROR-1003: Error in process routine for directory deleting. (" & $aDir[$a] & ")")
            EndIf
        EndIf
        ;If $a is not increased, program will loop forever
        $a += 1
        $sFile[$lStep] = FileFindFirstFile ("*")
    Else    ;If the directory is not empty
        tCounter()
    EndIf    
EndFunc

Func dirCheck()
    tCounter()
    rmdCheck()
        ;Records files into the list.db
        For $c = 1 to 1$sFile[$lStep]
            $sFiles[$lStep] = FileFindNextFile ($sFile[$lStep])
            If $sFiles[$lStep] = -1 Or @error = 1 Then ExitLoop    ;Prevents uncontrolled looping
            For $b = 1 to $iFiles[0]
                If $sFiles[$lStep] = $iFiles[$b] Then
                    FileWriteLine ($fLog,$tCounts & "(ignored)" & $sFiles[$lStep])
                    ConsoleWrite ($tCounts & "(ignored)" & $sFiles[$lStep] & "  Line:99" & @LF)
                    FileWriteLine ($dLog,$dTime & "WARNING-1001: File listed in ignore roster: " & $sFiles[$lStep])
                    $ignore = 1
                    ExitLoop
                Else
                    $ignore = 0
                EndIf
            Next
            If $ignore = 0 Then
                ;If its a directory, check subdirs
                $dList[$lStep] = DirGetSize ($sFiles[$lStep],1)
                If IsArray ($dList[$lStep]) Then    ;if $sFiles is a directory
                    For $d = 1 to $dList[$lStep][1]
    ;~                     ConsoleWrite ("$d is currenty:" & $d & " tCount is currently:" & $tcount _
    ;~                     & " sFile is currently:" & $sFile & " sFiles is currently:" & $sFiles & @LF)
                        $dLevels = $dLevels & $dLevel & "|"
                        $dLevels = StringRegExpReplace ($dLevels,$aDir[$a] & "|" & $aDir[$a],$aDir[$a])
                        ConsoleWrite ($dLevels & @LF)
                        subDir()
                        tCounter()
                    Next
                Else    ;if $sFiles is a file and not a directory
                    FileWriteLine ($fLog,$tCounts & $sFiles[$lStep])
                    ConsoleWrite ($tCounts & $sFiles[$lStep] & "  line:142" & @LF)
                EndIf
            EndIf
        Next
;~         $tCount -= 1
        tCounter()
EndFunc
;Close all the log files
FileClose ($fLog)
FileClose ($dLog)
FileClose ($cLog)
Edited by DoubleMcLovin

Share this post


Link to post
Share on other sites



use the _Array UDF

Share this post


Link to post
Share on other sites

This is a common coding problem called a "recursive search". Search the forum on that term, and you will find many examples.

:mellow;


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

This is a common coding problem called a "recursive search". Search the forum on that term, and you will find many examples.

:mellow;

Thank you! Glad to see I am not just approaching this completely backwards and that this happens often =] Thread marked as solved now that I have something to research.

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