ArunK
ArunK

Reputation: 11

Get Last Modified Time of a Shared Folder in Windows

I need to check a shared Path for Subfolder\File existence. If it exists need to check whether the LastModified Time of the shared path has exceeded more than 1 hour.

I am getting error "Path not found" for shared path, but the code works fine for Normal Directory.

Here is my code

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

This code works for the path mentioned in the script, but it throws error "Path not found" for the path \\server.com\subfolder\subfolder\subfolder.

Upvotes: 1

Views: 1058

Answers (2)

Rich
Rich

Reputation: 4170

If your using a networked shared folder which requires user permissions to access, you should attempt to make a temporarily added network drive so vbscript can attempt to access it with credentials.

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

Stolen from here: Access network share from within VBScript eg FileSystemObject

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

When in doubt, read the documentation. Use the FolderExists method to verify the existence of folders. Use the FileExists method to verify the existence of files.

Don't try to get a folder object unless you have verified it exists. Also, avoid re-using variables for different things.

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

Upvotes: 1

Related Questions