Script to list larger size folder with owner detail

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
EndIf
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")
EndIf

'Call the GetFile function to get all files
GetFiles(Path)

'Close the output file ObjOutFile.Close
WScript.Echo("Completed")

Function GetFiles(FolderName)
OnErrorResumeNext
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Set ObjFolder = fso.GetFolder(FolderName)
'Getting all subfolders
Set ObjSubFolders = ObjFolder.SubFolders

ForEach ObjFolderIn ObjSubFolders
'Writing SubFolder Name and Path
If Format = "GB"Then
    Size=Round(((ObjFolder.Size)/(1024*1024*1024)),2)
EndIf
If Format = "MB"Then
    Size=Round(((ObjFolder.Size)/(1024*1024)),2)
EndIf
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 = 0Then
   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")
EndIf
EndIf
'Getting all Files from subfolder
GetFiles(ObjFolder.Path)
Next
EndFunction

Script to list larger files with owner detail

Script is modified by me as per my requirement. It is parameter based script. Example is given below.

cscript file.vbs "C:\Program Files" 1GB


Above execution will search file 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
EndIf
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,File Name,File Path,Size(GB), Owner")
Else
ObjOutFile.WriteLine("Type,File Name,File Path,Size(MB), Owner")
EndIf

'Call the GetFile function to get all files
GetFiles(Path)

'Close the output file ObjOutFile.Close
WScript.Echo("Completed")

Function GetFiles(FolderName)
OnErrorResumeNext
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim ObjFiles
Dim ObjFile
Set ObjFolder = fso.GetFolder(FolderName)
Set ObjFiles = ObjFolder.Files

'Write all files to output files
ForEach ObjFileIn ObjFiles

If Format = "GB"Then
    Size=Round(((ObjFile.Size)/(1024*1024*1024)),2)
EndIf
If Format = "MB"Then
    Size=Round(((ObjFile.Size)/(1024*1024)),2)
EndIf
If Size > Int(data) Then
strFileName = ObjFolder & "\" & ObjFile.Name
WScript.Echo strFileName
Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strFileName & "'")
intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
WScript.Echo objSD.Owner.Name
If intRetVal = 0Then
   ObjOutFile.WriteLine(ObjFile.Type & "," & ObjFile.Name & "," & ObjFile.Path & "," & Size & "," & objSD.Owner.Domain & "\" & objSD.Owner.Name )
Else
   ObjOutFile.WriteLine(ObjFile.Type & "," & ObjFile.Name & "," & ObjFile.Path & "," & Size & ", Unable to find")
EndIf
EndIf
Next

'Getting all subfolders
Set ObjSubFolders = ObjFolder.SubFolders

ForEach ObjFolderIn ObjSubFolders
GetFiles(ObjFolder.Path)
Next
EndFunction

Script to find files and save in CSV format



This is a parameter based script. Syntaxes are given below.



cscript filesearch.vbs "path needs to search" size(MB or GB)

Example:
Below command will search list of file which are larger than 1 GB in "C:\Program Files" Path and stored in C:\Output.csv

cscript filesearch.vbs "c:\program files" 1GB

--------------------------------
Path
 = WScript.Arguments.Item(0)
Data1 = WScript.Arguments.Item(1)
Format = UCase(Right(Data1,2))
Data = Left(Data1,Len(Data1)-2)

If Format <> "GB"And Format <> "MB"Then
MsgBox"Data Format Error!!! Please correct"
WScript.Quit
EndIf
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,File Name,File Path,Size(GB)")
Else
ObjOutFile.WriteLine("Type,File Name,File Path,Size(MB)")
EndIf

'Call the GetFile function to get all files
GetFiles(Path)

'Close the output file ObjOutFile.Close
WScript.Echo("Completed")

Function GetFiles(FolderName)
OnErrorResumeNext
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim ObjFiles
Dim ObjFile
Set ObjFolder = fso.GetFolder(FolderName)
Set ObjFiles = ObjFolder.Files

'Write all files to output files
ForEach ObjFileIn ObjFiles

If Format = "GB"Then
    Size=Round(((ObjFile.Size)/(1024*1024*1024)),2)
EndIf
If Format = "MB"Then
    Size=Round(((ObjFile.Size)/(1024*1024)),2)
EndIf
WScript.Echo FolderName
If Size > Int(data) Then
ObjOutFile.WriteLine(ObjFile.Type & "," & ObjFile.Name & "," & ObjFile.Path & "," & Size)
EndIf
Next

'Getting all subfolders
Set ObjSubFolders = ObjFolder.SubFolders

ForEach ObjFolderIn ObjSubFolders
GetFiles(ObjFolder.Path)
Next


EndFunction

Note: Script is only modified by me as per my requirement.