Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script needed to count all Word files stored in users home directories.

Status
Not open for further replies.

mdk1981

Technical User
Dec 30, 2013
1
0
0
GB
I am trying to put together a script that would query a particular OU within my organisation and achieve the following;

1. List all users within the OU.

2. Query Job Tiltles of those users.

3. Count all .doc or .docx files within each users home directory within the OU.

4. Export all information into an Excel document.

So far, I have the compiled the vbscript below to count all files within each home directory in the OU. Although this doesn't seem to reflect the actual word document count.

The sub-folders go quite deep, and I'm not sure this is drilling down through the entire tree.

Any help would be greatly appreciated.

------------------
Set FSO = CreateObject("scripting.filesystemobject")
Set objContainer = GetObject("LDAP://OU=MyOU,OU=User Accounts,DC=MyDomain,DC=co,DC=uk")

For Each M in objContainer
If M.homeDirectory <> "" Then
FileCnt = 0
ReturnFileCountUsingFSO M.homeDirectory, FileCnt
WScript.echo M.cn & " ; " & M.Title & " ; " & FileCnt & " Word documents on home directory"
End If
Next
Set objGroup = Nothing

Function ReturnFileCountUsingFSO(strPath, FileCnt)
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
Set fld = FSO.GetFolder(strPath)

FileCnt = FileCnt + fld.Files.Count
For Each f In fld.SubFolders
If fso.GetExtensionName(objFile.Path) = "doc" Or fso.GetExtensionName(objFile.Path) = "docx" then
Count = Count + 1
END IF

Next
Set f = Nothing
Set fld = Nothing
Set FSO = Nothing
End Function
--------------------------------------------------


Any help would be greatly appreciated.
 
First, take away the [tt]on error resume next[/tt] because that suppresses all errors, making it very hard to troubleshoot. It makes you think something is working when really it's not working at all! Secondly, run through your function manually. Is it doing anything your want it to? Use recursion (a routine that calls itself) to traverse the folder structure.

A simple folder structure recursive function
Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
function countFiles (strPath)
   'get the object folder
   set objFolder = objFSO.GetFolder(strPath)

   'loop through the sub folders
   for each objSubFolder in objFolder.SubFolders
      intCount = intCount + countFiles(objSubFolder.Path)
   next

   'loop through the files in the folder
   for each objFile in objFolder.Files
      intCount = intCount + 1
   next

   'return file count
   countFiles = intCount
end function

intFiles = countFiles("c:\temp")
msgbox "the folder c:\temp has " & intFiles & " file(s)"

-Geates


 
Since you want the data to end up in an Excel workbook, why not run a macro (VBA) from there and populate the workbook directly?

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top