Notification

Icon
Error

Script - File search on network computers - Network file search for certain file types on computers

Posted: Wednesday, July 8, 2015 1:54:55 PM(UTC)
Daniel.B

Daniel.B

Member Original PosterPosts: 1,150
6
Like
This script allows to perform a network file search on a specific hard-drives (optionally limited to a certain folder including subfolders). The result will be written into a text file. If the hard-disk of target computers isn't too big, it should take less than 10 minutes to get all results.

PLEASE NOTE: This script uses a standard DOS file search command. However, it might strongly influence the performance of target computers. This depends on the parameters you are using and other circumstances which we have no influence on.

Following parameters can be given to the script in step 3:
/drive: (i.e. /drive:d ) - drive to search on (default will be c)
/type: (i.e. /type:exe ) - file type to search for (without this parameter, it lists all files)
/path: (i.e. /path:"\Program Files (x86)\" ) - folder to search in (incl. subfolders)
/target: (i.e. /target:"C:\LS\filesearch_output.txt" ) - output file target
/bare: (i.e. /bare:true ) - output file in bare list format without details

Create a new file on your package share under folder Scripts, name it "filesearch_dos.vbs" and fill it in an editor with the following code:
Code:

Option Explicit
Const ForWriting = 2
Const ForAppending = 8
Dim objFSO 			'File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTS 			'Text Stream Object
Dim objWS 			'WScript object
Dim Drive			'Parameter: drive to search in
Dim Filetype		'Parameter: file type to search for
Dim objFile			'file found
Dim query 			'WMI query string
Dim searchstring 	'files to search for
Dim path 			'Parameter: path of files to be searched
Dim target 			'Parameter: file location of output file
Dim targetfolder	'Folder of the output file
Dim arg 			'script arguments
Dim drivesinfo		'search info string drives
Dim typesinfo		'search info string filetypes
Dim info			'search info text
Dim i				'counter
Dim starttime		'start time for duration

'Read script arguments
if wscript.arguments.Named.count > 0 then 
	if WScript.Arguments.Named("drive") <> "" Then                                     
		Drive = WScript.Arguments.Named("drive")                                                      
	End If

	if WScript.Arguments.Named("type") <> "" Then                                     
		Filetype = WScript.Arguments.Named("type")                                                    
	End If
	
	if WScript.Arguments.Named("path") <> "" Then                                     
		path = WScript.Arguments.Named("path")
		If Left(path,1) <> "\" Then path = "\" & path		
	End If
	
	if WScript.Arguments.Named("target") <> "" Then                                     
		target = WScript.Arguments.Named("target")                                                         
	End If
	
End if

'Build search query
If Len(Drive) > 0 Then
    searchstring = Drive & ":"
Else
	searchstring = "c:"
End if
If Len(path) > 0 Then
	searchstring = searchstring & path
	if Right(searchstring,1) <> "\" then searchstring = searchstring & "\"
Else 
	searchstring = searchstring & "\"
End if
If Len(Filetype) > 0 Then
    searchstring = searchstring & "*." & Filetype
Else
	searchstring = searchstring & "*.*"
End if

'Define scan result target location and open file
If Not Len(target)>0 Then target = "C:\temp\LSfilesearch.txt"
targetfolder = Mid(target,1,InStrRev(target,"\"))
If Not objFSO.FolderExists(targetfolder) Then objFSO.CreateFolder(targetfolder)
Set objTS = objFSO.OpenTextFile(target, ForWriting, True) 

'write file search parameters into result file
starttime = Now()
info = "Starting file search on drive "
If Len(drive)>0 Then info = info & drive & ":" else info = info & "c:"
If Len(Filetype)>0 Then info = info & " for file type " & Filetype
If Len(path)>0 then info = info & " under path """ & path & """"
objTS.WriteLine(info & vbCrLf & "---")
objTS.Close()

'Execute search
query = """%comspec%"" /e:4096 /s /c dir /s "
if LCase(WScript.Arguments.Named("bare")) = "true" Then query = query & "/b"
query = query & "/o """ & searchstring & """ >> " & target
Set objWS = CreateObject("Wscript.Shell")
objWS.Run query, 0, True

Set objTS = objFSO.OpenTextFile(target, ForAppending, False) 
objTS.WriteLine("---" & vbCrLf & "Search completed in " & _
	Datediff("s",starttime,now()) & " seconds")
objTS.Close()
WScript.Quit 0
Script - DOS file searchDownload Package
DescriptionExecutes a file search on a local hard-drive

Parameters (with examples):
======================
/drive:c - drive to search on
/type:exe - file type to search for
/path:"\Program Files (x86)\" - folder to search in (incl. subfolders)
/target:"\\Servername\fileshare$\output.txt" - output file target
/bare:true - output file in bare list format
Final ActionNothing
Max. Duration10 min(s), 0 hour(s)
RescanNo
Steps
1. Check if script file is accessible
TypeCondition
SuccessGo To Next
FailureStop (Failure)
Conditions
File {PackageShare}\Scripts\ filesearch_dos.vbs Exists
2. Copy script to local HD
TypeCommand
Return Codes 0,1641,3010
SuccessGo To Next
FailureStop (Failure)
Command xcopy "{PackageShare}\Scripts\filesearch_dos.vbs" "C:\temp\" /q /y
3. Start file search
TypeScript
Return Codes 0,1641,3010
SuccessStop (Success)
FailureStop (Failure)
Command "C:\temp\filesearch_dos.vbs" /type:exe /path:"\Program Files (x86)\" /target:"c:\LS\filesearch_result.txt"
leblanc.daniel.4@hydro.qc.ca
posted: 7/28/2015 6:27:21 PM(UTC)
Where do we put this file and how to apply on Lansweeper server??

DanBrick wall
Susan.A
#2Susan.A Member Administration Posts: 1,535  
posted: 8/1/2015 12:48:41 PM(UTC)
Originally Posted by: leblanc.daniel.4@hydro.qc.ca Go to Quoted Post
Where do we put this file and how to apply on Lansweeper server??

DanBrick wall

The search results are written to a file. There is no built-in functionality for adding this information to the Lansweeper database, if this is what you're asking. You would have to add tables and write database scripts for this, which we cannot provide instructions or support for. Any changes you make to the database may cause issues and/or be overwritten during updates.
MiniWalks
#3MiniWalks Member Posts: 4  
posted: 8/11/2015 2:23:08 AM(UTC)
Looks like this could be a winner :)

Is there any way to use the computer name as a variable in the output file name?

ie
Target:\\NAS\Share\%PC_NAME%.SearchResult.txt ?

Cheers,
Daniel.B
#4Daniel.B Member Original PosterPosts: 1,150  
posted: 8/11/2015 8:39:49 AM(UTC)
For this you need to modify the script. At the top, add the following lines:
Code:
Dim objShell
Dim objShellEnvironment
Dim computername	'name of the computer the script is executed on
set objShell    = WScript.CreateObject("WScript.Shell")
set objShellEnvironment = objShell.Environment("Process")
computerName  = objShellEnvironment("ComputerName")


Then after these lines in the existing script
Code:
'Define scan result target location and open file
If Not Len(target)>0 Then target = "C:\temp\LSfilesearch.txt"

add the following line:
Code:

target = Left(target, Len(target)-4) & "_" & computername & ".txt"
mickeyshowers
#5mickeyshowers Member Posts: 34  
posted: 10/2/2015 11:00:34 PM(UTC)
The additions to the script produce an error on this line for me.

set objShellEnvironment = objShell.Environment("Process")

Variable is undefined: 'objShellEnvironment'
Daniel.B
#6Daniel.B Member Original PosterPosts: 1,150  
posted: 10/5/2015 12:34:13 PM(UTC)
@mickeyshowers: Thanks for testing it. Applause There were two mistakes. I corrected my last post. With this modification it is working as expected.

Keep in mind that if you are using a network share (anything other than {PackageShare}) as target, you need to deploy the package under a user account which has write access to that share.
mickeyshowers
#7mickeyshowers Member Posts: 34  
posted: 10/6/2015 9:19:09 PM(UTC)
It works! Thanks!
IrenaK
#8IrenaK Member Posts: 2  
posted: 10/29/2015 12:51:49 PM(UTC)
Hi ,Daniel,

could you edit this script with possibility to exclude some directory.

Thanks Angel
Daniel.B
#9Daniel.B Member Original PosterPosts: 1,150  
posted: 10/30/2015 6:20:29 PM(UTC)
Filtering out specific folders during the search is not possible. The only thing you can do is filtering out parts of the search result. The script attached to this post allows an additional parameter which lets you define text you don't want to see in your result. You can change the command in step 3 of the package to something like

/type:"exe" /target:"c:\LS\filesearch_result.txt" /exclude:"Windows\"

Note: You need to change the file type of the attached file from txt to vbs, or just copy the code to your existing script.
Handles attachments
filesearch_dos.txt (4kb) downloaded 137 time(s).
IrenaK
#10IrenaK Member Posts: 2  
posted: 11/2/2015 5:05:41 PM(UTC)
Thank you , but works correctly if - /exclude: "Windows\.*"
pryzhkov
#11pryzhkov Member Posts: 1  
posted: 3/18/2016 3:05:56 PM(UTC)
Hi, Daniel!
While using this script i was facing such issue: generated report contains unreadable characters, like next:
Quote:
Starting file search on drive d: for file type pst
---
Volume in drive D is New Volume
Volume Serial Number is ***********

Directory of d:\

16.03.2016 18:28 4я082 test.pst
1 File(s) 4я082 bytes

Directory of d:\$RECYCLE.BIN\S-1-5-21-2849041822-3509271814-4006248400-4254

17.03.2016 15:32 544 $IFQZIJV.pst
17.03.2016 15:08 0 $RFQZIJV.pst
2 File(s) 544 bytes

Directory of d:\pst_search

16.03.2016 18:28 4я082 test.pst
1 File(s) 4я082 bytes

Total Files Listed:
4 File(s) 8я708 bytes
0 Dir(s) 107я247я927я296 bytes free
---
Search completed in 0 seconds


what's the problem, and how can i fix it?
Thank you for your script!

Active Discussions

Lansweeper Looking for a report
by  RC62N  
Go to last post Go to first unread
Last post: Yesterday at 6:59:56 PM(UTC)
Lansweeper Assistance on Building a Report on Java Installations
by  Rob-CD   Go to last post Go to first unread
Last post: Yesterday at 8:56:30 AM(UTC)
Lansweeper Report: Win XS C drive
by  Esben.D  
Go to last post Go to first unread
Last post: 10/18/2019 12:44:38 PM(UTC)
Lansweeper Notebooks report
by  Esben.D   Go to last post Go to first unread
Last post: 10/18/2019 12:40:31 PM(UTC)
Lansweeper Report that shows two pieces of software
by  Jeyabal  
Go to last post Go to first unread
Last post: 10/18/2019 1:14:52 AM(UTC)
Lansweeper Exchange Mailbox Usage
by  nhouse24   Go to last post Go to first unread
Last post: 10/17/2019 10:31:09 PM(UTC)
Lansweeper Unauthorized Administrators by OU
by  Craig Fair  
Go to last post Go to first unread
Last post: 10/16/2019 8:05:04 PM(UTC)