forked from farishadi/Excel_Macro_References
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindInFolderMoveAndRename
More file actions
39 lines (32 loc) · 1.18 KB
/
FindInFolderMoveAndRename
File metadata and controls
39 lines (32 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Public Sub FindInFolderMoveAndRename()
'This functions gets into the specified folder, looks for the target file name and moves and renames the file
'to the desired name and location
Dim sDirectoryPath As String
Dim fileSys As FileSystemObject
Dim sFolderPath As Variant
Dim objFile As File
Dim strFilename As String
Dim dCompDate As Date
Dim bBOMDownloaded As Boolean
'set up filesys objects and set path for files - change for your folder
sDirectoryPath = Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Downloads"
Set fileSys = New FileSystemObject
Set sFolderPath = fileSys.GetFolder(sDirectoryPath)
'loop through each file and get date created. If larger than dCompDate then store fname
dCompDate = Date & " 00:00:01"
For Each objFile In sFolderPath.Files
If objFile.Name Like "agile_*" And objFile.DateCreated > dCompDate Then
strFilename = objFile.Name
dCompDate = objFile.DateCreated
End If
Next objFile
If strFilename <> "" Then
bBOMDownloaded = True
Name sDirectoryPath & "\" & strFilename As ThisWorkbook.Path & "\" & aBOMVals(i) & ".xls"
Else
bBOMDownloaded = False
End If
Set fileSys = Nothing
Set sFolderPath = Nothing
Set objFile = Nothing
End Sub