Jump to content
Sign in to follow this  
Angrysteel

File Copy Script

Recommended Posts

Angrysteel

Hi everyone,

Long time listener, first time poster.

I am writing a script to copy files(*.doc, *.xls, *,pdf, etc.)to C:\Backup.

Do I have to write a separate func for each file extension or is there an easier way to do do it.

Share this post


Link to post
Share on other sites
omikron48

You just need to make a function that does the searching based on a search string, then do a FileCopy for every item found.

The search string can be "*.doc" or "*.xls", etc.

Here's some file searching code. Just modify it to do what you want.

Global $RESULT_MAX = 0xFFF  ;Set to your preferred maximum number of results returned
Global $STACK_MAX = 0xFFF   ;Set to your preferred maximum number of elements in the stack
Global $STACK[$STACK_MAX]
$STACK[0] = 0

Func _Push($var)
    If $STACK[0] < $STACK_MAX - 1 Then
        $STACK[0] += 1
        $STACK[$STACK[0]] = $var
    Else
        Return 1
    EndIf
    Return 0
EndFunc

Func _Pop()
    If $STACK[0] > 0 Then
        $STACK[0] -= 1
        Return $STACK[$STACK[0] + 1]
    Else
        SetError(1)
    EndIf
    Return 0
EndFunc

;$path = the root path where the search is started
;$filter = the filter used by FileFindFirstFile and FileFindNextFile
;$exclude = the file attribute(s) to be excluded from the search
;$depth = the maximum recursion depth (# of subfolder levels from the root) for the search
;   -1 for search all subfolders
;   0 for search only root folder
;   n to search upto n subfolders deep
;Returns an array with [0] containing the number of returned results and [1] being the first result
;The returned values is the path of the matched file(s) starting from the root path of the search
;Sets @error to 1 if root path is invalid
Func _Search($path, $filter, $exclude, $depth)
    Local $current = @WorkingDir
    If FileChangeDir($path) == 1 Then
        If StringCompare(StringRight($path, 1), "\") <> 0 Then
            $path &= "\"
        EndIf
        Local $array[2] = [$path, 0]
        _Push($array)
        Local $result = _SearchUtil($filter, $exclude, $depth)
        FileChangeDir($current)
        Return $result
    Else
        SetError(1)
    EndIf
    FileChangeDir($current)
    Local $empty[2] = [0, 0]
    Return $empty
EndFunc

Func _SearchUtil($filter, $exclude, $depth)
    Local $result[$RESULT_MAX]
    Local $search, $fname, $attrib
    Local $array[2], $temp[2]
    $result[0] = 0
    While 1
        ;get top of stack
        $array = _Pop()
        If @error == 1 Then
            ExitLoop
        EndIf
        ;change directory
        FileChangeDir($array[0])
        ;search for matches in current directory
        $search = FileFindFirstFile($filter)
        While 1
            $fname = FileFindNextFile($search)
            If @error == 1 Then
                ExitLoop
            EndIf
            $attrib = FileGetAttrib($fname)
            If _IsIncluded($attrib, $exclude) Then
                ;File positively matched
                $result[0] += 1
                $result[$result[0]] = $array[0] & $fname
            EndIf
        WEnd
        ;search subdirectory
        If $depth == -1 Or $array[1] < $depth Then
            $search = FileFindFirstFile("*")
            While 1
                $fname = FileFindNextFile($search)
                If @error == 1 Then
                    ExitLoop
                EndIf
                $attrib = FileGetAttrib($fname)
                If StringInStr($attrib, "D") <> 0 Then
                    If _IsIncluded($attrib, $exclude) Then
                        ;Push new subfolder search into the stack
                        $temp[0] = $array[0]
                        $temp[1] = $array[1] + 1
                        _Push($temp)
                    EndIf
                EndIf
            WEnd
        EndIf
    WEnd
    Return $result
EndFunc

Func _IsIncluded($attrib, $exclude)
    For $i = 1 To StringLen($exclude)
        If StringInStr($attrib, StringMid($exclude, $i, 1)) <> 0 Then
            Return False
        EndIf
    Next
    Return True
EndFunc
Edited by omikron48

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.