Script is modified by me as per my requirement. It is parameter based script. Example is given below.
cscript folder.vbs "C:\Program Files" 1GB
Above execution will search folder larger than 1 GB in Program Files along with owner.
--------------------------------
Path = WScript.Arguments.Item(0)
Data1 = WScript.Arguments.Item(1)
Format = UCase(Right(Data1,2))
Data = Left(Data1,Len(Data1)-2)
Set objWMIService = GetObject("winmgmts:")
If Format <> "GB" And Format <> "MB" Then
MsgBox "Data Format Error!!! Please correct"
WScript.Quit
End If
Dim fso
Dim ObjOutFile
'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")
'Create an output file
Set ObjOutFile = fso.CreateTextFile("c:\Output.csv")
'Writing CSV headers
If Format = "GB" Then
ObjOutFile.WriteLine("Type,Folder Name,Path,Size(GB), Owner")
Else
ObjOutFile.WriteLine("Type,Folder Name,Path,Size(MB), Owner")
End If
'Call the GetFile function to get all files
GetFiles(Path)
'Close the output file ObjOutFile.Close
WScript.Echo("Completed")
Function GetFiles(FolderName)
On Error Resume Next
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Set ObjFolder = fso.GetFolder(FolderName)
'Getting all subfolders
Set ObjSubFolders = ObjFolder.SubFolders
For Each ObjFolder In ObjSubFolders
'Writing SubFolder Name and Path
If Format = "GB" Then
Size=Round(((ObjFolder.Size)/(1024*1024*1024)),2)
End If
If Format = "MB" Then
Size=Round(((ObjFolder.Size)/(1024*1024)),2)
End If
If Size > Int(data) Then
strFolderName = ObjFolder
WScript.Echo strFolderName
Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFolderName & "'")
intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
WScript.Echo objSD.Owner.Name
If intRetVal = 0 Then
ObjOutFile.WriteLine(ObjFolder.Type & "," & ObjFolder.Name & "," & ObjFolder.Path & "," & Size & "," & objSD.Owner.Domain & "\" & objSD.Owner.Name )
Else
ObjOutFile.WriteLine(ObjFolder.Type & "," & ObjFolder.Name & "," & ObjFolder.Path & "," & Size & ", Unable to find")
End If
End If
'Getting all Files from subfolder
GetFiles(ObjFolder.Path)
Next
End Function
Data1 = WScript.Arguments.Item(1)
Format = UCase(Right(Data1,2))
Data = Left(Data1,Len(Data1)-2)
Set objWMIService = GetObject("winmgmts:")
If Format <> "GB" And Format <> "MB" Then
MsgBox "Data Format Error!!! Please correct"
WScript.Quit
End If
Dim fso
Dim ObjOutFile
'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")
'Create an output file
Set ObjOutFile = fso.CreateTextFile("c:\Output.csv")
'Writing CSV headers
If Format = "GB" Then
ObjOutFile.WriteLine("Type,Folder Name,Path,Size(GB), Owner")
Else
ObjOutFile.WriteLine("Type,Folder Name,Path,Size(MB), Owner")
End If
'Call the GetFile function to get all files
GetFiles(Path)
'Close the output file ObjOutFile.Close
WScript.Echo("Completed")
Function GetFiles(FolderName)
On Error Resume Next
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Set ObjFolder = fso.GetFolder(FolderName)
'Getting all subfolders
Set ObjSubFolders = ObjFolder.SubFolders
For Each ObjFolder In ObjSubFolders
'Writing SubFolder Name and Path
If Format = "GB" Then
Size=Round(((ObjFolder.Size)/(1024*1024*1024)),2)
End If
If Format = "MB" Then
Size=Round(((ObjFolder.Size)/(1024*1024)),2)
End If
If Size > Int(data) Then
strFolderName = ObjFolder
WScript.Echo strFolderName
Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFolderName & "'")
intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
WScript.Echo objSD.Owner.Name
If intRetVal = 0 Then
ObjOutFile.WriteLine(ObjFolder.Type & "," & ObjFolder.Name & "," & ObjFolder.Path & "," & Size & "," & objSD.Owner.Domain & "\" & objSD.Owner.Name )
Else
ObjOutFile.WriteLine(ObjFolder.Type & "," & ObjFolder.Name & "," & ObjFolder.Path & "," & Size & ", Unable to find")
End If
End If
'Getting all Files from subfolder
GetFiles(ObjFolder.Path)
Next
End Function
Tried running this script on various parameters but getting an error.. See below:
ReplyDeleteFirst Time:
C:\SHARE\Script for folders greater than 1GB>folder.vbs "C:\Users\suser\Downloads"
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\SHARE\Script for folders greater than 1GB\folder.vbs(2, 1) Microsoft VBScript
runtime error: Subscript out of range
Second Time:
C:\SHARE\Script for folders greater than 1GB>folder.vbs "C:\Program Files"
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\SHARE\Script for folders greater than 1GB\folder.vbs(2, 1) Microsoft VBScript
runtime error: Subscript out of range
Please suggest ..
Deleteyou were executing it without passing size of folders looking for.
Deletefolder.vbs "C:\Program Files" 1GB