Windows删除文件太麻烦,有一个删不掉那以后的都不删了…………
自己做了一个想在目录上右键的时候直接清空目录下的所有文件
碰到删不掉的就跳过,能删就删…………可从Command或者路径名以后怎么取得这个目录下的所有文件(Path)?en ,是不是很简单啊?

解决方案 »

  1.   

    Set objFSO = CreateObject("Scripting.FileSystemObject")Set fd = objFSO.GetFolder("c:\11")
    For Each f1 In fd.Files
        msgbox f1.Name
    Next
      

  2.   

    刚才的有点错,应该是MsgBox f1.Path
      

  3.   

    Dim fso
    Dim fn As StringPrivate Sub Command1_Click()
        Dim fd As String
        Set fso = CreateObject("Scripting.FileSystemObject")
        fd = "d:\a"
        Call getFilenm(fd)
        MsgBox fn
    End SubFunction getFilenm(fdnm As String)
        Dim obFd, fl, sfd
        Set obFd = fso.GetFolder(fdnm)
        For Each fl In obFd.Files
            fn = fn & fl.Name & Chr(10)
        Next
        If obFd.SubFolders.Count > 0 Then
            For Each sfd In obFd.SubFolders
                Call getFilenm(sfd.Path)
            Next
        End If
    End Function
      

  4.   


    Option Explicit' 获得指定目录下的所有文件的文件名列表
    ' Path 为目录的绝对路径, FileName() 为返回的文件名数组
    Function GetDirectoryFileList(ByVal Path As String, ByRef FileName() As String) As Boolean
        Dim fName As String, i As Long
        If Right$(Path, 1) <> "\" Then Path = Path & "\"
        fName = Dir$(Path & "*.*")
        i = 0
        Do While fName <> ""
            ReDim Preserve FileName(i) As String
            FileName(i) = fName
            fName = Dir$
            i = i + 1
        Loop
        If i <> 0 Then
            ReDim Preserve FileName(i - 1) As String
            GetDirectoryFileList = True
        Else
            GetDirectoryFileList = False
        End If
    End FunctionPrivate Sub Command1_Click()
        Dim FileName() As String, i As Long
        GetDirectoryFileList "c:\", FileName
        For i = 0 To UBound(FileName)
            Debug.Print FileName(i)
        Next i
    End Sub
      

  5.   

    我的只用到了dir函数,没用对象。  :D
      

  6.   

    使用 drivelistbox + dirlistbox +filelistbox 就可以了,可以列出本地硬盘所有文件的列表,你想干什么都行.
      

  7.   

    filename=dir("c:\*.*") 
    i = 0 
    do while filename = "" 
    array1 (i)=filename 
    i = i + 1 
    filename = dir("c:\*.*") 
    loop :)
      

  8.   

    如何在VB中读取目录下所有文件及子目录 
    列出下列源代码。win98,vb5编译通过Private Function ListDirs(ByVal path As String, ByRef Dirs() As String, Recursive As Boolean, Optional Dircount As Long = 0) As Boolean 
    Dim Dirname As String
    Dim Dirstart As Long
    Dim a As Long' On Error GoTo ErrorHandlerDirstart = Dircount + 1
    If (Dircount = 0) Then
    ReDim Dirs(0)
    path = IIf(Right$(path, 1) = "\", path, path + "\")
    End IfDirname = Dir$(path + "*.*", vbDirectory)
    Do While (Dirname <> "")
    If (Dirname <> ".") And (Dirname <> "..") And ((GetAttr(path + Dirname) And vbDirectory) = vbDirectory) Then
    Dircount = Dircount + 1
    ReDim Preserve Dirs(Dircount)
    Dirs(Dircount) = path & Dirname & "\"
    'add to listbox
    'list1.additem Dirs(Dircount)
    End If
    Dirname = Dir
    LoopIf Recursive Then
    For a = Dirstart To Dircount
    If Not ListDirs(Dirs(a), Dirs, Recursive, Dircount) Then
    ListDirs = False
    Exit Function
    End If
    Next
    End If
    ListDirs = True
    Exit FunctionErrorHandler:
    'Any error message(s) can be placed here
    ListDirs = FalseEnd Function 
     
      

  9.   

    http://cache.baidu.com/c?word=vb%2C%C4%BF%C2%BC%3B%CF%C2%3B%B5%C4%3B%CB%F9%D3%D0%3B%CE%C4%BC%FE&url=http%3A//sillybird%2Ey365%2Ecom/vb/vb4%2Ehtm&b=4&user=baidu
      

  10.   

    filename=dir("c:\*.*") 
    i = 0 
    do while filename = "" 
    array1 (i)=filename 
    i = i + 1 
    filename = dir("c:\*.*") 
    loop 
    学习ing……
      

  11.   

    下面是偶一直用的!送给你!
    '00005.遍历目录中所有文件或文件夹
    '*****************************************************************************************
    '   Function:ZZG_collListDirectory
    '*****************************************************************************************
    '           名称                    属性                    说明
    '   参数:  zzg_strPath             String                  需要遍历的文件夹路径
    '           zzg_TypeOpt             Integer                 要取的文件或文件夹类型参数
    '                                                           参数值:0.常规
    '                                                                   2.隐藏
    '                                                                   4.系统文件
    '                                                                   8.磁盘卷标;如果指定,则其它属性都会忽略
    '                                                                   16.目录或文件夹
    '           zzg_blPointDirectory    Boolean                 是否要"."或".."[可选参数,默认为“False”]
    '   返回:  ZZG_collListDirectory   Collection              文件或文件夹集合
    '*****************************************************************************************
    '   引用:
    '*****************************************************************************************
    '   说明:  参数值可以参数累加起来的和。如既要隐藏又要系统文件,可以参数为6。
    '           常规参数总是有效的,因为参数为"0"。
    '           也可以用来查某个文件是否存在,方法就是把该文件的路径代入到zzg_strPath参数中
    '*****************************************************************************************
    '   作者:XXX(此入删去)
    '   电邮:[email protected]
    '   时间:11/27/2003
    '*****************************************************************************************
    Public Function ZZG_collListDirectory(zzg_strPath As String, zzg_TypeOpt As Integer, Optional zzg_blPointDirectory As Boolean = False) As Collection
        On Error GoTo ZZG_collListDirectory_Err                                     '容错
        '定义临时变量
        Dim tmpColl As New Collection                                               '存放文件
        Dim tmpPath As String                                                       '路径
        Dim tmpDir As String                                                        '取得的文件或文件夹名
        '定义部分临时参数值
        tmpPath = zzg_strPath                                                       '路径
        '遍历所有文件或文件夹
        tmpDir = Dir(tmpPath, zzg_TypeOpt)                                          '取第一个文件
        Do While tmpDir <> ""                                                       '当文件取得成功
            If zzg_blPointDirectory Then                                            '是否要带"."或".."的文件夹
                tmpColl.Add tmpDir                                                  '把内容添加到集合中
            Else
                If tmpDir <> "." And tmpDir <> ".." Then                            '判断是否是"."或".."
                    tmpColl.Add tmpDir                                              '把内容添加到集合中
                End If
            End If
            tmpDir = Dir                                                            '取文件
        Loop
        Set ZZG_collListDirectory = tmpColl                                         '返回取得的文件或文件夹集合
        Set tmpColl = Nothing
        Exit Function                                                               '退出函数
    ZZG_collListDirectory_Err:                                                      '标号
        Set tmpColl = Nothing                                                       '撤销tmpColl
        Dim tmpColl_Err As New Collection                                           '产生一个空集合
        Set ZZG_collListDirectory = tmpColl_Err                                     '返回这个空集合
        Set tmpColl_Err = Nothing                                                   '撤销tmpColl_Err
        Exit Function                                                               '退出该函数
    End Function
      

  12.   

    这个比较好的处理了出错的情况
    因为如果某个文件windows自身删除不了,那么程序也无法删除
    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 TypePrivate Declare Function SHFileOperation Lib _
            "shell32" _
            (lpFileOp As SHFILEOPSTRUCT) As LongConst FO_COPY = &H2
    Const FO_DELETE = &H3
    Const FO_MOVE = &H1
    Const FO_RENAME = &H4
    Const FOF_ALLOWUNDO = &H40
    Const FOF_NOCONFIRMATION = &H10
    Dim DirString As StringPrivate Sub Command2_Click()
        Dim xFile As SHFILEOPSTRUCT
        
        '删除根目录下的temp.txt文件
        xFile.pFrom = "c:\bmp\*.*"
        'xFile.pTo = "c:\"
        xFile.wFunc = FO_DELETE
        xFile.hwnd = Me.hwnd
        '将fFlags设置为FOF_ALLOWUNDO
        '允许被删除的文件放置到回收站中
        xFile.fFlags = FOF_ALLOWUNDO
        If SHFileOperation(xFile) Then
            Debug.Print "Success"
        End If
    End Sub