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

How To
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.

How to Recurively Copy Files


Question:

I am just trying to automate a data transfer from a external source like USB Drive or DVD to a specific folder on my hard drive. Here is what I have so far...
DirOne=AskDirectory("Wella Default Folder Selected","","C:\Documents and Settings\%TNumber%\My Documents\Wella Data","",0)
DirTwo=AskDirectory("Select Media Source","","C:\","",0)
FileCopyAttr("%DirTwo%\*.*", "%DirOne%", @FALSE, "r")
I am able to copy the inital files, however on the external source there are folders that need to come over as well... I know this is probably right under my nose, thanks for the assistance...

Answer:

The FileCopy function can only copy a single directory. You need to include code that can recursively copies directories. There are a few ways to do this with WinBatch.:

Option 1 - Use XCopy UDF (User Defined Function):

#DefineFunction UDFXcopy(fromdir,todir)
    Terminate(DirExist(fromdir)==@FALSE,"Eeep","From directory does not exist")
    ;make sure we have a full path or a UNC
    fromtest=StrIndex(fromdir,":",0,@FWDSCAN) + (StrSub(fromdir,1,2)=="\\")
    totest=StrIndex(todir,":",0,@FWDSCAN) + (StrSub(todir,1,2)=="\\")
    Terminate(fromtest==0,"Eeep","Full path must be specified for FromDir")
    Terminate(totest==0,"Eeep","Full path must be specified for ToDir")
    
    DirChange(fromdir)
    ;clean up passed paramters just in case
    If StrSub(fromdir,StrLen(fromdir),1) != "\" Then fromdir=StrCat(fromdir,"\")
    If StrSub(todir,StrLen(todir),1) != "\" Then todir=StrCat(todir,"\")
    
    ;Make sure target directory exists
    If DirExist(todir)==@FALSE Then DirMake(todir)
    
    ;copy files in this directory
    FileCopy( StrCat(fromdir,"*.*") , StrCat(todir,"*.*") , 0)
    ;optional mod instead of above line use following to remode r/o bit
    ;FileCopyAttr( strcat(fromdir,"*.*") , strcat(todir,"*.*") , 0, "r")

    ;get list of subdirectories
    dirlist=DirItemize("*.*")
    count=ItemCount(dirlist,@TAB)
    ;Preocess each subdirectory
    For xx=1 To count
       thisdir=ItemExtract(xx,dirlist,@TAB)
       fulltodir=StrCat(todir,thisdir,"\")
       fullfromdir=StrCat(fromdir,thisdir,"\")
       UDFXcopy(fullfromdir,fulltodir)
    Next
    Return
#EndFunction

UDFXcopy("C:\temp","c:\tempx")
Message("","Xcopy complete")

Option 2 -The Shell Operation extender function aFileCopy.

;Copies directory structure from source to target.
AddExtender("wwsop34i.DLL")
src = "C:\Temp\testsource\*.*"
targ = "C:\Temp\testtarg"
flags = 0
If !DirExist(targ) then DirMake(targ)
afilecopy(src,targ,flags)

Option 3 - The File Searcher extender. See the sample called XCOPY.wbt

AddExtender("wsrch34i.dll")
; XCOPY program using Searcher Extender.
; This new version DOES copy empty files
; and empty subdirectories.
; Here the source and target starting directories are defined
topsrcdir="C:\TEMP\"   ; This directory MUST have a trailing \   !!!
toptargdir="C:\BTEMP\"   ; This directory MUST have a trailing \   !!!
; Precompute a handy number
ttslenplusone=strlen(topsrcdir)+1
ttslensourceplusone=strlen(topsrcdir)+1
BoxOpen("XCOPY  1 %topsrcdir%","Initializing")
; Make top level target dir
if !DirExist(toptargdir) then DirMake(toptargdir)
; Initialize our dir and file counters
filcount=0
dircount=1
; Define search criteria.  Basically
; Start at topsrcdir
; File all files  *.*
; Drill down (16)  New Dir notify (32)   16+32=48
handle=srchInit(topsrcdir,"*.*","","",48) 
; Do the xcopy
while 1   
   foundsrcitem=srchNext(handle)
   if foundsrcitem=="" then break
   targfile=StrCat(toptargdir,strsub(foundsrcitem,ttslensourceplusone,-1))
   ; Check last character of foundsrcitem to see if it is a new directory
    if StrSub(foundsrcitem,strlen(foundsrcitem),1)=="\"
       ; New Dir
       dircount=dircount+1
       if !DirExist(targfile) then DirMake(targfile)
       ; Make Dir
       BoxTitle("XCOPY %dircount% %foundsrcitem%")
    else
       ; New File
       filcount=filcount+1
       FileCopy(foundsrcitem,targfile,0)
       ; Copy File
       BoxText("File %filcount%%@crlf%%foundsrcitem%%@crlf%%targfile%")
   endif
endwhile
srchFree(handle)
Message("XCOPY %dircount% directories","%filcount% files copied")
exit

Option 4 - Call DOS XCOPY.

RunWait("xcopy.exe","C:\data\*.* /M/V/S/Y C:\bkup\data\") 
Or...
RunWait("cmd.exe","/c xcopy   ... ... /M/V/S/Y ...")

Article ID:   W17007
File Created: 2007:07:03:14:27:34
Last Updated: 2007:07:03:14:27:34