Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Create a report file of all the shared folders in a machine with each extension as header and the count of objects in it.

Hi,

Create a report file of all the shared folders in a machine with each extension as header and the count of objects in it.
Any way a script can create a report of the shared folders.
like
jpeg   c:\foldername\   45 files
gif    d:\mpf                60 files

According to folders and extension types i need the report.

Regards
Sharath
Avatar of ahoffmann
ahoffmann
Flag of Germany image

is following sufficient (where your expected result is last summary line):

for /f "tokens=1,2,3,4" %a in ('dir /s *.gif') do @if %d==Bytes echo gif  %a %b
Avatar of bsharath

ASKER

Thanks for the response...
I am not getting any results when run...

I should be able to get results of any extension.All extension totals has to be mentioned..
> I am not getting any results ..
my cristall ball does not show me your version/language of cmd.exe etc. But I assume that you have to replace   Bytes   by the correct word you get when you do a simpl dir in the line tellung you how much files there're and how many bytes, probaly just replace it by   bytes
Also you have to run that command after doing something like
C:
cd mpf


> to get results of any extension
first I want to know if the provided for command returns the result you're interested in, then we can improve it for example to reduce it to just the line you need, and then I'll ask you what you mean by "any extension" and all extensions" (which I can assume again, buts it's very vage too:)


Avatar of RobSampson
Sharath, if you try this script on a smallish test folder, it will count the amount of each extension in each folder, and output to CSV:

'=================
Const FOLDER_PATH = "C:\Temp\Temp"
Const OUTPUT_FILE = "FileExtensionCount.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")

strResults = """Folder"",""File Extension"",""Amount"""

Set objFileTypes = CreateObject("Scripting.Dictionary")
      
For Each objFile In objFSO.GetFolder(FOLDER_PATH).Files
      strExtension = LCase(objFSO.GetExtensionName(objFile.Path))
      If objFileTypes.Exists(FOLDER_PATH & ";" & strExtension) Then
            objFileTypes(FOLDER_PATH & ";" & strExtension) = Int(objFileTypes(FOLDER_PATH & ";" & strExtension)) + Int(1)
      Else
            objFileTypes.Add FOLDER_PATH & ";" & strExtension, Int(1)
      End If
Next
For Each objSubFolder In objFSO.GetFolder(FOLDER_PATH).SubFolders
      Recurse_SubFolder objSubFolder
Next
      
For Each strPathExt in objFileTypes
      strPath = Split(strPathExt, ";")(0)
      strExt = Split(strPathExt, ";")(1)
      'WScript.Echo strExtension & " - " & objFileTypes(strExtension)
      strResults = strResults & VbCrLf & """" & strPath & """,""" & strExt & """,""" & Int(objFileTypes(strPathExt)) & """"
Next
Set objFileTypes = Nothing

Set objOutputFile = objFSO.CreateTextFile(OUTPUT_FILE, True)
objOutputFile.Write strResults
objOutputFile.Close
Set objOutputFile = Nothing

Set objFolder = Nothing
Set objFSO = Nothing

MsgBox "Done. Please see " & OUTPUT_FILE

' RECURSE SUB ROUTINE
Sub Recurse_SubFolder(objFolder)
      For Each objFile In objFolder.Files
            strExtension = LCase(objFSO.GetExtensionName(objFile.Path))
            If objFileTypes.Exists(objFolder.Path & ";" & strExtension) Then
                  objFileTypes(objFolder.Path & ";" & strExtension) = Int(objFileTypes(objFolder.Path & ";" & strExtension)) + Int(1)
            Else
                  objFileTypes.Add objFolder.Path & ";" & strExtension, Int(1)
            End If
      Next
      For Each objSubFolder In objFolder.SubFolders
            Recurse_SubFolder objSubFolder
      Next
End Sub
'=================

Regards,

Rob.
Excellent Rob this is exactly what i wanted.But any way it can scan just the shared folders instead of me providing each path
There should be.  Will you be running this on one computer with shared folders, or against a few computers?

Rob.
Its going to be on a remote machines shared folders.If possible then multiple machines is helpful..
OK, try this.  It is going to create one CSV file per shared folder, that does NOT end with a $ character.  Otherwise that would include C$ and IPC$ shares, which would check the entire drive.

This could take a VERY long time, so try it with servers with very few shares first, in Servers_With_Shares.txt

'====================
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const strInputFile = "Servers_With_Shares.txt"

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
While Not objInputFile.AtEndOfStream
      strServer = objInputFile.ReadLine
      Set objWMIService = GetObject("winmgmts:\\" & strServer & "\root\cimv2")
      Set colItems = objWMIService.ExecQuery _
          ("Select * From Win32_Share")
      For Each objItem in colItems
            If objItem.Type = 0 And Right(objItem.Name, 1) <> "$" Then
                strPath = "\\" & strServer & "\" & Replace(objItem.Path, ":", "$")
                strFileName = strServer & "_" & objItem.Name & "_Extension_List.csv"

                  strResults = """Folder"",""File Extension"",""Amount"""
                  Set objFileTypes = CreateObject("Scripting.Dictionary")
                Count_Extensions strPath

                  For Each strPathExt in objFileTypes
                        strPath = Split(strPathExt, ";")(0)
                        strExt = Split(strPathExt, ";")(1)
                        'WScript.Echo strExtension & " - " & objFileTypes(strExtension)
                        strResults = strResults & VbCrLf & """" & strPath & """,""" & strExt & """,""" & Int(objFileTypes(strPathExt)) & """"
                  Next

                  Set objFileTypes = Nothing
                  Set objOutputFile = objFSO.CreateTextFile(strFileName, True)
                  objOutputFile.Write strResults
                  objOutputFile.Close
                  Set objOutputFile = Nothing
                  
                  Set objFolder = Nothing
                  
                  'MsgBox "Done. Please see " & strFileName
          End If
      Next
Wend
objInputFile.Close
MsgBox "Script has finished going through servers."
Set objFSO = Nothing
WScript.Quit

Sub Count_Extensions(FOLDER_PATH)
      For Each objFile In objFSO.GetFolder(FOLDER_PATH).Files
            strExtension = LCase(objFSO.GetExtensionName(objFile.Path))
            If objFileTypes.Exists(FOLDER_PATH & ";" & strExtension) Then
                  objFileTypes(FOLDER_PATH & ";" & strExtension) = Int(objFileTypes(FOLDER_PATH & ";" & strExtension)) + Int(1)
            Else
                  objFileTypes.Add FOLDER_PATH & ";" & strExtension, Int(1)
            End If
      Next
      For Each objSubFolder In objFSO.GetFolder(FOLDER_PATH).SubFolders
            Recurse_SubFolder objSubFolder
      Next
End Sub

' RECURSE SUB ROUTINE
Sub Recurse_SubFolder(objFolder)
      For Each objFile In objFolder.Files
            strExtension = LCase(objFSO.GetExtensionName(objFile.Path))
            If objFileTypes.Exists(objFolder.Path & ";" & strExtension) Then
                  objFileTypes(objFolder.Path & ";" & strExtension) = Int(objFileTypes(objFolder.Path & ";" & strExtension)) + Int(1)
            Else
                  objFileTypes.Add objFolder.Path & ";" & strExtension, Int(1)
            End If
      Next
      For Each objSubFolder In objFolder.SubFolders
            Recurse_SubFolder objSubFolder
      Next
End Sub
'====================

Regards,

Rob.
I get this..
---------------------------
Windows Script Host
---------------------------
Script:      C:\Shares search extension.vbs
Line:      46
Char:      7
Error:      Path not found
Code:      800A004C
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
Change the Count_Extensions(FOLDER_PATH) to this:

Sub Count_Extensions(FOLDER_PATH)
      If objFSO.FolderExists(FOLDER_PATH) = True Then
      For Each objFile In objFSO.GetFolder(FOLDER_PATH).Files
            strExtension = LCase(objFSO.GetExtensionName(objFile.Path))
            If objFileTypes.Exists(FOLDER_PATH & ";" & strExtension) Then
                  objFileTypes(FOLDER_PATH & ";" & strExtension) = Int(objFileTypes(FOLDER_PATH & ";" & strExtension)) + Int(1)
            Else
                  objFileTypes.Add FOLDER_PATH & ";" & strExtension, Int(1)
            End If
      Next
      For Each objSubFolder In objFSO.GetFolder(FOLDER_PATH).SubFolders
            Recurse_SubFolder objSubFolder
      Next
      Else
            MsgBox "Could not find " & FOLDER_PATH
      End If
End Sub


Regards,

Rob.
I get this Rob..

---------------------------
Windows Script Host
---------------------------
Script:      C:\Shares search extension.vbs
Line:      56
Char:      13
Error:      Type mismatch: 'Recurse_SubFolder'
Code:      800A000D
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rob the csv is created but no data in it...

I get this...

---------------------------

---------------------------
Could not find \\dev-chen-mrd1\F$\Indlist old
---------------------------
OK  
---------------------------
Hmmmm....can you browse to a folder with that "exact" name?  Or is that the share name, rather than the actual folder name?

You should be able to click Start --> Run, type in
\\dev-chen-mrd1\F$\Indlist old

and display that folder.....

Aside from that though, it should be creating at least a few files, called
<<SERVERNAM>>_<<SHARENAME>>_Extension_List.csv

Regards,

Rob.
Rob
When i access this like this..
\\dev-chen-mrd1\F$\Indlist old
Does not work
If i access like this it opens..
\\dev-chen-mrd1\Indlist old

As i am running from a remote machine the F$ should not be accessed..
OK then, change this:
                strPath = "\\" & strServer & "\" & Replace(objItem.Path, ":", "$")
to this:
                strPath = "\\" & strServer & "\" & objItem.Name


Regards,

Rob.
Sorry for the delay i tested it on a big share..
Works great Rob...
Thanks Rob...
No problem.  Yeah, it takes a while!

Rob.