WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

File Version and Dir Mgmt

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Distribute Directory Tree Sample

Keywords: 	  distrib 

;**************************************************************************
;DISTRIB utility for distributing specified directory trees from a source
;server to a target server. Useful for copying application and data
;directories around the enterprise 
;An INI file is used to hold the source and target information, and a log
;file is written to record the successful (or otherwise) copy operations.
;This program uses the XCOPY32.EXE file to perform the actual
;copy, so this file must be somewhere on the path of your workstation.
;
;When the program is launched, the subdirectories directly below the 
;SourceRoot folder in the Distrib.INI file are presented in a 
;selection box.
;You can select one or more of the subdirectories shown there, and
;when you press OK, each of the selected subdirectory trees will be
;copied below each of the TARGETx folders listed in Distrib.INI
;If the subdirectories do not exist, they will be created, including
;empty folders. Existing files will be overwritten, regardless of
;version.  (Controlled by the switches on XCOPY32.EXE)
;When each XCOPY32 process is complete, an entry specifying source
;and target folders will be written to DistribLog.txt, so if the 
;program fails at any point, the log file will show how far it got
;successfully. The DistribLog.txt file is overwritten each time the
;Distrib.EXE program is run, to avoid building up a huge logfile.
;
;Source and target locations are referenced by UNC addresses in the
;example above, although conventional drive letter assignments will
;also work. However, it is assumed that the user account under which
;DISTRIBxx.EXE is being executed, has READ/WRITE access to all the
;specified target folders. Additional target directories can be
;added if required, the only important requirement is that the
;numbering of Target1, Target2, etc, is consecutive. (If a number
;is missed, any target directories with a higher number will be
;omitted)
;
;Written by Edward Tippelt  ert@pobox.com
;Released November 1998
;
;Example format of INI file to be placed in same directory as the .WBT or .EXE:
;
;[Parameters]
;
;SourceRoot=\\SourceServer\Apps\Dir\subdir\apps
;Target1=\\TargetServerA\Apps\Dir\subdir\apps
;Target2=\\TargetServerB\Apps\Dir\subdir\apps
;Target3=\\TargetServerC\Apps\Dir\subdir\apps
;Target4=\\TargetServerD\Apps\Dir\subdir\apps
;Target5=\\TargetServerE\Apps\Dir\subdir\apps

;**************************************************************************
Sounds(0)


;Check for INI file in current directory
DistribDir = Dirget()
Info=StrCat(Distribdir,"DISTRIB.INI")
if !FileExist(info) then goto ErrorCode1
 
:begin
;ask user if they want to blat the target directories prior to copy
blat=AskYesNo("Erase Target Directories First ??","Do you want to erase Target Directories prior to copy?")
;make absolutely sure they know what they are doing
if blat==@YES then blat=AskYesNo("ARE YOU ABSOLUTELY SURE ?","Please confirm that you want to erase Target Directories prior to copy")
;get source server root directory for copy
SourceRoot = IniReadPvt("Parameters","SourceRoot","Error",info)
If SourceRoot=="Error" then goto ErrorCode2
If StrSub(SourceRoot,StrLen(SourceRoot),1) != "\" Then SourceRoot = StrCat (SourceRoot,"\")
DirList=DirItemize("%SourceRoot%*.*")
:choose
Choices=AskItemList("Choose Directories to Copy",DirList, @TAB, @sorted, @multiple)
if Choices=="" then goto choose
DirNum=ItemCount(Choices, @TAB)
for i=1 to Dirnum
Dir%i%=ItemExtract(%i%,Choices,@TAB)
next
;get target servers
TSN=1 ;TargetServerNumber variable
While TSN>0
  T%TSN%=IniReadPvt("Parameters","Target%TSN%","Error",info)
  If T%TSN%=="Error" then break
  If StrSub(T%TSN%,StrLen(T%TSN%),1) != "\" Then T%TSN% = StrCat (T%TSN%,"\")
  TSN=TSN+1
EndWhile    
if TSN==1 then goto ErrorCode3
TSN=TSN-1 ;decrement by one to give number of target servers      

;At this point: 
;Dirnum holds the number of directory trees to copy from source to target
;Dir1 to Dir%i% hold the name of the root directory for each directory tree                                                                         
;TSN holds the number of target servers
;T1 to T hold the target directory paths

;Copy routine
BoxOpen("Copying from %SourceRoot%","")
Logfile=StrCat(DistribDir,"DistribLog.txt")
LogH=FileOpen(Logfile,"write")

for ii = 1 to TSN        ;for each of the servers
for jj = 1 to Dirnum     ;and each of the directories

src=StrCat(SourceRoot,Dir%jj%)
targ=StrCat(T%ii%,Dir%jj%)

;blat code section
if blat==@YES
   if DirExist(Targ) 
        Now=TimeYmdHms( )
        BoxText("Deleting:%@CRLF%%targ%%@CRLF%prior to copy")
          ;Delete directory tree
          ;enter with parameter targ containing the root of the directory tree
          ;to be deleted

          If StrSub(targ, StrLen(targ),1) != "\" Then targ1 = StrCat (targ, "\")
          DirChange (targ1)

          While @TRUE
           ThisDir = DirGet() ; current dir
           DirList = DirItemize ("*.*") ;look for any subdirectories

           If DirList != ""   ;this goes to a `last branch` of the tree
            DirChange (ItemExtract(1,DirList,@TAB))
            Continue
           Endif
          ; to get here, there must not be any 
          ; subdirectories under the present directory 
          FileList = FileItemize ("*.*")
          If FileList != "" ; there are files to delete
           FileAttrSet ("%thisdir%*.*", "rsh")  ;NOTE: must give full path as otherwise these
           FileDelete ("%thisdir%*.*")			   ;two commands will fail. (Bug in WB98E)
          Endif

          ;at this point the current directory is empty of 
          ;subdirectories and files and can be removed.

          DirChange ("..") ; back up one directory
          DirAttrSet (ThisDir, "rsh")
          DirRemove (ThisDir)

          ;now to exit if whole tree is deleted
          If StrLower(ThisDir) == StrLower(targ1) Then Break

          EndWhile

        Now1=TimeYmdHms( )
        if TimeDiffSecs(Now1, Now)< 2 then Delay(3)	;neat way of controlling display time
    endif
endif
msg=StrCat("Copying directory ",Dir%jj%," to:%@CRLF%",T%ii%)
BoxText(msg)
RunHideWait("XCOPY32.exe",'"%src%" "%targ%" /e /i /h /r /k /y')
Now=TimeYmdHms( )
msg=StrCat("Logging directory tree sizes of:%@CRLF%",src,"%@CRLF%and:%@CRLF%",targ)
BoxText(msg)
srcsize=DirSize(src,0)
targsize=DirSize(targ,0)
Now1=TimeYmdHms( )
if TimeDiffSecs(Now1, Now)< 2 then Delay(3)     
logmsg=StrCat("Copy completed of ",src,"   Size = ",srcsize)
FileWrite(LogH,logmsg)
logmsg=StrCat("               to ",targ,"   Size = ",targsize)
FileWrite(LogH,logmsg)
if targsize



Article ID:   W13774
Filename:   Distribute Directory Tree.txt
File Created: 2000:12:26:14:11:36
Last Updated: 2000:12:26:14:11:36