我正在寻找一种方法来查找最新的文件夹,将文件内部复制到目标文件夹。
答案 0 :(得分:0)
试试这个:
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
答案 1 :(得分:0)
我发现您将文件夹作为参数传递时遇到了一些困难。 Ansgar Wiechers的例子展示了如何用硬编码值来做到这一点。为简单起见,我们通常会在答案中这样做。
rootFolder = "C:\root" 'target folder (where to search)
dstFolder = "C:\dst" 'destionation (where to copy)
但如果你喜欢像他们那样动态地传递它们......
CScript my_task.vbs C:\root C:\dst
...然后在.vbs文件的开头包含此内容:
With WScript.Arguments
If .Count < 2 Then WScript.Quit(-1)
rootFolder = .Item(0)
dstFolder = .Item(1)
End With
接下来,通过阅读您的评论,在我看来,您只需要直接子文件夹,如果是这样,因为在批处理脚本中不太好,我会做这样的事情:
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
二手DIR开关:
/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