jfitty Posted February 15, 2010 Share Posted February 15, 2010 Hey guys, I'm wanting some help with DirCopy. I'm using it in a backup script I'm putting together, but I want it to exclude a certain directory. Is this possible? Would someone have an example script I could use? Thanks Link to comment Share on other sites More sharing options...
whim Posted February 15, 2010 Share Posted February 15, 2010 DirMove() this directory to another location first, do your stuff and then move it back. Or, look into _FileListToArray (a forum search should turn up useful scripts) HTH, whim Link to comment Share on other sites More sharing options...
99ojo Posted February 15, 2010 Share Posted February 15, 2010 (edited) 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 February 15, 2010 by 99ojo Link to comment Share on other sites More sharing options...
jfitty Posted February 16, 2010 Author Share Posted February 16, 2010 That script is really good! What if the directory was a subfolder of a folder in the main directory (eg Source\Folder\FolderToExclude)? Link to comment Share on other sites More sharing options...
Beege Posted February 16, 2010 Share Posted February 16, 2010 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. Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
jfitty Posted February 16, 2010 Author Share Posted February 16, 2010 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.ive tested it out, but it doesnt seem like it does Link to comment Share on other sites More sharing options...
99ojo Posted February 16, 2010 Share Posted February 16, 2010 (edited) 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 February 16, 2010 by 99ojo Link to comment Share on other sites More sharing options...
jfitty Posted February 16, 2010 Author Share Posted February 16, 2010 Thanks mate! Where would I change the code if I wanted to go deeper into the folder structure? EG, 3 or 4 folders in? Link to comment Share on other sites More sharing options...
99ojo Posted February 16, 2010 Share Posted February 16, 2010 (edited) Hi, recode with a function from BugFix expandcollapse popup;#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 February 16, 2010 by 99ojo Link to comment Share on other sites More sharing options...
99ojo Posted February 16, 2010 Share Posted February 16, 2010 (edited) Correct some logical bug: expandcollapse popup;#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 Stefan Edited February 17, 2010 by 99ojo Link to comment Share on other sites More sharing options...
jfitty Posted February 18, 2010 Author Share Posted February 18, 2010 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 More sharing options...
99ojo Posted February 18, 2010 Share Posted February 18, 2010 Hi, that should work. Just change the line in the For..Loop: If FileExists (Stringreplace ($arfolders [$i], $source, $target)) And $arfolders [$i] <> StringTrimRight ($source, 1) Then FileCopy ($arfolders [$i] & "\*.*", Stringreplace ($arfolders [$i], $source, $target)) ;-)) Stefan Link to comment Share on other sites More sharing options...
jfitty Posted February 18, 2010 Author Share Posted February 18, 2010 hmm no luck with that unfortunately. I can get the files to list in the array, but they just wont copy Link to comment Share on other sites More sharing options...
99ojo Posted February 18, 2010 Share Posted February 18, 2010 (edited) Hi, this should get the goal: expandcollapse popup;#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 February 18, 2010 by 99ojo Link to comment Share on other sites More sharing options...
jfitty Posted February 18, 2010 Author Share Posted February 18, 2010 still nothing unfortunately. what part of the filecopy code should i be playing with? Link to comment Share on other sites More sharing options...
99ojo Posted February 18, 2010 Share Posted February 18, 2010 (edited) 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: expandcollapse popup;#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 February 18, 2010 by 99ojo Link to comment Share on other sites More sharing options...
jfitty Posted February 20, 2010 Author Share Posted February 20, 2010 It seems to treat these files as directories. As a test, I copied my xampp directory to a folder called "backup". This is what the console said: Dircopy : C:\xampp\apache_start.bat to backup\apache_start.bat Link to comment Share on other sites More sharing options...
99ojo Posted February 22, 2010 Share Posted February 22, 2010 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 More sharing options...
trademaid Posted April 7, 2010 Share Posted April 7, 2010 (edited) 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 April 7, 2010 by trademaid Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now