I haven't looked at this in a while, but I believe it works.
copyDir=: 4 : 0
NB.* copyDir: copy directory x. and all its contents to directory y.
fromdir=. endSlash x. [ todir=. endSlash y.
assert. dirExists fromdir
if. -.dirExists todir do. createdir todir end.
assert. dirExists todir
fls=. dir fromdir,'*'
subdirs=. 0{"1 (whd=. 'd' e.&>4{"1 fls)#fls
fls=. 0{"1 (-.whd)#fls
sf=. 0 0 NB. Count successes and failures.
for_fl. fls do. fl=. >fl
if. fexist todir,fl do. ferase todir,fl end. NB. Overwrite existing
ct=. 1 0 NB. Assume 1 success, 0 failures...
try. (fromdir,fl) fcopy todir,fl catch. ct=. 0 1 end.
sf=. sf+ct
end.
for_sbdr. subdirs do. sbdr=. >sbdr
if. -.dirExists sbdr do. createdir sbdr end.
ct=. 1 0 NB. Assume 1 success, 0 failures...
try. ssf=. (fromdir,sbdr) copyDir todir,sbdr catch. ct=. 0 1 end.
sf=. sf+ssf+ct
end.
)
NB.* fcopy: copy file x. to y. using WIN32 API call.
fcopy=: 'CopyFileA' win32apir @: (; ;&1)
NB.EG 'c:\temp\src.xls' fcopy 'c:\temp\dest.xls' NB. Returns 1 iff
successful
endSlash=: 3 : 0
NB.* endSlash: ensure terminal path separator char, e.g. for directory.
y.,PATHSEP_j_#~PATHSEP_j_~:{:y.
NB.EG -:/endSlash &.>'C:\ADir\';'C:\ADir'
NB. 1
)
NB.* dirExists: 0: dir does not exist; 1: exists
dirExists=: 3 : '0&~:#@:dirlist1 (-PATHSEP_j_={:y.)}.y.=. ,y.'
NB.* createdir: Create directory
createdir=: 3 : 'try. 1!:5 <y. catch. 0 end.'
NB.* createDirPath: create (all elements of) directory path.
createDirPath=: 3 : 0
ps=. PATHSEP_j_
dn=. y.
NB. Break path into pieces, e.g. 'C:\tmp\foo'->'C:';'\tmp';'\foo'
pdn=. <;.1 (ps#~ps~:{.dn),(-ps={:dn)}.dn
if. ':' e. dn do. NB. Remove '\' before drive letter
pdn=. (<dn{.~>:dn i. ':') 0}pdn
NB. Combine drive letter w/1st path element, e.g.
'\C:';'\tmp';'\foo'->'C:\tmp';'\foo'
pdn=. ((<ps-.~>0{pdn),&.>1{pdn) 0}}.pdn
end.
NB. Successive directories to create, e.g.
'C:\tmp';'C:\tmp\foo';'C:\tmp\foo\bar'
d2c=. <"1 pdn#~-.dirExists"1 pdn=. ;\pdn
rc=. 1 NB. Success too if they're already all there
if. 0<#d2c do. rc=. createdir&>d2c end.
rc
)
On 7/31/07, Sherlock, Ric <[EMAIL PROTECTED]> wrote:
>
> Options for copying a directory tree:
> 0. write a verb using fcopynew verb from files.ijs and verbs from
> dir.ijs to copy each file in the tree
> 1. do the same but use winapi functions.
> 2. call xcopy
>
>
> I haven't found anything in my forum/wiki search that covers this issue.
> Has anybody already tackled this and is happy to share advice/code?
>
> More on my desired usage:
> I have a set of directory trees.
> Each tree contains files and directories that together define a
> configuration for a simulation program.
>
> Under programmatic control, I want to copy a chosen template to a
> sub-directory of the users directory.
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm