我想使用 vbsscript 找到最新的文件夹并将文件复制到目标文件夹。
这是在 SO 上完成的,但提供的解决方案对我不起作用。
Ansgar Wiechers 提供了此代码。
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
将第一行更改为rootFolder = "D:\"
并将第二行更改为 时
dstFolder = "C:\"
,出现以下错误:
Script: C:\Users\xxx\Desktop\new 456 82.vbs
Line: 14
Char: 5
Error: Object required: 'FindMostRecent(...)'
Code: 800A01A8
Source: Microsoft VBScript runtime error
更改根到D:\Files
和目标C:\test
给出:
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
我还没有合并copy
在同一个线程中找到的命令:
cls set knownpath=C:\a for /f "delims=" %%a in ('dir /ad /od /b "%knownpath%"') do ( set name=%%a ) copy "%knownpath%\%name%" "C:\b"
我不知道应该在脚本中的哪个位置放置命令。我Set FindMostRecent = mrf
在换行后立即尝试,但出现另一个错误。也尝试过End
功能,并得到另一个错误。
我需要把它分解给我,就好像我是一个五岁的孩子一样。
编辑:好的,现在主要代码正在运行,我正在尝试插入复制命令。这是我到目前为止所尝试的,它给了我错误。
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"
编辑2:
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