iceberg

File & Folder Copy with exclusions

8 posts in this topic

The below code is from this forum post

"https://www.autoitscript.com/forum/topic/91170-copy-specific-types-of-file/#comment-656890"

$sSource = 'C:\'
$sTarget = 'C:\Backup'
$aFileExtension = '*.doc|*.docx'
$aExcludeDir = 'Documents and Settings|Program Files|Recycler|System Volume Information|Temp|Windows'

; Check the inforamtion above is correct.
If StringRight($sSource, 1) = '\' Then $sSource = StringTrimRight($sSource, 1)
If StringRight($sTarget, 1) = '\' Then $sTarget = StringTrimRight($sTarget, 1)
If Not FileExists($sSource) Then
    MsgBox(0, 'Warning...', 'Source path ' & $sSource & ' does not exists.')
    Exit
EndIf
If Not FileExists($sTarget) Then DirCreate($sTarget)

; Create an array of file types and exclude directory.
$aExcludeDir = StringSplit($aExcludeDir, '|')
$aFileExtension = StringSplit($aFileExtension, '|')

; Create a folder list, compare the list and remove the exclude directory.
$aFolderList = _FileListToArrayEx($sSource, '*', 2, 0) 
If IsArray($aFolderList) Then
    For $x = 1 To $aFolderList[0]
        For $y = 1 To $aExcludeDir[0]
            If StringInStr($aFolderList[$x], $aExcludeDir[$y]) Then $aFolderList[$x] = ''
            If StringInStr($aFolderList[$x], $sTarget) Then $aFolderList[$x] = ''
        Next
    Next
EndIf

; Search file extension (*.doc, *.docx) and copy them into $sTarget folder.
If IsArray($aFileExtension) Then
    For $a = 1 To $aFileExtension[0]
        For $b = 1 To $aFolderList[0]
            If $aFolderList[$b] = '' Then ContinueLoop
            $aCopyList = _FileListToArrayEx($aFolderList[$b], $aFileExtension[$a], 1)
            If IsArray($aCopyList) Then
                For $x = 1 To $aCopyList[0]
                    $sDest = $sTarget & StringReplace($aCopyList[$x], $sSource, '')
                    $iSize = FileGetSize($aCopyList[$x]) - FileGetSize($sDest)
                    If $iSize <> 0 Or FileGetTime($aCopyList[$x], 0, 1) <> FileGetTime($sDest, 0, 1) Then FileCopy($aCopyList[$x], $sDest, 9)
                Next
            EndIf
        Next
        ; Search $sSource path for file types without recursive
        $aCopyList = _FileListToArrayEx($sSource, $aFileExtension[$a], 1, 0)
        If IsArray($aCopyList) Then
            For $x = 1 To $aCopyList[0]
                $sDest = $sTarget & StringReplace($aCopyList[$x], $sSource, '')
                $iSize = FileGetSize($aCopyList[$x]) - FileGetSize($sDest)
                If $iSize <> 0 Or FileGetTime($aCopyList[$x], 0, 1) <> FileGetTime($sDest, 0, 1) Then FileCopy($aCopyList[$x], $sDest, 9)
            Next
        EndIf
    Next
Else
    MsgBox(0, 'Warning...', 'File extension missing.')
    Exit
EndIf
MsgBox(0, 'Backup', 'Done with the backup.')

Func _FileListToArrayEx($sPath, $sFilter = '*', $iFlag = 0, $iRecursive = 1, $iRunFirstTime = 1)
    Local $aFileList = '', $aFolderList = '', $Tmp = ''
    Local $aBadChar[6] = ['\', '/', ':', '>', '<', '|']
    
    If StringRight($sPath, 1) = '\' Then $sPath = StringTrimRight($sPath, 1)
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    For $iCC = 0 To UBound($aBadChar) - 1
        If StringInStr($sFilter, $aBadChar[$iCC]) Then Return SetError(2, 2, "")
    Next
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")

    $sFilter = StringReplace($sFilter, '\', '')
    $iFirstFile = FileFindFirstFile($sPath & '\' & $sFilter)
    If @error Then Return
    
    While 1     
        $iNextFile = FileFindNextFile($iFirstFile)
        If @error Then ExitLoop
        $sFullPath = $sPath & '\' & $iNextFile
        If StringInStr(FileGetAttrib($sFullPath), "D") Then
            If $iFlag <> 1 Then $aFileList &= $sFullPath & @CRLF
            If $iRecursive Then 
                $Tmp = _FileListToArrayEx($sFullPath, $sFilter, $iFlag, $iRecursive = 1, 0)
                If $Tmp <> Chr(38) And $Tmp <> ChrW(38) Then $aFileList &= $Tmp
            EndIf
        Else
            If $iFlag <> 2 Then $aFileList &= $sFullPath & @CRLF
        EndIf
    WEnd    
    FileClose($iFirstFile)

    If $iRunFirstTime Then      
        If $sFilter <> '*' And $sFilter <> '*.*' And $iRecursive Then
            $aFolderList = _FileListToArrayEx($sPath, '*.*', 2, 1, 0)
            $aFolderList = StringSplit(StringTrimRight($aFolderList, 2), @CRLF, 1)          
            For $x = 1 To $aFolderList[0]
                $Tmp = _FileListToArrayEx($aFolderList[$x], $sFilter, $iFlag, $iRecursive, 0)
                If $Tmp <> Chr(38) And $Tmp <> ChrW(38) Then $aFileList &= $Tmp
            Next        
        EndIf
        $aFileList = StringSplit(StringTrimRight($aFileList, 2), @CRLF, 1)
        If $aFileList[$aFileList[0]] = '' Then Return SetError(4, 4, "")
    EndIf
    Return SetError(0, 0, $aFileList)
EndFunc

The question is how do I EXCLUDE file extensions instead of INCLUDE?

The script above demos on a INCLUDE basis.

Kindly assist.

Thank you.


mouse not found....scroll any mouse to continue.

Share this post


Link to post
Share on other sites



thanks jguinch.

I am exploring _FileListToArrayRec now.

But how do i make the array display each result individually, with the full path of the file?

kindly assist.


mouse not found....scroll any mouse to continue.

Share this post


Link to post
Share on other sites

for the full path, there is a parameter to set in _FileListToArrayRec : $iReturnPath (see helpfile)

_ArrayDisplay, as indicated by its name, displays an array. What do you expect ? Display the array line by line ? => You need a loop

Share this post


Link to post
Share on other sites

ok i hv got this now.

#include <File.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $sAutoItDir = StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", Default, -1))
    If StringRight($sAutoItDir, 5) = "beta\" Then
        $sAutoItDir = StringTrimRight($sAutoItDir, 5)
    EndIf
    ConsoleWrite($sAutoItDir & @CRLF)

    $aArray = _FileListToArrayRec($sAutoItDir, "*|*.exe", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH)

    For $i = $aArray[0] To 1 Step -1
        MsgBox($MB_SYSTEMMODAL, "", $aArray[$i])
    Next

EndFunc

It works fine though. But am I doing it right?

Thanks for your guidance.


mouse not found....scroll any mouse to continue.

Share this post


Link to post
Share on other sites

Yes thats right.

after going thru the help file examples, i think i shd be able to handle the filtering part.

Big Thanks!


mouse not found....scroll any mouse to continue.

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