Copy toàn bộ file excel để lưu!

Liên hệ QC

ThuNghi

Hãy cho rồi sẽ nhận!
Thành viên đã mất
Tham gia
16/8/06
Bài viết
3,808
Được thích
4,449
Copy toàn bộ file excel để lưu!
Tôi muốn copy toàn bộ file excel để lưu nhưng sẽ có nhiều file trùng tên nhưng khác folder.
Ưu tiên lấy file nào edit sau cùng.
Nhờ các bạn giúp cho giải pháp.
Cám ơn nhiều!
 
Copy toàn bộ file excel để lưu!
Tôi muốn copy toàn bộ file excel để lưu nhưng sẽ có nhiều file trùng tên nhưng khác folder.
Ưu tiên lấy file nào edit sau cùng.
Nhờ các bạn giúp cho giải pháp.
Cám ơn nhiều!

- Copy toàn bộ là sao? Tất cả các ổ đĩa à? Hay trong 1 thư mục cho trước
- Lưu vào đâu? Vào cùng 1 thư mục cho trước?
 
Upvote 0
Bạn cần thuật toán hay cần code?

Thuật toán:
- Dùng FileSystemObject để lấy danh sách files cho vào mảng - mảng 2 chiều, tên file và ngày. Dùng hàm đệ quy để đi hết cả mọi subFolders.
- Dùng thuộc tính DateLastModified để ghi ngày của file
- Sort mảng theo ngày
- Đọc mảng và copy file. File nào có tên giống file trước nó thì cộng thêm số thứ tự.
 
Upvote 0
Bạn cần thuật toán hay cần code?

Thuật toán:
- Dùng FileSystemObject để lấy danh sách files cho vào mảng - mảng 2 chiều, tên file và ngày. Dùng hàm đệ quy để đi hết cả mọi subFolders.
- Dùng thuộc tính DateLastModified để ghi ngày của file
- Sort mảng theo ngày
- Đọc mảng và copy file. File nào có tên giống file trước nó thì cộng thêm số thứ tự.
Vậy cho giúp code luôn, lâu quá không đụng code.
Cám ơn Bạn.
 
Upvote 0
Cái này cũng dễ mà:
Mã:
Private Sub CopyFiles(ByVal SourceFolder As String, ByVal TargetFolder As String, _
                      ByVal FileType As String, ByVal InSub As Boolean)
  Dim fsoFile As Object, fsoFolder As Object, SubFolder
  Dim FileName As String, TargetFile As String, sExt As String
  Dim dDat1 As Double, dDat2 As Double
  FileType = Replace(FileType, " ", "")
  FileType = ";" & FileType & ";"
  With CreateObject("Scripting.FileSystemObject")
    Set fsoFolder = .GetFolder(SourceFolder)
    For Each fsoFile In fsoFolder.Files
      dDat2 = 0
      FileName = fsoFile.Path
      sExt = .GetExtensionName(FileName)
      If InStr(1, FileType, ";" & sExt & ";", vbTextCompare) Then
        TargetFile = .BuildPath(TargetFolder, fsoFile.Name)
        dDat1 = fsoFile.DateLastModified
        If .FileExists(TargetFile) Then dDat2 = .GetFile(TargetFile).DateLastModified
        If dDat1 > dDat2 Then .CopyFile FileName, TargetFile, True
      End If
    Next fsoFile
    If InSub Then
      For Each SubFolder In fsoFolder.subFolders
        CopyFiles SubFolder.Path, TargetFolder, FileType, True
      Next SubFolder
    End If
  End With
ExitSub:
End Sub
Sub Main()
  Dim SourceFolder As String, TargetFolder As String
  Dim bChk1 As Boolean, bChk2 As Boolean
  With Application.FileDialog(4)
    .AllowMultiSelect = False
    If .Show = -1 Then
      SourceFolder = .SelectedItems(1)
      bChk1 = True
    End If
  End With
  If bChk1 Then
    With Application.FileDialog(4)
      .AllowMultiSelect = False
      If .Show = -1 Then
        TargetFolder = .SelectedItems(1)
        bChk2 = True
      End If
    End With
    If bChk2 Then CopyFiles SourceFolder, TargetFolder, "[COLOR=#ff0000]xlsm;jpg[/COLOR]", True
  End If
End Sub
Lưu ý: Chổ màu đỏ là những loại file mà bạn cần copy
Chạy Sub Main, sẽ có 2 Dialog mở ra. Dialog thứ nhất cho bạn chọn Source Folder và Dialog thứ hai cho bạn chọn Target Folder
Thí nghiệm: Tạo 1 folder tạm trên Desktop rồi chạy Sub và kiểm tra độ chính xác nhé
 
Upvote 0
Web KT
Back
Top Bottom