精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● VFP>>〖外部引用〗>>删除某个目录下的所有子目录(包括只读和隐藏属性)的程序!

主题:删除某个目录下的所有子目录(包括只读和隐藏属性)的程序!
发信人: fatwood(冲锋不止(ui))
整理人: hunter__fox(2004-09-28 09:29:43), 站内信件
? delFold("c:\abc\")  &&删除 c:\abc 及其下的所有子目录
? delFold("c:\abc\*.*") &&删除 c:\abc\ 下的文件及其下的所有子目录(不删除 c:\abc)
? delFold("c:\abc\*.txt") && 删除掉 c:\abc\ 下的所有扩展名为"txt"的文件(不包含子目录里的TXT文件)


********** 以下是程序 ********
func delFold
para deltree
local oheap,cdeststring,nstringbase

#define FOF_ALLOWUNDO                   64
#define FOF_CONFIRMMOUSE                 2
#define FOF_FILESONLY                  128
#define FOF_MULTIDESTFILES               1
#define FOF_NOCONFIRMATION              16
#define FOF_NOCONFIRMMKDIR             512
#define FOF_RENAMEONCOLLISION            8
#define FOF_SILENT                       4
#define FOF_SIMPLEPROGRESS             256
#define FOF_WANTMAPPINGHANDLE           32
#define FOF_NOCOPYSECURITYATTRIBS     2048
#define FOF_NOERRORUI                 1024

#define FO_COPY                  2
#define FO_DELETE                3
#define FO_MOVE                  1
#define FO_RENAME                4

Declare integer SHFileOperation in SHELL32.dll string @LPSHFILEOPSTRUCT
Declare integer GetActiveWindow in WIN32API
oheap = createobj('Heap')
cdeststring = deltree + chr(0) + chr(0)
nstringbase = oheap.allocblob(cdeststring)
cfileopstruct = numtodword(getactivewindow()) ;
    + numtodword(FO_DELETE) ;
    + numtodword(nstringbase) ;
    + numtodword(nstringbase + len(cdeststring)) ;
    + numtodword(FOF_NOCONFIRMATION+FOF_NOERRORUI) + chr(0) ;
    + numtodword(0) ;
    + numtodword(0)
If SHFileOperation(cfileopstruct) = 0
retu .T.
Else
retu .F.
Endif
rele oheap
return



Define class heap as custom
Protected inhandle, innumallocsactive,iaallocs[1,3]
inhandle = null
innumallocsactive = 0
iaallocs = null
Name = "heap"

Procedure alloc
Lparameter nsize
Declare integer HeapAlloc in WIN32API as HAlloc integer hHeap, integer dwFlags, integer dwBytes
Declare integer HeapSize in WIN32API as HSize integer hHeap, integer dwFlags, integer lpcMem
Local nptr
With this
nptr = halloc(.inhandle, 0, @nsize)
If nptr # 0
.innumallocsactive = .innumallocsactive + 1
Dimension .iaallocs[.inNumAllocsActive,3]
.iaallocs[.inNumAllocsActive,1] = nptr
.iaallocs[.inNumAllocsActive,2] = hsize(.inhandle, 0, nptr)
.iaallocs[.inNumAllocsActive,3] = .t.
Else
nptr = null
Endif
Endwith
Return nptr
Endproc

Function allocblob
Lparameter cbstringtocopy
Local nallocptr
With this
nallocptr = .alloc(len(cbstringtocopy))
If ! isnull(nallocptr)
.copyto(nallocptr,cbstringtocopy)
Endif
Endwith
Return nallocptr
Endfunc

Function allocstring
Lparameter cstring
Return this.allocblob(cstring + chr(0))
Endfunc

Function allocinitas
Lparameter nsizeofbuffer, nbytevalue
If type('nByteValue') # 'N' or ! between(nbytevalue,0,255)
nbytevalue = 0
Endif
Return this.allocblob(replicate(chr(nbytevalue),nsizeofbuffer))
Endfunc

Procedure dealloc
Lparameter nptr
Declare integer HeapFree in WIN32API as HFree ;
integer hHeap, ;
integer dwFlags, ;
integer lpMem
Local nctr
nctr = null
With this
nctr = .findallocid(nptr)
If ! isnull(nctr)
=hfree(.inhandle, 0, nptr)
.iaallocs[nCtr,3] = .f.
Endif
Endwith
Return ! isnull(nctr)
Endproc

Procedure copyto
Lparameter nptr, csource
Declare RtlMoveMemory in WIN32API as RtlCopy ;
integer nDestBuffer, ;
string @pVoidSource, ;
integer nLength
Local nctr
nctr = null
If type('nPtr') = 'N' and type('cSource') $ 'CM' ;
and ! (isnull(nptr) or isnull(csource))
With this
nctr = .findallocid(nptr)
If ! isnull(nctr)
=rtlcopy((.iaallocs[nCtr,1]), ;
csource, ;
min(len(csource),.iaallocs[nCtr,2]))
Endif
Endwith
Endif
Return ! isnull(nctr)
Endproc

Procedure copyfrom
Lparameter nptr
Declare RtlMoveMemory in WIN32API as RtlCopy ;
string @DestBuffer, ;
integer pVoidSource, ;
integer nLength
Local nctr, ubuffer
ubuffer = null
nctr = null
If type('nPtr') = 'N' and ! isnull(nptr)
With this
nctr = .findallocid(nptr)
If ! isnull(nctr)
ubuffer = repl(chr(0),.iaallocs[nCtr,2])
=rtlcopy(@ubuffer, ;
(.iaallocs[nCtr,1]), ;
(.iaallocs[nCtr,2]))
Endif
Endwith
Endif
Return ubuffer
Endproc

Protected function findallocid
Lparameter nptr
Local nctr
With this
For nctr = 1 to .innumallocsactive
If .iaallocs[nCtr,1] = nptr and .iaallocs[nCtr,3]
Exit
Endif
Endfor
Return iif(nctr <= .innumallocsactive,nctr,null)
Endwith
Endproc

Procedure sizeofblock
Lparameters nptr
Local nctr, nsizeofblock
nsizeofblock = null
With this
nctr = .findallocid(nptr)
Return iif(isnull(nctr),null,.iaallocs[nCtr,2])
Endwith
Endproc

Procedure destroy
Declare HeapDestroy in WIN32API as HDestroy integer hHeap
Local nctr
With this
For nctr = 1 to .innumallocsactive
If .iaallocs[nCtr,3]
.dealloc(.iaallocs[nCtr,1])
Endif
Endfor
hdestroy[.inHandle]
Endwith
DoDefault()
Endproc

Procedure init
Declare integer HeapCreate in WIN32API as HCreate ;
integer dwOptions, ;
integer dwInitialSize, ;
integer dwMaxSize
#Define swapfilepagesize 4096
#Define blockallocsize 2 * swapfilepagesize
With this
.inhandle = hcreate(0, blockallocsize, 0)
Dimension .iaallocs[1,3]
.iaallocs[1,1] = 0
.iaallocs[1,2] = 0
.iaallocs[1,3] = .f.
.innumallocsactive = 0
Endwith
Return (this.inhandle # 0)
Endproc

Enddefine

Function setmem
Lparameters nptr, csource
Declare RtlMoveMemory in WIN32API as RtlCopy ;
integer nDestBuffer, ;
string @pVoidSource, ;
integer nLength
rtlcopy(nptr, csource, len(csource))
Return .t.
Endfunc

Function getmem
Lparameters nptr, nlen
Declare RtlMoveMemory in WIN32API as RtlCopy ;
string @DestBuffer, ;
integer pVoidSource, ;
integer nLength
Local ubuffer
ubuffer = repl(chr(0),nlen)
=rtlcopy(@ubuffer, nptr, nlen)
Return ubuffer
Endfunc

Function getmemstring
Lparameters nptr, nsize
Declare integer lstrcpyn in WIN32API as StrCpyN ;
string @ lpDestString, ;
integer lpSource, ;
integer nMaxLength
Local ubuffer
If type('nSize') # 'N' or isnull(nsize)
nsize = 512
Endif
ubuffer = repl(chr(0), nsize)
If strcpyn(@ubuffer, nptr, nsize-1) # 0
ubuffer = left(ubuffer, max(0,at(chr(0),ubuffer) - 1))
Else
ubuffer = null
Endif
Return ubuffer
Endfunc

Function shorttonum
Lparameter tcint
Local b0,b1,nretval
b0=asc(tcint)
b1=asc(subs(tcint,2,1))
If b1<128
nretval=b1 * 256 + b0
Else
b1=255-b1
b0=256-b0
nretval= -( (b1 * 256) + b0)
Endif
Return nretval
Endfunc

Function numtoshort
Lparameter tnnum
Local b0,b1,x
If tnnum>=0
x=int(tnnum)
b1=int(x/256)
b0=mod(x,256)
Else
x=int(-tnnum)
b1=255-int(x/256)
b0=256-mod(x,256)
If b0=256
b0=0
b1=b1+1
Endif
Endif
Return chr(b0)+chr(b1)
Endfunc

Function dwordtonum
Lparameter tcdword
Local b0,b1,b2,b3
b0=asc(tcdword)
b1=asc(subs(tcdword,2,1))
b2=asc(subs(tcdword,3,1))
b3=asc(subs(tcdword,4,1))
Return ( ( (b3 * 256 + b2) * 256 + b1) * 256 + b0)
Endfunc

Function numtodword
Lparameter tnnum
Return numtolong(tnnum)
Endfunc

Function wordtonum
Lparameter tcword
Return (256 *  asc(subst(tcword,2,1)) ) + asc(tcword)
Endfunc

Function numtoword
Lparameter tnnum
Local x
x=int(tnnum)
Return chr(mod(x,256))+chr(int(x/256))
Endfunc

Function numtolong
Lparameter tnnum
Declare RtlMoveMemory in WIN32API as RtlCopyLong ;
string @pDestString, ;
integer @pVoidSource, ;
integer nLength
Local cstring
cstring = space(4)
=rtlcopylong(@cstring, bitor(tnnum,0), 4)
Return cstring
Endfunc

Function longtonum
Lparameter tclong
Declare RtlMoveMemory in WIN32API as RtlCopyLong ;
integer @ DestNum, ;
string @ pVoidSource, ;
integer nLength
Local nnum
nnum = 0
=rtlcopylong(@nnum, tclong, 4)
Return nnum
Endfunc

Function allocnetapibuffer
Lparameter nsize
If type('nSize') # 'N' or nsize <= 0
Return null
Endif
If ! 'NT' $ os()
Return null
Endif
Declare integer NetApiBufferAllocate in NETAPI32.dll ;
integer dwByteCount, ;
integer lpBuffer
Local nbufferpointer
nbufferpointer = 0
If netapibufferallocate(int(nsize), @nbufferpointer) # 0
nbufferpointer = null
Endif
Return nbufferpointer
Endfunc

Function deallocnetapibuffer
Lparameter nptr
If type('nPtr') # 'N'
Return .f.
Endif
If ! 'NT' $ os()
Return .f.
Endif
Declare integer NetApiBufferFree in NETAPI32.dll ;
integer lpBuffer
Return (netapibufferfree(int(nptr)) = 0)
Endfunc

Function copydoubletostring
Lparameter ndoubletocopy
Declare RtlMoveMemory in WIN32API as RtlCopyDbl ;
string @DestString, ;
double @pVoidSource, ;
integer nLength
Local cstring
cstring = space(8)
=rtlcopydbl(@cstring, ndoubletocopy, 8)
Return cstring
Endfunc

Function doubletonum
Lparameter cdoubleinstring
Declare RtlMoveMemory in WIN32API as RtlCopyDbl ;
double @DestNumeric, ;
string @pVoidSource, ;
integer nLength
Local nnum
nnum = 0.000000000000000000
=rtlcopydbl(@nnum, cdoubleinstring, 8)
Return nnum
Endfunc




----
传纸仔

[关闭][返回]