PDA

View Full Version : How do I move files into latest created subfolder



GDB
05-05-2019, 04:53 AM
Hi

I have a folder that has subfolders in it.

Im moving files into the folder but im trying to then move the files into the latest created subfolder.

Can you do that via VBA?

Logit
05-05-2019, 06:56 AM
.
This is one method :

https://stackoverflow.com/questions/28902162/to-search-for-a-last-updated-folder-in-a-directory-using-vba

Paul_Hossler
05-05-2019, 07:00 AM
https://ss64.com/vb/filesystemobject.html

https://www.rondebruin.nl/win/s3/win026.htm


FileSystemObject can move files

Here's how to find the latest folder if you don't know it

An example will help … like


"I want to move all the files in Folder1 (which I know / would like to select) into the subfolder with the latest create date (which I want the system to find)

Folder1
File1
File2
FIle3

SubfolderA (created 3/4/2019)

SubfolderB (created 4/4/2019)


So SubfolderB ends with File1, File2, and File3






Option Explicit
Sub test()
MsgBox LatestFolder("C:\Users\Daddy\Documents")
End Sub


'https://ss64.com/vb/filesystemobject.html
Function LatestFolder(sPath As String) As String
Dim oFSO As Object, oFolders As Object, oFolder As Object
Dim sNewestFolder As String
Dim dtDate As Date


dtDate = 0

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolders = oFSO.GetFolder(sPath).SubFolders

For Each oFolder In oFolders
If oFolder.DateCreated > dtDate Then
sNewestFolder = oFolder.Name
dtDate = oFolder.DateCreated

' Debug.Print sNewestFolder, dtDate

End If
Next

LatestFolder = sNewestFolder

End Function

GDB
05-05-2019, 09:33 AM
spot on big thanks