Jump to content

Exclude in DirCopy


jfitty
 Share

Recommended Posts

Hi,

there are some important informations missing.

1) If you use DirCopy to backup one directory including subdirectories and files, like DirCopy ("c:\root"...) -> no chance.

2) If you use some other combinations, may be.

This might help you:

#include <file.au3>

Global $source = "c:\source\"
Global $target = "c:\backup\"
Global $exclude = "excludedir" ; only foldername
Global $arfolders, $arfiles
Global $berror = False
;backup folders of source
$arfolders = _FileListToArray (StringTrimRight ($source, 1), "*.*", 2)
If @error Then 
    MsgBox (0,"Folders", "No folders found", 5)
Else
    For $i = 1 To UBound ($arfolders) - 1
        If StringInStr ($arfolders [$i], $exclude) Then
            ContinueLoop
        Else
            Dircopy ($source & $arfolders [$i], $target & $arfolders [$i])
            If @error Then $berror = True
        EndIf
    Next
EndIf

If $berror Then MsgBox (0,"Error", "Attention! Error copying folders!")
;you may not need the files
$arfiles = _FileListToArray (StringTrimRight ($source, 1), "*.*", 1)
If @error Then 
    MsgBox (0,"Files", "No fildes found", 5)
Else
    For $i = 1 To UBound ($arfiles) - 1
        FileCopy ($source & $arfiles [$i], $target & $arfiles [$i], 8)
    Next
EndIf

;-))

Stefan

P.S: Haha, haven't seen Whim's suggestions........

Edited by 99ojo
Link to comment
Share on other sites

That script is really good! What if the directory was a subfolder of a folder in the main directory (eg Source\Folder\FolderToExclude)?

His script should still work. Hes used func StringInStr() to exclude the folder so if anywhere in the address match's your exclude folder name, it will be skipped.
Link to comment
Share on other sites

ive tested it out, but it doesnt seem like it does

Hi,

no it doesn't because I only read out the directories on the 1 st level of your source directory.

;backup folders of source

$arfolders = _FileListToArray (StringTrimRight ($source, 1), "*.*", 2)

If your exclude folder is on deeper level, you have to recode it, example for 2 nd level:

#include <file.au3>

Global $source = "c:\source\"
Global $target = "c:\backup\"
Global $exclude = "excludedir" ; only foldername
Global $arfolders, $arfiles, $arsndlevel
Global $berror = False
;backup folders of source
$arfolders = _FileListToArray (StringTrimRight ($source, 1), "*.*", 2)
If @error Then 
    MsgBox (0,"Folders", "No folders found", 5)
Else
    For $i = 1 To UBound ($arfolders) - 1
        $arsndlevel = _FileListToArray ($source & $arfolders [$i],  "*.*", 2)
        For $j = 1 To UBound ($arsndlevel) - 1
            If StringInStr ($arsndlevel [$j], $exclude) Then 
                ContinueLoop
            Else
                Dircopy ($source & $arfolders [$i] & "\" & $arsndlevel [$j], $target & $arfolders [$i] & "\" & $arsndlevel [$j])
                If @error Then $berror = True
            EndIf
        Next
    Next
EndIf

If $berror Then MsgBox (0,"Error", "Attention! Error copying folders!")
;you may not need the files, !! this is only to copy files on the 1 st folder level !!
$arfiles = _FileListToArray (StringTrimRight ($source, 1), "*.*", 1)
If @error Then 
    MsgBox (0,"Files", "No fildes found", 5)
Else
    For $i = 1 To UBound ($arfiles) - 1
        FileCopy ($source & $arfiles [$i], $target & $arfiles [$i], 8)
    Next
EndIf

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Hi,

recode with a function from BugFix

;#include <file.au3>

Global $source = "c:\source\"
Global $target = "c:\backup\"
Global $exclude = "excludedir" ; only foldername
Global $arfolders
Global $berror = False
;backup folders of source
$arfolders = _GetFilesFolder_Rekursiv (StringTrimRight ($source, 1), "*", 1)
If @error Then 
    MsgBox (0,"Folders", "No folders found", 5)
Else
    For $i = 1 To UBound ($arfolders) - 1
        If StringInStr ($arfolders [$i], $exclude) Then 
            ContinueLoop
        Else
            Dircopy ($arfolders [$i] , StringReplace  ($arfolders [$i], $source, $target))
            If @error Then $berror = True
        EndIf
    Next
EndIf

If $berror Then MsgBox (0,"Error", "Attention! Error copying folders!")

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     recursive listing of files and/or folders
; Parameter(s):    $sPath     Basicpath of listing ('.' -current path, '..' -parent path)
;                  $sExt      Extension for file selection '*' or -1 for all (Default)
;                  $iDir      -1 Files+Folder(Default), 0 only Files, 1 only Folder
;      optional:   $iRetType  0 for Array, 1 for String as Return
;      optional:   $sDelim    Delimiter for string return
;                             0 -@CRLF (Default)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                  Array[0] includes count of found files/folder
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

:(

Correct some logical bug:

;#include <file.au3>
;#include <array.au3> ; to see array returned by _GetFiles... Func
Global $source = "c:\source\"
Global $target = "c:\backup\"
Global $exclude = "excludedir" ; only foldername
Global $arfolders
Global $berror = False
;backup folders of source
$arfolders = _GetFilesFolder_Rekursiv (StringTrimRight ($source, 1), "*", 1)
;_ArrayDisplay ($arfolders) ; don't forget include line if you want to see array
If @error Then 
    MsgBox (0,"Folders", "No folders found", 5)
Else
    For $i = UBound ($arfolders) - 1 To 1 Step - 1
        If FileExists (Stringreplace  ($arfolders [$i], $source, $target)) Then 
            FileCopy ($arfolders [$i] & "\*.*", Stringreplace  ($arfolders [$i], $source, $target))
        Else
            If StringInStr ($arfolders [$i], $exclude) Then ContinueLoop
            Dircopy ($arfolders [$i] , Stringreplace  ($arfolders [$i], $source, $target))
            If @error Then $berror = True
        EndIf
     Next
EndIf
If $berror Then MsgBox (0,"Error", "Attention! Error copying folders!")

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     recursive listing of files and/or folders
; Parameter(s):    $sPath     Basicpath of listing ('.' -current path, '..' -parent path)
;                  $sExt      Extension for file selection '*' or -1 for all (Default)
;                  $iDir      -1 Files+Folder(Default), 0 only Files, 1 only Folder
;      optional:   $iRetType  0 for Array, 1 for String as Return
;      optional:   $sDelim    Delimiter for string return
;                             0 -@CRLF (Default)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                  Array[0] includes count of found files/folder
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc

:mellow:

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Thats perfect mate! Thanks for that. One more thing, is there a way I can make it copy the files that are in the root directory? Eg, I want to copy the folder C:\Temp, but files that are in the Temp directory dont get copied, just the folders. I've had a play with it but cant seem to get it to work

Link to comment
Share on other sites

Hi,

this should get the goal:

;#include <file.au3>
;#include <array.au3> ; to see array returned by _GetFiles... Func
Global $source = "c:\source\"
Global $target = "c:\backup\"
Global $exclude = "excludedir" ; only foldername
Global $arfolders
Global $berror = False
;backup folders of source
$arfolders = _GetFilesFolder_Rekursiv (StringTrimRight ($source, 1), "*", 1)
;_ArrayDisplay ($arfolders) ; don't forget include line if you want to see array
If @error Then 
    MsgBox (0,"Folders", "No folders found", 5)
Else
    For $i = UBound ($arfolders) - 1 To 1 Step - 1
        If FileExists (Stringreplace  ($arfolders [$i], $source, $target)) And $arfolders [$i] <> StringTrimRight ($source, 1) Then 
            FileCopy ($arfolders [$i] & "\*.*", Stringreplace  ($arfolders [$i], $source, $target))
        Else
            If StringInStr ($arfolders [$i], $exclude) Then ContinueLoop
            Dircopy ($arfolders [$i] , Stringreplace  ($arfolders [$i], $source, $target))
            If @error Then $berror = True
        EndIf
     Next
EndIf
If $berror Then MsgBox (0,"Error", "Attention! Error copying folders!")

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     recursive listing of files and/or folders
; Parameter(s):    $sPath     Basicpath of listing ('.' -current path, '..' -parent path)
;                  $sExt      Extension for file selection '*' or -1 for all (Default)
;                  $iDir      -1 Files+Folder(Default), 0 only Files, 1 only Folder
;      optional:   $iRetType  0 for Array, 1 for String as Return
;      optional:   $sDelim    Delimiter for string return
;                             0 -@CRLF (Default)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                  Array[0] includes count of found files/folder
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc
Edited by 99ojo
Link to comment
Share on other sites

Hi,

This is working for me. What is exactly your problem.

1) My program reads out all subfolders beneath $source

2) DirCopy if folder is not existing on target and folder is not equal $exclude

FileCopy if folder exists on target AND folder is not equal $source

This might helps you for debugging:

;#include <file.au3>
;#include <array.au3> ; to see array returned by _GetFiles... Func
Global $source = "c:\source\"
Global $target = "c:\backup\"
Global $exclude = "excludefolder" ; only foldername
Global $arfolders
Global $berror = False
;backup folders of source
$arfolders = _GetFilesFolder_Rekursiv (StringTrimRight ($source, 1), "*", 1)
;_ArrayDisplay ($arfolders) ; don't forget include line if you want to see array
If @error Then 
    MsgBox (0,"Folders", "No folders found", 5)
Else
    For $i = UBound ($arfolders) - 1 To 1 Step - 1
        If FileExists (Stringreplace  ($arfolders [$i], $source, $target)) And $arfolders [$i] <> StringTrimRight ($source, 1) Then 
            ConsoleWrite ("Filecopy : " & $arfolders [$i] & " to " & Stringreplace  ($arfolders [$i], $source, $target) & @CRLF)
            FileCopy ($arfolders [$i] & "\*.*", Stringreplace  ($arfolders [$i], $source, $target))
        Else
            If StringInStr ($arfolders [$i], $exclude) Then ContinueLoop
            ConsoleWrite ("Dircopy : " & $arfolders [$i] & " to " & Stringreplace  ($arfolders [$i], $source, $target) & @CRLF)
            Dircopy ($arfolders [$i] , Stringreplace  ($arfolders [$i], $source, $target))
            If @error Then $berror = True
        EndIf
     Next
EndIf
If $berror Then MsgBox (0,"Error", "Attention! Error copying folders!")

;==================================================================================================
; Function Name:   _GetFilesFolder_Rekursiv($sPath [, $sExt='*' [, $iDir=-1 [, $iRetType=0 ,[$sDelim='0']]]])
; Description:     recursive listing of files and/or folders
; Parameter(s):    $sPath     Basicpath of listing ('.' -current path, '..' -parent path)
;                  $sExt      Extension for file selection '*' or -1 for all (Default)
;                  $iDir      -1 Files+Folder(Default), 0 only Files, 1 only Folder
;      optional:   $iRetType  0 for Array, 1 for String as Return
;      optional:   $sDelim    Delimiter for string return
;                             0 -@CRLF (Default)  1 -@CR  2 -@LF  3 -';'  4 -'|'
; Return Value(s): Array (Default) or string with found pathes of files and/or folder
;                  Array[0] includes count of found files/folder
; Author(s):       BugFix (bugfix@autoit.de)
;==================================================================================================
Func _GetFilesFolder_Rekursiv($sPath, $sExt='*', $iDir=-1, $iRetType=0, $sDelim='0')
    Global $oFSO = ObjCreate('Scripting.FileSystemObject')
    Global $strFiles = ''
    Switch $sDelim
        Case '1'
            $sDelim = @CR
        Case '2'
            $sDelim = @LF
        Case '3'
            $sDelim = ';'
        Case '4'
            $sDelim = '|'
        Case Else
            $sDelim = @CRLF
    EndSwitch
    If ($iRetType < 0) Or ($iRetType > 1) Then $iRetType = 0
    If $sExt = -1 Then $sExt = '*'
    If ($iDir < -1) Or ($iDir > 1) Then $iDir = -1
    _ShowSubFolders($oFSO.GetFolder($sPath),$sExt,$iDir,$sDelim)
    If $iRetType = 0 Then
        Local $aOut
        $aOut = StringSplit(StringTrimRight($strFiles, StringLen($sDelim)), $sDelim, 1)
        If $aOut[1] = '' Then
            ReDim $aOut[1]
            $aOut[0] = 0
        EndIf
        Return $aOut
    Else
        Return StringTrimRight($strFiles, StringLen($sDelim))
    EndIf
EndFunc

Func _ShowSubFolders($Folder, $Ext='*', $Dir=-1, $Delim=@CRLF)
    If Not IsDeclared("strFiles") Then Global $strFiles = ''
    If ($Dir = -1) Or ($Dir = 0) Then
        For $file In $Folder.Files
            If $Ext <> '*' Then
                If StringRight($file.Name, StringLen($Ext)) = $Ext Then _
                    $strFiles &= $file.Path & $Delim
            Else
                $strFiles &= $file.Path & $Delim
            EndIf
        Next
    EndIf
    For $Subfolder In $Folder.SubFolders
        If ($Dir = -1) Or ($Dir = 1) Then $strFiles &= $Subfolder.Path & '\' & $Delim
        _ShowSubFolders($Subfolder, $Ext, $Dir, $Delim)
    Next
EndFunc

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

Hi,

it is obvious that you are not using my code. In my coding, the array $arfolders only has directory names as values:

$arfolders = _GetFilesFolder_Rekursiv (StringTrimRight ($source, 1), "*", 1)

For further asisstence you have to post your code.

;-))

Stefan

Link to comment
Share on other sites

  • 1 month later...

1) How to we do a dircopy, but only copy the newest file (if it exists on destination)

2) how do I do a run with spaces in the parameters?

It would fine for $source="c:\temp_folder" but not $source="c:\temp folder"

$source="c:\temp folder"

$dest="d:\temp folder"

run ("xcopy " & $source & " " & $dest, @WindowsDir, @SW_MAXIMIZE)

Edited by trademaid
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...