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 Trees from a Source Server to Multiple Target Servers

Keywords:   distribute directory trees

The following sample code will distribute directory trees from a source server to multiple target servers, optionally deleting existing target directories before copy. Execution can be deferred for up to 12 hours.

Ideal for copying applications and data around servers during off-peak periods. Activity is logged to a logfile.

;**************************************************************************
;DISTRIB utility for distributing specified directory trees from a source
;server to a target server. An INI file is used to hold the source and
;target information, as detailed in the Sample Format below.
;Written by Edward Tippelt  ert@pobox.com
;Compiled and tested using Winbatch 98E
;Revision History
;Oct 1998
;1.0    Initial Release
;1.1    Added directory tree size checks to validate copies
;1.2    Added code to blat targets prior to copy if requested
;       Added better delay code around BoxText updates using TimeDiffSecs
;Nov 1998
;1.3    Fixed problem with target directory names containing spaces
;1.4    Replaced call to Deltree with a routine written in Winbatch
;Dec 1998
;1.5    Added feature to defer copy process in 1 hour increments

;SAMPLE FORMAT OF DISTRIB.INI

;[Parameters]

;SourceRoot=\\src_server\apps\released\apps
;Target1=\\targ_server1\apps\released\apps
;Target2=\\targ_server2\apps\released\apps
;Target3=\\targ_server3\apps\released\apps
;Target4=\\targ_server4\apps\released\apps
;Target5=\\targ_server5\apps\released\apps
;Target6=\\targ_server6\apps\released\apps
;Target7=\\targ_server7\apps\released\apps
;Target8=\\targ_server8\apps\released\apps
;Target9=\\targ_server9\apps\released\apps

;The DISTRIB.INI file must be in the same directory as DISTRIB.EXE,
;as that is where the program looks for it.
;DISTRIB.EXE 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
;An option allows you to delete existing target directories and create
;new ones from scratch to make sure source and target are identical.
;If the subdirectories do not exist, they will be created, including
;empty folders. Existing files will be overwritten, regardless of
;version. (All controlled by switches on the XCOPY command line)
;When each XCOPY32 process is complete, an entry specifying source
;and target folders will be written to DistribLog.txt, in the same
;folder as the executable and INI file, so it cannot be a read-only
;network share. 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
;DISTRIB.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)

;**************************************************************************
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      

;Wait until later dialogue

DistTimeFormat=`WWWDLGED,5.0`

DistTimeCaption=`When do you want to run the distribution`
DistTimeX=28
DistTimeY=41
DistTimeWidth=199
DistTimeHeight=202
DistTimeNumControls=14

DistTime01=`22,172,160,DEFAULT,PUSHBUTTON,DEFAULT,"Cancel",0`
DistTime02=`28,38,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 1 hours time",1`
DistTime03=`112,38,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 2 hours time",2`
DistTime04=`28,60,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 3 hours time",3`
DistTime05=`112,60,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 4 hours time",4`
DistTime06=`28,82,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 5 hours time",5`
DistTime07=`112,82,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 6 hours time",6`
DistTime08=`28,104,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 7 hours time",7`
DistTime09=`112,104,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 8 hours time",8`
DistTime10=`28,126,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 9 hours time",9`
DistTime11=`112,126,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 10 hours time",10`
DistTime12=`112,148,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 12 hours time",12`
DistTime13=`28,148,64,DEFAULT,PUSHBUTTON,DEFAULT,"Run in 11 hours time",11`
DistTime14=`22,14,160,DEFAULT,PUSHBUTTON,DEFAULT,"Run Now",13`

ButtonPushed=Dialog("DistTime")


Switch ButtonPushed

	Case 0
		Goto cancel
		Break
	Case 13
	   goto COPY
	   Break
	Case Buttonpushed
		break
EndSwitch

for waiting=1 to buttonpushed
timedelay(3599)
next

:COPY
;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")
        ;RunHideWait("command.com",' /C Deltree.exe /Y "%targ%"')
          ;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")
           FileDelete ("%thisdir%*.*")
          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)
    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:   W13775
Filename:   Distribute Directory Trees to Multiple Targets.txt
File Created: 2000:12:26:14:11:30
Last Updated: 2000:12:26:14:11:30