紧急求解,怎么删除一个目录下的所有文件和文件夹?
但是不要删除这个目录,比如说删除d:\yyy下的所有目录和文件,但是不要把yyy删了.另:怎样设置一个目录为其享目录,比如d:\yes怎么将它设置成共享目录,就是有一
个手的形状的样子?

解决方案 »

  1.   

    '窗体申明部分
    Private Type SHFILEOPSTRUCT
            hWnd As Long
            wFunc As Long
            pFrom As String
            pTo As String
            fFlags As Integer
            fAnyOperationsAborted As Long
            hNameMappings As Long
            lpszProgressTitle As String
    End Type
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    Dim SHfileOp As SHFILEOPSTRUCT
    '具体实现部分   
    SHfileOp.wFunc = FO_DELETE
    SHfileOp.pFrom = Arguments  '具体要删除哪个目录,给出物理路径
    SHfileOp.fFlags = FOF_NOCONFIRMATION + FOF_SILENT
    SHFileOperation SHfileOp
      

  2.   

    Dim fso As New FileSystemObject
       fso.CreateFolder ("c:\delfolder")
       fso.DeleteFolder ("c:\delfolder")
      

  3.   

    2000下用API来实现目录共享及删除共享
    '共享类型
    Private Const STYPE_ALL       As Long = -1
    Private Const STYPE_DISKTREE  As Long = 0
    Private Const STYPE_PRINTQ    As Long = 1
    Private Const STYPE_DEVICE    As Long = 2
    Private Const STYPE_IPC       As Long = 3
    Private Const STYPE_SPECIAL   As Long = &H80000000'权限
    Private Const ACCESS_READ     As Long = &H1
    Private Const ACCESS_WRITE    As Long = &H2
    Private Const ACCESS_CREATE   As Long = &H4
    Private Const ACCESS_EXEC     As Long = &H8
    Private Const ACCESS_DELETE   As Long = &H10
    Private Const ACCESS_ATRIB    As Long = &H20
    Private Const ACCESS_PERM     As Long = &H40
    Private Const ACCESS_ALL      As Long = ACCESS_READ Or _
                                            ACCESS_WRITE Or _
                                            ACCESS_CREATE Or _
                                            ACCESS_EXEC Or _
                                            ACCESS_DELETE Or _
                                            ACCESS_ATRIB Or _
                                            ACCESS_PERM'共享信息
    Private Type SHARE_INFO_2
      shi2_netname       As Long        '共享名
      shi2_type          As Long        '类型
      shi2_re        As Long        '备注
      shi2_permissions   As Long        '权限
      shi2_max_uses      As Long        '最大用户
      shi2_current_uses  As Long        '
      shi2_path          As Long        '路径
      shi2_passwd        As Long        '密码
    End Type
      
    '设置共享
    Private Declare Function NetShareAdd Lib "netapi32" _
                               (ByVal ServerName As Long, _
                                ByVal level As Long, _
                                buf As Any, _
                                parmerr As Long) As Long'删除共享
    Private Declare Function NetShareDel Lib "netapi32.dll" _
                               (ByVal ServerName As Long, _
                                ByVal ShareName As Long, _
                                ByVal dword As Long) As Long
                         
    '设置共享
    Private Sub Command1_Click()   Dim success As Long
                   
       success = ShareAdd("\\RONGGANG","D:\","VB_Path","VB相关目录","")
                          
    End Sub'删除共享
    Private Sub Command2_Click()
       Dim success As Long
                   
       success = DelShare("\\RONGGANG","VB_Path")
       
    End Sub'设置共享(返回0 为成功)
    '参数:
    'sServer          计算机名
    'sSharePath       要共享路径
    'sShareName       显示的共享名
    'sShareRe     备注
    'sSharePw         密码
    Private Function ShareAdd(sServer As String, _
                              sSharePath As String, _
                              sShareName As String, _
                              sShareRe As String, _
                              sSharePw As String) As Long
       
       Dim lngServer   As Long
       Dim lngNetname  As Long
       Dim lngPath     As Long
       Dim lngRe   As Long
       Dim lngPw       As Long
       Dim parmerr    As Long
       Dim si2        As SHARE_INFO_2
       
       lngServer = StrPtr(sServer)      '转成地址
       lngNetname = StrPtr(sShareName)
       lngPath = StrPtr(sSharePath)
       
       '如果有备注信息
       If Len(sShareRe) > 0 Then
          lngRe = StrPtr(sShareRe)
       End If
       
       '如果有密码
       If Len(sSharePw) > 0 Then
          lngPw = StrPtr(sSharePw)
       End If
          
      '初始化共享信息
       With si2
          .shi2_netname = lngNetname
          .shi2_path = lngPath
          .shi2_re = lngRe
          .shi2_type = STYPE_DISKTREE
          .shi2_permissions = ACCESS_ALL
          .shi2_max_uses = -1
          .shi2_passwd = lngPw
       End With
                              
      '设置共享(用户名,共享类型,共享信息,)
       ShareAdd = NetShareAdd(lngServer, _
                              2, _
                              si2, _
                              parmerr)
                              
    End Function'删除共享(返回0 为成功)
    '参数:
    'sServer       计算机名
    'sShareName    共享名
    Private Function DelShare(sServer As String, _
                              sShareName As String) As Long
       
       Dim lngServer   As Long       '计算机名
       Dim lngNetname  As Long       '共享名   lngServer = StrPtr(sServer)      '转成地址
       lngNetname = StrPtr(sShareName)   '删除共享
       DelShare = NetShareDel(lngServer, lngNetname, 0)End Function
      

  4.   

    98/ME下实现文件夹的共享和删除共享
    在一个独立模块中输入:
    'Subject: Re: NetAccess Add
    'Date:    Wed, 27 Jan 1999 09:00:16 -0700
    'From:    "Eric Hoffman" <[email protected]>
    'Organization: Posted via RemarQ, http://www.remarQ.com - Discussions start here!
    'Newsgroups: microsoft.Public.VB.winapi.networks'*****Here is the code to SHARE FOLDERS under 95/98.******'Note: This is only an example of how to create a Share-Level-Access share.
    'For User-Level-Access it will be a little different. I have finally figured
    'it out, but I haven't put together a clean routine to do it yet. When I do,
    'I'll post It also.Option Explicit' Net Share Errors
    Public Const NERR_NoWorkstation = 2102           ' The workstation driver is not installed.
    Public Const NERR_UnknownServer = 2103           ' The server could not be located.
    Public Const NERR_RemoteOnly = 2106              ' This operation is not supported on workstations.
    Public Const NERR_ServerNotStarted = 2114        ' The Server service is not started.
    Public Const NERR_UnknownDevDir = 2116           ' The device or directory does not exist.
    Public Const NERR_RedirectedPath = 2117          ' The operation is invalid on a redirected resource.
    Public Const NERR_DuplicateShare = 2118          ' The name has already been shared.
    Public Const NERR_NetworkError = 2136            ' A general network error occurred.
    Public Const NERR_InvalidAPI = 2142              ' The requested API is not supported on the remote server.' Standard Errors
    Public Const ERROR_ACCESS_DENIED = 5
    Public Const ERROR_INVALID_PARAMETER = 87
    Public Const ERROR_INVALID_NAME = 123
    Public Const ERROR_INVALID_LEVEL = 124' ******************** Win32 API Declares ********************
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
                      (lpDest As Any, lpSource As Any, ByVal lBytes As Long)
    ' ******************** Net Share Declares ********************
    ' Share Types
    Public Const STYPE_DISKTREE = 0
    Public Const STYPE_PRINTQ = 1
    Public Const STYPE_DEVICE = 2
    Public Const STYPE_IPC = 3
    Public Const STYPE_SPECIAL = &H80000000Public Const SHI_USES_UNLIMITED = -1&' Share Access constants
    Public Const SHI50F_RDONLY = &H1
    Public Const SHI50F_FULL = &H2
    Public Const SHI50F_DEPENDSON = SHI50F_RDONLY Or SHI50F_FULL
    Public Const SHI50F_ACCESSMASK = SHI50F_RDONLY Or SHI50F_FULL
    Public Const SHI50F_PERSIST = &H100    '/* The share is recreated when Windows Starts */
    Public Const SHI50F_SYSTEM = &H200     '/* The share is not normally visible */
    Public Const LM20_NNLEN = 12           '// LM 2.0 Net name length
    Public Const LM20_UNLEN = 20           '// LM 2.0 Maximum user name length
    Public Const LM20_PWLEN = 14           '// LM 2.0 Maximum password length
    Public Const SHPWLEN = 8               '// Share password length (bytes)
    Public Const SHARELEVEL50 = 50Public Type SHARE_INFO_50
        yNetName(LM20_NNLEN)    As Byte         'charshi50_netname[LM20_NNLEN+1];
        '/* share name */
        yType                   As Byte         ' unsigned char shi50_type;
        '/* see below */
        nFlags                  As Integer      ' short shi50_flags;
        '/* see below */
        lpzRe               As Long         ' char FAR *shi50_re;
        '/* ANSI comment string */
        lpzPath                 As Long         ' char FAR *shi50_path;
        '/* shared resource */
        yRWPassword(SHPWLEN)    As Byte         ' char shi50_rw_password[SHPWLEN+1];
        '/* read-write password (share-level security) */
        yROPassword(SHPWLEN)    As Byte         ' char shi50_ro_password[SHPWLEN+1];
        '/* read-only password (share-level security) */
    End TypePublic Declare Function NetShareAdd50 Lib "svrapi" Alias "NetShareAdd" _
                           (ByVal lpzServerName As String, _
                            ByVal nShareLevel As Integer, _
                            ShareInfo As Any, _
                            ByVal nBufferSize As Integer) As LongPublic Declare Function NetShareDelete Lib "svrapi" Alias "NetShareDel" _
                           (ByVal lpzServerName As String, _
                            ByVal sShareName As String, _
                            ByVal nReserved As Integer) As LongPublic Function CreateShare(ByVal sSharePath As String, _
                                ByVal sShareName As String, _
                                ByVal sRe As String, _
                                ByVal sROPass As String, _
                                ByVal sRWPass As String) As Long    ' Create a share that depends on password for RW or RO access.
        ' Returns the errorcode of the create share API call.
        ' Note: this code doesn't check for proper string lengths like it should    Dim ShareInfo       As SHARE_INFO_50
        Dim lReturn         As Long    Dim sServerName     As String
        Dim ySharePath()    As Byte
        Dim yRe()       As Byte    sServerName = ""    ' Create the share on the local computer    With ShareInfo        .yType = STYPE_DISKTREE     ' Disk type share
            .nFlags = SHI50F_PERSIST + SHI50F_DEPENDSON ' + SHI50F_SYSTEM ' if you want the share to be hidden
                                       ' SHI50F_FULL or SHI50F_RDONLY, use one of these instead of SHI50F_DEPENDSON
                                       ' if you only want one kind of access        ' Fill in the path to be shared (Must be in all upper case)
            ySharePath() = StrConv(UCase$(sSharePath & vbNullChar), vbFromUnicode)
            .lpzPath = VarPtr(ySharePath(0))        ' Fill in a comment about the share (Case doesn't matter here)
            yRe() = StrConv(sRe & vbNullChar, vbFromUnicode)
            .lpzRe = VarPtr(yRe(0))        ' Fill in the ShareName to be created (All Upper Case)
            Erase .yNetName()   ' This was just here for debugging in the IDE
            sShareName = UCase$(sShareName & vbNullChar)
            CopyMemory .yNetName(0), ByVal sShareName, Len(sShareName)        ' Fill this in for sharelevel access (Should be all uppercase to avoid NT vs 95/98 troubles)
            Erase .yRWPassword()    ' This was just here for debugging in the IDE
            sRWPass = UCase$(sRWPass & vbNullChar)
            CopyMemory .yRWPassword(0), ByVal sRWPass, Len(sRWPass)        ' Fill this in for sharelevel access (Should be all uppercase to avoid NT vs 95/98 troubles)
            Erase .yROPassword()    ' This was just here for debugging in the IDE
            sROPass = UCase$(sROPass & vbNullChar)
            CopyMemory .yROPassword(0), ByVal sROPass, Len(sROPass)
        End With    lReturn = NetShareAdd50(sServerName, SHARELEVEL50, ShareInfo, LenB(ShareInfo))    Debug.Print "lReturn:"; lReturn    CreateShare = lReturnEnd FunctionPublic Function DeleteShare(ByVal sShareName As String) As Long    ' Remove the share from the local computer    DeleteShare = NetShareDelete("", UCase$(sShareName), 0)    Debug.Print "lReturn:"; DeleteShare, "DLL Error:"; Err.LastDllErrorEnd Function
      

  5.   

    对了,删除文件部分写掉了三个常量的申明,这里补上.
    Private Const FO_DELETE = &H3
    Private Const FOF_NOCONFIRMATION = &H10
    Private Const FOF_SILENT = &H4
      

  6.   

    一、shell "deltree 某文件夹",vbHide
       或其它的解决办法
    二、shell "explorer 某文件夹",true
       sendkeys "某个操作序列"呵呵。
    能够用水果刀解决的问题为什么要用斧头来解决
      

  7.   

    jet9600(司马) :运行时提示:实时错误,53,错文件没找到,
    是不是还有什么代码要加的??
      

  8.   

    to  tianya51(北塘) ( ) 
    删除文件有简单的FSO或者KILL函数,共享如果在98下的话,没有,2000下可以用命令行net SHARE命令
      

  9.   

    用filelist控件
    呵呵
    简单应该可以的
      

  10.   

    假如你要删除d:\yyy下的所有文件及目录
    chdriver d:\
    chdir d:\yyy
    shell "del *.*",vbHide共享文件夹要改一下注册表(很多帖子都说过如何改注册表,我就不重复了)

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Network\LanMan\主键下新建子键myshare$(在网络中显示的共享目录名),然后在下面新建以下项
        "Flags"值为"00000258"("REG_DWORD")
        "Type"值为0("REG_DWORD")
        "Path"值为"d:\yyy" ,使d:\yyy完全共享
      

  11.   

    ’需引用scrrun.dll
    Private Sub DelFolder(byval vFolder as string)    On Error Resume Next
        
        Dim fs As New Scripting.FileSystemObject
        
        Dim tfile As File
        
        Dim tFolder As Folder, tSubFolder As Folder
        
        Set tFolder = fs.GetFolder(vFolder )
        
       
        For Each tfile In tFolder.Files
            
     
            fs.DeleteFile tfile.Path, True    Next
        
        For Each tSubFolder In tFolder.SubFolders
            
            fs.DeleteFolder tSubFolder.Path, True    Next
        
       
        Set fs = NothingEnd Sub