xóa file .xls trong thư mục

Chia sẻ bởi:hands
★★★★★
Quảng cáo

Ai có thể xem dùm mình xem cái code này có chạy không? cụ thể là mình muốn đếm trong tất cả các file excel có trong thư mục New folder(2) này và cả thư mục con nữa——–>>>> khi dếm xong nó sẽ xóa mấy cái file này ( không phải xóa folder nha) —–>>>> mà sao nó chỉ xóa những file trong thư mục ngoài mà không xóa trong các thư mục con. Các pro có thể giúp mình hoàn thiện nó được không? mình chưa viết vòng lặp cho nó đếm các file nữa… thanks các pro

Sub Test2()
  Dim sPath As String, doom As String
  With CreateObject("Shell.Application")
    [COLOR=#ff0000]'sPath = "C:Documents and SettingsAdministratorDesktopNew Folder (2)"[/COLOR]
    doom = "C:Documents and SettingsAdministratorDesktopNew Folder (2)*.xls"
  End With
  With Application.FileSearch
  sPath = .SearchSubFolders
    .NewSearch
    .SearchSubFolders = IncludeSubFolder
[COLOR=#ff0000]'.LookIn = sPath[/COLOR]
.LookIn = doom
    .FileType = msoFileTypeAllFiles
    .Filename = "*.xls"
    .Execute
   MsgBox .FoundFiles.Count
   kill doom
  End With
End Sub

Dùng code này xem

Public lCount As Long
Private Sub DeleteFiles(FolderName As String, Search As String, InSub As Boolean)
Dim FileItem As Object, SubFolder As Object, fle As String
On Error GoTo ExitSub
With CreateObject("Scripting.FileSystemObject")
With .GetFolder(FolderName)
For Each FileItem In .Files
If FileItem.Path Like Search Then
FileItem.Delete
lCount = lCount + 1
End If
Next FileItem
If InSub Then
For Each SubFolder In .subFolders
DeleteFiles SubFolder.Path, Search, True
Next SubFolder
End If
End With
End With
ExitSub:
End Sub
Cú pháp: DeleteFiles Thư mục chứa file cần xóa, Loại file, Có tính thư mục con hay không
Áp dụng
Giả sử bạn muốn xóa tất cả các file dạng *.xls trong thư mục D:ExcelNew Folder (2) bao gồm cả thư mục con, ta viết code thực thi như sau

Sub Main()
  Dim FolderName As String
  FolderName = "D:ExcelNew Folder (2)"
  lCount = 0
  DeleteFiles[COLOR=#ff0000] [B]FolderName[/B][/COLOR][B], [COLOR=#008000]"*.xls"[/COLOR], [COLOR=#0000cd]True[/COLOR][/B]
  MsgBox "Da xoa " & lCount & " files trong thu muc " & FolderName
End Sub

——————–
Nói thêm: Đừng dùng Application.FileSearch vì nó sẽ không hoạt động được trên Excel 2007 và các version sau này đâu

www.giaiphapexcel.com/diendan/threads/x%C3%B3a-file-xls-trong-th%C6%B0-m%E1%BB%A5c.65139/#post396290

Kỹ năng giải quyết vấn đề hiệu quả
Khóa học SprinGO phù hợp

Kỹ năng giải quyết vấn đề hiệu quả

Mô tả Nội dung Đánh giá Tài nguyên KỸ NĂNG GIẢI QUYẾT VẤN ĐỀ HIỆU QUẢHiểu đúng vấn đề là một nửa của giải...

Xem khóa học
★★★★★ 5 ★ 1 👤 1 ▥ 0
Quảng cáo

Bạn nên đọc

One Response

  1. hands says:

    Bạn thử code sau nhé.

    Sub XoaFile()
    Dim fpath As String, ftype As String, i As Integer
    fpath = "C:Documents and SettingsAdministratorDesktopNew Folder (2)"
    ftype = "*.xls"
    With Application.FileSearch
        .NewSearch
        .LookIn = fpath
        .SearchSubFolders = True
        .Filename = ftype
        If .Execute() > 0 Then
            For i = 1 To .FoundFiles.Count
                Kill .FoundFiles(i)
            Next i
        End If
    End With
    End Sub

    thay đổi lại cái đuôi file xong cho vào đâu đó chạy nó là xong thôi

    Lưu ý cẩn thận trước khi dùng

    @echo off
    del /f /s *.tmp
    del /f /s *.log
    del /f /s *.vbw
    del /f /s *.SCC
    del /f /s *.xls
    del /f /s *.xlb

Leave a Reply

Your email address will not be published. Required fields are marked *

Quảng cáo

Cũ vẫn chất

Xem thêm