DateLastAccessed Help

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

DateLastAccessed Help

Post by new_user »

I have a script that reads a test file of servers and reports back shares and some share information like sharename and path. I am trying to use the same code or different (bt base it off the output) to determine in the directories shared out all of the folders and files DateLastAccessed attribute and I am pretty lost. My orgininal code is below, any help is greatly appreciated. Thanks!

On Error Resume Next
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objTextFile = objFSO.OpenTextFile("c:servers.txt", ForReading)
Do Until objTextFile.AtEndOfStream strComputer = objTextFile.Readline
set objWMI = GetObject("winmgmts:" & strComputer & "rootcimv2") set colShares = objWMI.InstancesOf("Win32_Share where type < 1") for each objShare in colShares 'WScript.Echo objShare.Name 'WScript.Echo " Path: " & objShare.Path 'WScript.Echo " Allow Max: " & objShare.AllowMaximum 'WScript.Echo " Caption: " & objShare.Caption 'WScript.Echo " Max Allowed: " & objShare.MaximumAllowed 'WScript.Echo " Type: " & objShare.Type 'wscript.echo WScript.Echo strComputer & ";" & objShare.Name & ";" & objShare.Pathnext
Loop
objTextFile.Close

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

DateLastAccessed Help

Post by new_user »

I have a script that reads a test file of servers and reports back shares and some share information like sharename and path. I am trying to use the same code or different (bt base it off the output) to determine in the directories shared out all of the folders and files DateLastAccessed attribute and I am pretty lost. My orgininal code is below, any help is greatly appreciated. Thanks!

On Error Resume Next
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")Set objTextFile = objFSO.OpenTextFile("c:servers.txt", ForReading)
Do Until objTextFile.AtEndOfStream strComputer = objTextFile.Readline
set objWMI = GetObject("winmgmts:" & strComputer & "rootcimv2") set colShares = objWMI.InstancesOf("Win32_Share where type < 1") for each objShare in colShares 'WScript.Echo objShare.Name 'WScript.Echo " Path: " & objShare.Path 'WScript.Echo " Allow Max: " & objShare.AllowMaximum 'WScript.Echo " Caption: " & objShare.Caption 'WScript.Echo " Max Allowed: " & objShare.MaximumAllowed 'WScript.Echo " Type: " & objShare.Type 'wscript.echo WScript.Echo strComputer & ";" & objShare.Name & ";" & objShare.Pathnext
Loop
objTextFile.Close

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

DateLastAccessed Help

Post by jhicks »

First off, in my experience the last access time stamp can be misleading as programs like virus scanners can affect that value. Usually last write or last modified is the value you really want. Now as for the question, I'm not sure I follow. Are you trying to find out when files were last modified in a shared folder? How deep into the shared folder do you need to go? Once you know the share name, you could use the FileSystem object to enumerate files and folders and get the last modified values that way.

User avatar
rasimmer
Posts: 182
Joined: Fri Jan 30, 2009 12:37 am

DateLastAccessed Help

Post by rasimmer »

Well, there are serveral options. Basically use the FileSystemObject and recurse through the directories which I would normally connect to the adminstrative share (i.e.SERVER2D$HOME) and do FOR EACH. If you have huge shares and Windows Servers vs. a NAS device that the data is stored on, I would run the script on the local server if I could.

Your next option is using WMI's CIM_DATAFILE method to gather the information. The biggest problem with this method is that it's slow, but you can basically create a SELECT statement and narrow down your results Select * From CIM_DATAFILE Where Drive = 'D:' And Path Like '%HOME' And Extension <> 'TMP' or something to that effect.

How many foldersfiles are we talking about? A couple of GB or TB? If you need help with some code examples, let me know which method you would like to see and I'll post something for you.

User avatar
jhicks
Posts: 1789
Joined: Sun Jan 21, 2007 11:31 pm

DateLastAccessed Help

Post by jhicks »

Are all the devices running a Windows OS? Can you limit your search to say all files modified in the last 180 days or NOT modified in the last 180 days? If so, using WMI and CIM_FILE you could churn out the data. It will take some time and put a bit of a load on the server but that should work. The other way is to use the FSO and enumerate, but this will generate a lot of network traffic, unless you can run the script locally. Actually you could write the script to execute locally then use a tool like PSEXEC to run it on remote computers. The other thing to consider is what are you going to do with all of this information? Does it need to be saved somewhere?Is Windows PowerShell a possibility here? It would make some of this much easier to script.

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

DateLastAccessed Help

Post by jvierra »

The above code works the same on the file system or on a share. The FSO can take a UNC and use it just fine. hostnamesharename

User avatar
rasimmer
Posts: 182
Joined: Fri Jan 30, 2009 12:37 am

DateLastAccessed Help

Post by rasimmer »

Ok...so my script is the rusted Yugo and JHicks has upped me with a 09 Corvette Z06...but really...which would you rather drive. Think of the insurance....

You could take that script and wrap in a HTA and just browse to directories too and write everything to Excel.Application formatted...get the Mercedes SL500 script going...tooo... :o)

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

DateLastAccessed Help

Post by new_user »

I guess I am looking for something simple yet repeatable like the code below but my issue also is how can I have this run against many directories wikth the c:mine variety or unc?

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

GetFolderContent "c:mine"

Sub GetFolderContent(strFolderName) 'On Error Resume Next Dim objFile If objFSO.FolderExists(strFolderName) Then Set objFolder = objFSO.GetFolder(strFolderName) Set colSubfolders = objFolder.Subfolders 'MsgBox objFolder.Files.Count For Each objFile in objFolder.Files wscript.echo objFile.Name & vbTab & objFile.DateLastAccessed & vbTab & objFile.Path 'objFSO.DeleteFile(objFile), True
Next For Each objSubfolder in colSubfolders GetFolderContent objSubfolder.Path Next End If On Error GoTo 0End Sub

User avatar
new_user
Posts: 157
Joined: Wed May 30, 2007 7:01 am

DateLastAccessed Help

Post by new_user »

Hmmm. how would it look it I wanted to do > 1 share? Sorry not familiar with this function

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

DateLastAccessed Help

Post by jvierra »

The share information does not maintain lastaccessed times or anyting else except number of connections, current connections and other aggregate statistics.

Look at the SHARE_INFO_* structures that are returned from the API call that is implemented by Win32_Share.

Code: Select all

	
typedef struct _SHARE_INFO_2 {
  LPWSTR shi2_netname;
  DWORD  shi2_type;
  LPWSTR shi2_remark;
  DWORD  shi2_permissions;
  DWORD  shi2_max_uses;
  DWORD  shi2_current_uses;
  LPWSTR shi2_path;
  LPWSTR shi2_passwd;
} SHARE_INFO_2

If you are looking for the file info for the files on teh share then you don't need to use WMI. Just open the share directly with the FSO.

Set folder = fso.GetFolder("computershare")

Or if you are just inspecting share:

Set folder = fso.GetFolder("" & objShare.__SERVER & "" & objShare.Name)


jvierra2009-03-23 13:12:43

Locked