0
votes

I am looking for a way to find the most recent folder, copy the files inside to a destination folder.

2

2 Answers

0
votes

I see you met some difficulties with passing your folders as arguments. Ansgar Wiechers's example showed how to do that with hard-coded values. That we do usually in our answers for simplicity sake.

rootFolder = "C:\root"    'target folder (where to search)
dstFolder  = "C:\dst"     'destionation (where to copy)

But if you prefer to pass them dynamical like...

CScript my_task.vbs C:\root C:\dst

...then include this at the beginning of your .vbs file:

With WScript.Arguments
    If .Count < 2 Then WScript.Quit(-1)
    rootFolder = .Item(0)
    dstFolder  = .Item(1)
End With

Next, by reading your comments, seems to me you need just the immediate subfolders, and if so, as am not so good in batch scripting, I'll do something like this:

tmpFile    = "result.txt" 'temp file
With CreateObject("WScript.Shell")
    .CurrentDirectory = rootFolder
    .Run "CMD /C DIR /A:D /B /O:-D /T:C > " & tmpFile, 0, True
    With CreateObject("Scripting.FileSystemObject")
        With .OpenTextFile(tmpFile)
            mostRecent = .ReadLine
        End With
        .GetFile(tmpFile).Delete
    End With
    .CurrentDirectory = mostRecent
    .Run "CMD /C COPY *.* " & dstFolder, 0, False
End With

Used DIR switches:

/A:D  = attributes Directory
/B    = bare format
/O:-D = order by date/time (newly first)
/O:D  = order by date/time (oldest first)
/T:C  = sort by Creation
/T:A  = sort by Last Access
/T:W  = sort by Last Written
0
votes

Try this:

rootFolder = "C:\root"
dstFolder  = "C:\dst"

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