Reputation: 11
I want to find the most recent folder and copy files to destination folder with vbsscript.
This is done here on SO, but the solution provided does not work for me.
Ansgar Wiechers provided this code.
rootFolder = "C:\root"
dstFolder = "C:\dst"
Set fso = CreateObject("Scripting.FileSystemObject")
Set mostRecent = FindMostRecent(fso.GetFolder(rootFolder))
For Each f In mostRecent
f.Copy fso.BuildPath(dstFolder, f.Name)
Next
Function FindMostRecent(fldr)
Set mrf = fldr
For Each sf In fldr.SubFolders
Set mrsf = FindMostRecent(sf)
If mrsf.DateLastModified > mrf.DateLastModified Then Set mrf = mrsf
Next
Set FindMostRecent = mrf
End Function
When changing first line to rootFolder = "D:\"
and second to
dstFolder = "C:\"
, I get the following error:
Script: C:\Users\xxx\Desktop\new 456 82.vbs
Line: 14
Char: 5
Error: Object required: 'FindMostRecent(...)'
Code: 800A01A8
Source: Microsoft VBScript runtime error
Changing root to D:\Files
and destination to C:\test
gives:
Script: C:\Users\Bradley\Desktop\new 456 8.vbs
Line: 7
Char: 1
Error: Object doesn't support this property or method
Code: 800A01B6
Source: Microsoft VBScript runtime error
I have not yet incorporated the copy
command found in the same thread:
cls set knownpath=C:\a for /f "delims=" %%a in ('dir /ad /od /b "%knownpath%"') do ( set name=%%a ) copy "%knownpath%\%name%" "C:\b"
I have no idea where in the script I should place the command. I tried right after Set FindMostRecent = mrf
on a new line, but I get another error. Tried after End
function too, and get another error.
I need this broken down to me as if I was a five year old.
edit: ok so now that the main code is working I'm trying to insert the copy command. This is what I tried so far and it's giving me error.
rootFolder = "D:\test"
dstFolder = "D:\test2"
Set fso = CreateObject("Scripting.FileSystemObject")
Set mostRecent = FindMostRecent(fso.GetFolder(rootFolder))
For Each f In mostRecent.Files
f.Copy fso.BuildPath(dstFolder, f.Name)
Next
Function FindMostRecent(fldr)
Set mrf = fldr
For Each sf In fldr.SubFolders
Set mrsf = FindMostRecent(sf)
If mrsf.DateLastModified > mrf.DateLastModified Then Set mrf = mrsf
Next
Set FindMostRecent = mrf
End Function
cls set knownpath="D:\test" for /f "delims=" %%a in ('dir /ad /od /b "%knownpath%"') do ( set name=%%a ) copy "%knownpath%\%name%" "D:\test2"
EDIT2:
rootFolder = "D:\Files"
dstFolder = "D:\test"
Set fso = CreateObject("Scripting.FileSystemObject")
Set mostRecent = FindMostRecent(fso.GetFolder(rootFolder))
mostRecent.Copy dstFolder
Function FindMostRecent(fldr)
Set mrf = fldr
For Each sf In fldr.SubFolders
Set mrsf = FindMostRecent(sf)
If mrsf.DateLastModified > mrf.DateLastModified Then Set mrf = mrsf
Next
Set FindMostRecent = mrf
End Function
Upvotes: 1
Views: 1071
Reputation: 200193
The following will copy the most recently modified folder from the rootFolder
tree (which could be rootFolder
itself!) to dstFolder
:
rootFolder = "D:\Files"
dstFolder = "D:\test"
Set fso = CreateObject("Scripting.FileSystemObject")
Set mostRecent = FindMostRecent(fso.GetFolder(rootFolder))
mostRecent.Copy dstFolder
Function FindMostRecent(fldr)
Set mrf = fldr
For Each sf In fldr.SubFolders
Set mrsf = FindMostRecent(sf)
If mrsf.DateLastModified > mrf.DateLastModified Then Set mrf = mrsf
Next
Set FindMostRecent = mrf
End Function
Note that with dstFolder = "D:\test"
the instruction mostRecent.Copy dstFolder
will copy the content of mostRecent
to the destination folder:
Before:
D:\
+-Files\
| +-foo.txt
| `-bar\
`-test\
After:
D:\
+-Files\
| +-foo.txt
| `-bar\
`-test\
+-foo.txt
`-bar\
If you want to copy the entire mostRecent
folder:
Before:
D:\
+-Files\
| +-foo.txt
| `-bar\
`-test\
After:
D:\
+-Files\
| +-foo.txt
| `-bar\
`-test\
`-Files\
+-foo.txt
`-bar\
the destination must have a trailing backslash:
mostRecent.Copy dstFolder & "\"
Upvotes: 0
Reputation: 200193
What the code does:
FindMostRecent()
is a recursive (i.e. self-referencing) function that returns the most recently modified folder in the folder tree under the given root folder.
If the given folder does not contain subfolders, the current folder is the most recent folder, so the function defaults to the current folder by setting mrf = fldr
.
If the folder contains subfolders, the most recent folder from each subfolder tree is determined by a recursive call (FindMostRecent(sf)
). The "last modified" date from that folder (mrsf
) is compared to the "last modified" date of the current most recent folder (mrf
). If mrsf
is more recent than mrf
, the latter is replaced with the former.
When each existing subfolder has been checked (if any), mrf
contains the most recent folder from the folder tree under fldr
, which is then returned to the caller.
As for the errors, try adding some debugging code to the function:
Function FindMostRecent(fldr)
Set mrf = fldr
WScript.Echo "TypeName(mrf) = " & TypeName(mrf)
WScript.Echo "mrf = " & mrf.Name
WScript.Echo mrf.Path
For Each sf In fldr.SubFolders
Set mrsf = FindMostRecent(sf)
On Error Resume Next
If mrsf.DateLastModified > mrf.DateLastModified Then Set mrf = mrsf
If Err Then
WScript.Echo "TypeName(mrsf) = " & TypeName(mrsf)
WScript.Echo "mrsf = " & mrsf.Name
End If
On Error Goto 0
Next
Set FindMostRecent = mrf
End Function
That should help you track down at which point the error occurs.
Upvotes: 1