TreeCopy Utility - A Smarter XCOPY
Keywords: xcopy
Here's a handty utility that operates like XCOPY only with a bit more intelligence. Copies one subdirectory structure to another. optionally replaces newer files in the target with older files in the source, or leaves the newer ones untouched.To Download a zip file with all of E.R.'s examples
;TREECOPY.WBT ; ;***************************************************************************** ; TREECOPY UTILITY (TU) ; Written by E.Tippelt 100115.3301@compuserve.com ; Will do a winbatch xcopy function ; Uses the DSM.WBT code which handles single directory copies with additional ; code to extract the directory trees ;***************************************************************************** :TU1 ;Entry Parameters ;Filetype="*.*" ;DeleteTargetFiles=0 ;DowngradeTargetFiles=0 ;SourceRoot=askline("GET SOURCE DIRECTORY","Please specify source directory","") ;TargetRoot=askline("GET TARGET DIRECTORY","Please specify target directory","") if !DirExist(SourceRoot) Message("PARAMETER ERROR","Source Directory does not exist, Program Aborted") return endif TargetDrive=StrCat(StrSub(TargetRoot,1,2),"\") ;Get the target drive letter level=0 treelist="" treelist%level%=SourceRoot :TU5 if treelist%level%=="" goto TU10 ;Got the directory list, now do something with it! else gosub TU50 endif goto TU5 :TU10 ;The variable TREELIST now contains a space delimited, fully pathed, list of ;all the source subdirectories, using the source directory as the starting point. ;This bit of code will save the directory list to a file, as it will be too ;long to display in a message box in most cases ;handle = fileopen("c:\dirlist.txt","write") ;filewrite(handle,treelist) ;fileclose(handle) dr=itemcount(treelist," ") ;message("Number of Directories",dr) SourceDir=SourceRoot ;do the root directories first TargetDir=TargetRoot gosub dsm5 ;do the directory copy ;Now make sure that both SourceRoot and TargetRoot have a backslash on the end ;to cater for the situation where either source or target is something like c: If StrSub(SourceRoot,StrLen(SourceRoot),1)!= "\" Then SourceRoot=StrCat(SourceRoot,"\") If StrSub(TargetRoot,StrLen(TargetRoot),1)!= "\" Then TargetRoot=StrCat(TargetRoot,"\") for k = 2 to dr SourceDir=itemextract(k,treelist," ") TargetDir=StrReplace(SourceDir,SourceRoot,TargetRoot) gosub dsm5 ;do the directory copy next k Return ;Go back to calling program ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;SUBROUTINES ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;***************************************************************************** ;GET DIRECTORIES AT A GIVEN LEVEL IN THE TREE ;***************************************************************************** :TU50 treelist=StrCat(treelist," ",treelist%level%) treelist=strtrim(treelist) ;This will be the space delimited list of all the ;directories in the tree workinglevel=treelist%level% treeno=ItemCount(workinglevel," ") ;get the number of entries in the current tree level level=level+1 treelist%level%="" ;get ready to generate the next level of the tree for i=1 to treeno srcdir=itemextract(i,workinglevel," ") gosub TU60 if PathedDirList!="" then treelist%level%=strcat(treelist%level%," ",PathedDirList) next i treelist%level%=strtrim(treelist%level%) return ;***************************************************************************** ;GET SUBDIRECTORIES AT A GIVEN LEVEL IN THE TREE ;***************************************************************************** :TU60 ;Entry parameters: ;SrcDir ;level slash="" length=strlen(SrcDir) Last=strsub(SrcDir,length,1) if last!="\" then slash="\" DirChange(SrcDir) DirList=DirItemize("*.*") DirNo=ItemCount(Dirlist," ") ;Count the entries in DirList PathedDirList="" if DirNo==0 then return for j=1 to DirNo Dir=ItemExtract(j,DirList," ") ;Get each entry in the directory list Dir=StrCat(SrcDir,slash,Dir) PathedDirList=strcat(PathedDirList," ",Dir) next j PathedDirList=strtrim(PathedDirList) ;Fully pathed list of subdirectories return ;at a given level in the tree ;Becomes the treelist%level% for the next level ;**************************************************************************** ; Directory Synchronization Module (DSM) ; Written by E.Tippelt 100115.3301@compuserve.com ; Will synchronize the Source and Target directories ; Old files will be overwritten in the target ; Files not present in the source will be deleted in the target if required ;**************************************************************************** :DSM1 ;Entry parameters: ;SourceDir="C:\windows" ;TargetDir="C:\test" ;Filetype="*.*" ;DeleteTargetFiles=0 ;DowngradeTargetFiles=0 :DSM5 ;Start Here OrigDir=DirGet() ;Save current Directory :DSM10 if Filetype=="" then Filetype="*.*" SourceList=Fileitemize("%SourceDir%\%Filetype%") TargetList=Fileitemize("%TargetDir%\%Filetype%") SourceNo=ItemCount(Sourcelist," ") ;Count the entries in SourceList Dirchange(TargetDrive) if !DirExist("%TargetDir%") DirMake("%TargetDir%") goto DSM50 endif TargetNo=ItemCount(TargetList," ") ;Count the entries in TargetList if TargetNo==0 then goto DSM50 DirChange(TargetDir) ErrorMode(@OFF) FileAttrSet("*.*","rAsh") ;Clear RSH/ Set A target attributes for copy ;in case there are existing files with set attribs ErrorMode(@CANCEL) for i=1 to SourceNo Item=ItemExtract(i,SourceList," ") ;Get each entry in the source list Pos=ItemLocate(Item,TargetList," ") ;See if present in target list if pos !=0 TargetList=ItemRemove(pos,TargetList," ") ;if present, delete the item TargetNo=TargetNo-1 If TargetNo==0 then break endif next i :DSM15 ;At this point, Targetlist contains a list of all the files not ;present on the source if DeleteTargetFiles != 1 then goto DSM20 ;Skip delete process if delete flag not set ;OK, Lets delete some files if FileDelete(TargetList)==@True then goto DSM20 ;Done it, so carry on Message("Target Directory Delete Error","Error deleting files in %TargetDir%") :DSM20 ;This bit copies the Source files to the Target if not the same DirChange(SourceDir) for j=1 to SourceNo Item=ItemExtract(j,SourceList," ");Get each entry in the source list FC=FileCompare(Item,"%TargetDir%\%Item%") Switch FC Case 0 ;Files are the same, so skip to next break Case 1 ;Source file is same size, but newer, so copy it FileCopy("%Item%","%TargetDir%\%Item%",@false) break Case 2 ;Source file is newer, so copy it FileCopy("%Item%","%TargetDir%\%Item%",@false) break Case 3 ;Target file missing, so copy it FileCopy("%Item%","%TargetDir%\%Item%",@false) break Case FC ;Target must be newer, so go on to next file if DowngradeTargetFiles==0 then break ;unless downgrade requested FileCopy("%Item%","%TargetDir%\%Item%",@false) ;Downgrade requested, so copy anyway EndSwitch :DSM22 next j goto DSMEND :DSM50 ;This bit copies the Source files to a newly created (empty) Target DirChange(SourceDir) Boxtext("Copying %SourceDir%") if FileCopy(SourceList,"%TargetDir%",@false)==@true then goto DSM55 Message("File Copy Error","Error Copying Source to Target") :DSM55 goto DSMEND :DSMEND DirChange(OrigDir) ;Restore current Directory RETURN ;**************************************************************************** ; End of Directory Synchronization Module (DSM) ;**************************************************************************** ;**************************************************************************** ; End of Treecopy Utility (TU) ;****************************************************************************
Article ID: W13806Filename: TreeCopy Utility - a Smarter XCOPY.txt