获取Windows中共享文件夹的上次修改时间

时间:2016-10-13 14:31:53

标签: vbscript

我需要检查子文件夹\文件存在的共享路径。如果存在,则需要检查共享路径的LastModified Time是否超过1小时。

我收到错误“找不到路径”的共享路径,但代码适用于普通目录。

这是我的代码

Dim fso, folder
folder = "C:/test"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folder)
If folder.Files.Count + folder.SubFolders.Count = 0 Then
    WScript.Echo "Folder is Empty"
ElseIf (folder.DateLastModified > 60) Then
    WScript.Echo "Exceeded 1 hour"
Else
    WScript.Echo "Not Exceeded 1 hour"
End If

此代码适用于脚本中提到的路径,但它会为路径\\server.com\subfolder\subfolder\subfolder引发错误“找不到路径”。

2 个答案:

答案 0 :(得分:0)

如有疑问,请阅读documentation。使用FolderExists方法验证文件夹是否存在。使用FileExists方法验证文件是否存在。

除非您已验证文件夹对象存在,否则请勿尝试获取该文件夹对象。另外,避免为不同的事情重复使用变量。

path = "\\server\share\sub\folder"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(path) Then
    Set folder = fso.GetFolder(path)
    ...
End If

答案 1 :(得分:0)

如果您使用需要用户权限才能访问的联网共享文件夹,则应尝试创建临时添加的网络驱动器,以便vbscript可以尝试使用凭据访问它。

ServerShare = "\\192.168.3.56\d$"
UserName = "domain\username"
Password = "password"

Set NetworkObject = CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")

NetworkObject.MapNetworkDrive "", ServerShare, False, UserName, Password

Set Directory = FSO.GetFolder(ServerShare)
For Each FileName In Directory.Files
    WScript.Echo FileName.Name
Next

Set FileName = Nothing
Set Directory = Nothing
Set FSO = Nothing

NetworkObject.RemoveNetworkDrive ServerShare, True, False

Set ShellObject = Nothing
Set NetworkObject = Nothing

从这里被盗:Access network share from within VBScript eg FileSystemObject