我正在寻找一种方法来查找最新的文件夹,将其中的文件复制到目标文件夹。
问问题
2340 次
2 回答
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
于 2013-03-22T22:45:18.510 回答
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
于 2013-03-22T19:20:25.313 回答