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
I think this will do what you want (You can remove the /K to make the command window go away).
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.