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

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

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

Backup Folders With All Permissions


I had a need to copy an NTFS folder structure, on a regular basis, to an external usb drive as a backup. The backup was of our ftp site and I needed to make sure the target folders had the same permission as the source. I had many subfolder with special and individual NTFS security permissions as well as folders set to not inherit permissions from parent. When you copy all of these special permissions are lost. So I wrote the below script to copy and preserve all permissions. The script also gives you a progress bar showing what file is being copied.
AddExtender("wwsop34i.DLL")
AddExtender("WWWNT34I.DLL")

IntControl(5,1,0,0,0)  ;;See system and hidden files
IntControl(12,5,0,0,0) ;;Allow quiet termination of script
IntControl(73,2,0,0,0) ;;Error handling

GoSub Init

;;;Ask for Traget or get it from command line
If param0 == 0
   bkdrv=AskDirectory("Please Pick the Drive or Directory to Place The Backup", "", "", "", 4 )
   bkdrv=StrFix(bkdrv,"",StrLen(bkdrv)-1)  ;;remove the "\"
Else
   bkdrv = (param1)
   If StrSub (bkdrv, StrLen(bkdrv), 1) == "\" ;;Find out if there is a "\" at the end of the string
      bkdrv=StrFix(bkdrv,"",StrLen(bkdrv)-1) ;;remove the "\"
   EndIf
EndIf

List=DirItemRecursive("","z:\ftpsite","*.*",0,9999)     ;;Get a list of all directories and sub-directories
eee=Arrayize(list,@TAB)  ;;;Make and Array of the returned directory list
ListCount=ArrInfo(eee,1)

For x=0 To ListCount
   Dir=eee[x]
   If !DirExist(StrCat(bkdrv,ItemExtract( 2, dir, ":" )))
      DirMake(StrCat(bkdrv,ItemExtract( 2, dir, ":" )))  ;;Create directory on target if it doesn't exist
   EndIf
   ret = wntSecurityGet("", Dir, 300, 1, 0) ;;Find out if security rights are inherited or not
   If ret == 1   ;;; 1 = Not inhereted    0 = inherited
      Rights=GetAccess(Dir)
      Count=ItemCount(Rights,@TAB)
      For y = 1 To Count
         AccessList=ItemExtract(y,Rights,@TAB)
         User=ItemExtract( 1, AccessList, "," )
         Access=ItemExtract( 2, AccessList, "," )
         wntAccessAdd("",StrCat(bkdrv,ItemExtract( 2, Dir, ":" )), user, 300, access ) ;;Apply non inherited security rights to the target
      Next
      wntAccessMod("",StrCat(bkdrv,ItemExtract( 2, Dir, ":" )), 300, 2, 1 )  ;;; Remove inherit permisions flag
   EndIf
Next

Drop(list)

;;Copy all files from source to target one directory at a time
dirlist=DirItemize("z:\FTPSite\*.*")
dcount=ItemCount(dirlist, @TAB)

GoSub progress      ;;put up a progress bar

For XX = 1 To dcount
   folder=ItemExtract(XX, dirlist, @TAB)

   f=0
   ccc=0
   eee=0

   ;;;;;;;;Getting File Count Here for Selected Folder
   result=FileItemRecursive("",StrCat("z:\FTPSite\",folder,"\"),'*.*',0,9999)
   eee=Arrayize(result,@TAB)
   ccc=ArrInfo(eee, 1 )

   While 1

      f=f+1
      If f==ccc+1 Then Break
      exefile=eee[f-1]

      file=ItemExtract(2, exefile, ":")

      FilesToCopy=(ccc+10)
      FilesCopied=(f-1)

      BoxTitle(StrCat("Please wait. Copying %folder% folder item: ",f," of ",FilesToCopy))
      BoxDataClear(Thermid,"Hello")
      BoxUpdates(Thermid,0)
      BoxColor(Thermid,BLUE,0)
      xxx= 100+((800*FilesCopied)/FilesToCopy)
      per=(100.0*FilesCopied)/FilesToCopy
      per=Int(per)
      BoxDrawRect(Thermid,"104,554,%xxx%,796",2)
      BoxColor(Thermid,GRAY,0)
      BoxDrawRect(Thermid,"%xxx%,554,896,796",2)
      BoxPen(Thermid,BLACK,ThermPenWidth)
      BoxDrawLine(Thermid,rectThermLine1)
      BoxDrawLine(Thermid,rectThermLine2)
      BoxDrawLine(Thermid,rectThermLine3)
      BoxDrawLine(Thermid,rectThermLine4)

      If per<50 Then BoxTextColor(Thermid,WHITE)
      BoxDrawText(Thermid,rectThermPercent,"%per%%%",0,0)
      BoxTextColor(Thermid,BLACK)
      BoxColor(Thermid,LTGRAY,0)
      BoxDrawText(Thermid,rectThermText1,StrCat("Copying: ",file),1,0)

      BoxUpdates(Thermid,2)

      DirMake(FilePath(StrCat(bkdrv,file)))

      diff = 0

      If FileSize(exefile) == 0 Then diff = 1        ;;copy 0 byte files

      ;;Check if source file is different from target and only copy if different
      ErrorMode(@OFF)
      If FileSizeEx(exefile) != FileSizeEx(StrCat(bkdrv,file)) Then diff = 1
      If FileTimeGet(exefile) != FileTimeGet(StrCat(bkdrv,file)) Then diff = 1
      ErrorMode(@CANCEL)

      If diff == 1
         aFileCopy(exefile, StrCat(bkdrv,file), 4|1024)
      EndIf

   EndWhile

Next XX

;;Copy files in the root of the top level source directory
:files
filelist=FileItemize("z:\FTPSite\*.*")
fcount=ItemCount(filelist, @TAB)

For X = 1 To fcount

   file=Propercase(ItemExtract(X, filelist, @TAB))
   fsrc = StrCat("z:\FTPSite\",file)
   ftarg = StrCat(bkdrv,"\FTPSite\",file)
   flags = 4|1024
   aFileCopy(fsrc,ftarg,flags)

Next X
BoxDestroy(3)

Exit

:Init
;;; Propercase Function
#DefineFunction Propercase(sbad)
sbad=StrLower(sbad)
count=ItemCount(sbad," ")
sgood=""

For xx=1 To count
   thisword=ItemExtract(xx,sbad," ")
   thislen=StrLen(thisword)
   If thislen > 1
      thisword=StrCat(StrUpper(StrSub(thisword,1,1)),StrSub(thisword,2,-1))
   Else
      If thislen==1
         thisword=StrUpper(thisword)
      EndIf
   EndIf
   If sgood=="" Then sgood=thisword
      Else sgood=StrCat(sgood," ",thisword)
Next
Return(sgood)
#EndFunction

#DefineFunction FileItemRecursive(tree,dir,mask,currentlevel,maxlevel)
If currentlevel>=maxlevel Then Return(tree)
currentlevel=currentlevel+1

DirChange(dir)
dlist=ItemSort(DirItemize("*.*"),@TAB)
dcount=ItemCount(dlist,@TAB)

For xx=1 To dcount
   thisdir=ItemExtract(xx,dlist,@TAB)
   tree=FileItemRecursive(tree,thisdir,mask,currentlevel,maxlevel)
Next

If tree=="" Then tt=""
   Else tt=@TAB

flist=FileItemPath(mask)
If flist != ""
   flist=ItemSort(flist,@TAB)
   tree=StrCat(tree,tt,flist)
EndIf

DirChange("..")
Return(tree)

#EndFunction

#DefineFunction DirItemRecursive(tree,dir,mask,currentlevel,maxlevel)
;DebugTrace(@ON,"xxxdebug.txt")
If currentlevel>=maxlevel Then Return(tree)
currentlevel=currentlevel+1

DirChange(dir)

If tree=="" Then tree=DirGet()
   Else tree=StrCat(tree,@TAB,DirGet())
dlist=ItemSort(DirItemize("*.*"),@TAB)
dcount=ItemCount(dlist,@TAB)

For xx=1 To dcount
   thisdir=ItemExtract(xx,dlist,@TAB)
   tree=DirItemRecursive(tree,thisdir,mask,currentlevel,maxlevel)
Next

DirChange("..")
Return(tree)
#EndFunction

#DefineFunction GetAccess(dir)
records=""
userlist = wntAccessList("",dir , 300, 1)
usercount = ItemCount(userlist,@TAB)
For x=1 To usercount
   user=ItemExtract(x,userlist,@TAB)
   permissions=wntAccessGet("",dir,user,300,0)

   If records==""
      records=StrCat(user,",",permissions)
   Else
      records=StrCat(records,@TAB,user,",",permissions)
   EndIf
Next

Return(records)
#EndFunction

Return


:WBERRORHANDLER
IntControl(73,2,0,0,0)
Return


:progress
;;;;;;;; Assorted Colors ;;;;;;;;;
LTGRAY="192,192,192"
GRAY="128,128,128"
GREEN="0,255,0"
BLACK="0,0,0"
WHITE="255,255,255"
RED="255,0,0"
BLUE="0,0,255"
YELLOW="255,255,0"

BoxesUp("200,300,700,500",@NORMAL)

rectTherm="0,0,1000,1000"
rectThermLine1="100,550,900,550"
rectThermLine2="900,550,900,800"
rectThermLine3="900,800,100,800"
rectThermLine4="100,800,100,550"
ThermPenWidth=20
rectThermText1="100,50,1000,199"
rectThermPercent="490,625,550,725"

Thermid=3

;Draw thermoneter outline
BoxNew(Thermid,rectTherm,2)
BoxUpdates(Thermid,0)
BoxColor(Thermid,LTGRAY,0)
BoxDrawRect(Thermid,"",2)
; Draw updating thermometer here
; there are 3 virtual pixels per percent
; we have to draw 3 boxes and some text...
BoxDataTag(Thermid,"Hello")

Return

Article ID:   W17212
File Created: 2007:07:03:14:28:50
Last Updated: 2007:07:03:14:28:50