VBA to Copy files using complete path and file nam

2019-09-01 02:30发布

I am writing a macro that needs to:

1 Get file list (around 10k rows) from a specific folder and subfolders of that folder and post it into an excel workbook (Sheet1) with File name and extension "somefile.ext" in column A and complete file path in column C (eg. D:\2014\Client Name\Misc\somefile.ext)

2 Filter out the files that meet my requirement and delete rows that do not.

3 use the complete path from column C to copy those listed files into a new directory but maintaining the subfolder structure so that:

D:\2014\Client Name\Misc\somefile.ext becomes D:\2015\Client Name\Misc\somefile.ext .

Where the path already exists (created with this macro) in the new folder but the file does not.

Now I have made it up to #3 on my own. I am stuck at copying those files, I simply lack the know-how. I am asking you guys for help.

Here is the code that works up to but not including point 3:

Option Explicit
Sub ListFiles()

Dim objFSO As Scripting.FileSystemObject
Dim objTopFolder As Scripting.folder
Dim strTopFolderName As String

Range("A1").Value = "File Name"
Range("B1").Value = "File Type"
Range("C1").Value = "File Patch"

strTopFolderName = "D:\2014"

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTopFolder = objFSO.GetFolder(strTopFolderName)

Call RecursiveFolder(objTopFolder, True)

Columns.AutoFit

End Sub

Sub RecursiveFolder(objFolder As Scripting.folder, _
IncludeSubFolders As Boolean)

Dim objFile As Scripting.file
Dim objSubFolder As Scripting.folder
Dim NextRow As Long

NextRow = Cells(Rows.Count, "A").End(xlUp).Row + 1

For Each objFile In objFolder.Files
    Cells(NextRow, "A").Value = objFile.Name
    Cells(NextRow, "B").Value = objFile.Type
    Cells(NextRow, "C").Value = objFile.path
    NextRow = NextRow + 1
Next objFile

If IncludeSubFolders Then
    For Each objSubFolder In objFolder.SubFolders
        Call RecursiveFolder(objSubFolder, True)
    Next objSubFolder
End If
End Sub
Sub delete_rows()

Dim lastrow As Long
Dim row_index As Long

Application.ScreenUpdating = False
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For row_index = lastrow - 1 To 1 Step -1
If InStr(Cells(row_index, "A").Value, "Processing") = 0 Then
Cells(row_index, "A").EntireRow.Delete
End If
Next
Columns.AutoFit
Application.ScreenUpdating = True
End Sub

1条回答
祖国的老花朵
2楼-- · 2019-09-01 03:06

I think this will do what you want (You can remove the /K to make the command window go away).

  Call Shell("""cmd"" /K copy " & _
    "D:\2014\Client Name\Misc\somefile.ext " & _
    "D:\2015\Client Name\Misc\somefile.ext", vbNormalFocus)

EDIT: Tim's answer (as a comment) is much more straightforward. I was thinking that a "shelled" command could use wildcards, which may be useful and I don't think you can do that using FileCopy.

FileCopy source, destination

source: Required. String expression that specifies the name of the file to be copied. The source may include directory or folder, and drive. destination: Required. String expression that specifies the target file name. The destination may include directory or folder, and drive.

查看更多
登录 后发表回答