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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vbscript to find file from a folder on remote PCs & display result

Status
Not open for further replies.

5564

Technical User
May 8, 2012
2
GB
Hi Team,

I found this scrit below that search for files by their extention and save the result to html file.T Excelent, that's what I'd like and looking for to use.
Is it possible to modify it to meet what w want to acomplesh as below:
* I whant the script to search specific file name ie. "123.XML" from a location like "%username%\Application Data\ADOBE\2345" using a text file with a list of network PCs and display result with the path.
Your help is appreciated. ta

THIS IS THE vbscript IFOUND IN THIS FURUM:
'*******************************************************************************************************************'
Description in English:'It is a Vbscript to search for files by their extensions'and generate search results in a Table in HTML with quote information'on these files such that their paths, creation date, modification date, size and attributes'and display thumbnails of image types diffrent when searching'Eg if the search includes image files will be displayed as thumbnails'*****************************************************************************************************************

Code:
Dim fso, OutFile, sDrv, sFName, sReport, sFile, sTitle ,strHTML
sTitle = "Recherche des Fichiers Par leurs Extensions © Hackoo"
Set fso = CreateObject("Scripting.FileSystemObject")
OutFile = "Recherche.html"
If fso.FileExists(OutFile) Then fso.DeleteFile(OutFile)

Set sReport = fso.OpenTextFile(OutFile, 8, True)
sDrv = InputBox("Entrez la lettre du lecteur à la recherche (lettre seulement)" & vbcrlf&_
"ou bien " & vbcrlf & "(Saisissez * pour rechercher dans toutes les lettres de lecteur local)", sTitle)
If sDrv = "" Then WScript.Quit

sFName = InputBox ("Entrez l'extension du fichier à rechercher exemple JPG ou bien GIF ou bien DOC ou bien XLS etc ....", sTitle)
If sFName = "" Then WScript.Quit

strHTML="<html><body text=white bgcolor=#1234568><style type='text/css'>"&_
"a:link {color: #F19105;}"&_
"a:visited {color: #F19105;}"&_
"a:active {color: #F19105;}"&_
"a:hover {color: #FF9900;background-color: rgb(255, 255, 255);}"&_
"</style>"

strHTML=strHTML &"<center><h2><B> <font color=Red>[COUNT] </font>Fichiers Trouvés dont l'extension est <font color=red>"""& sFName &""" </font> sur le lecteur <font color=red>"& UCase(sDrv) & ":</B></font></h2></center>"&_
"<center><table border='3' cellpadding='1' style='border-collapse: collapse; font size:11pt' bordercolor='#CCCCCC' width='100%' id='Table1'></center>" & _
"<td><center><strong>Chemin</strong></center></td>"&_
"<td><center><strong>Date de Création</strong></center></td>"& _
"<td><center><strong>Date de Modification</strong></center></td>"&_
"<td><center><strong>Taille</strong></center></td>"&_
"<td><center><strong>Attributs</strong></center></td>"

If sDrv = "*" Then
Dim d,dc,racine
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
racine = d.Driveletter & ":"
If d.IsReady Then
GetResults racine , sFName
End If
Next
Else
GetResults sDrv & ":", sFName
End If
sReport.WriteLine strHTML &"</table></body></html>"
Wscript.CreateObject("WScript.Shell").Run OutFile

Sub GetResults(drv, fname)
On Error Resume Next
Dim sWQL, oFile, sAttrib,sFilePath,size
ext = Array("png","jpg","jpeg","gif","bmp","psd","tif")
sWQL = "select * from cim_datafile where Drive='" & _
drv & "' AND Extension = '" & fname & "'"
Results = 0
For Each oFile In GetObject("winmgmts:").execquery(sWQL)
Results = Results + 1
sFile = oFile.Name
Set f = fso.GetFile(sFile)

SizeKo = Round(FormatNumber(f.Size)/(1024),0) & " Ko" 'Taille en Ko
SizeMo = Round(FormatNumber(f.Size)/(1048576),0) & " Mo" 'Taille en Mo
SizeGo = Round(FormatNumber(f.Size)/(1073741824),0) & " Go" 'Taille en Go

If f.size < 1024 Then
Size = f.size & " Octets"
elseif f.size < 1048576 Then
Size = SizeKo
elseif f.size < 1073741824 Then
Size = SizeMo
else
Size = SizeGo
end if
sFilePath = f.Path
If oFile.Archive Then sAttrib = "Archive "
If oFile.Compressed Then sAttrib = sAttrib & " Compressé "
If oFile.Encrypted Then sAttrib = sAttrib & " Crypté "
If oFile.Hidden Then sAttrib = sAttrib & " Caché "
If oFile.System Then sAttrib = sAttrib & " Système "
If oFile.Readable Then sAttrib = sAttrib & " Lecture "
If oFile.Writeable Then sAttrib = sAttrib & " Ecriture "

If UCase(ext(0)) = UCase(fso.GetExtensionName(oFile.Name)) or UCase(ext(1)) = UCase(fso.GetExtensionName(oFile.Name))or UCase(ext(2)) = UCase(fso.GetExtensionName(oFile.Name)) or UCase(ext(3)) = UCase(fso.GetExtensionName(oFile.Name)) or UCase(ext(4)) = UCase(fso.GetExtensionName(oFile.Name)) or UCase(ext(5)) = UCase(fso.GetExtensionName(oFile.Name)) or UCase(ext(6)) = UCase(fso.GetExtensionName(oFile.Name)) Then

ImgFileName = oFile.Name
strHTML=strHTML & "<tr><td><center><a target=_Blank href='"& sFilePath &"'>"&ImgFileName&"<br><img src='"& sFilePath &"' border=1 height=50 width=80></center></td><td><center>" & f.DateCreated & "</center></td>" & _
"<td><center>" & f.DateLastModified & "</center></td><td><center>"& Size & "</center></td>"&_
"<td><center>" & sAttrib & "</center></td></tr>"
else
strHTML=strHTML & "<tr><td><a target=_Blank href='" & sFilePath & "'>" & _
sFilePath & "</a></td><td><center>" & f.DateCreated & "</center></td>" & _
"<td><center>" & f.DateLastModified & "</center></td><td><center>"& Size & "</center></td>"&_
"<td><center>" & sAttrib & "</center></td></tr>"
end if
Next
strHTML = Replace(strHTML, "[COUNT]", Results)
End Sub
 
Yes, it is possible...

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
here's a script I assembled with just the meat of what you are looking for. Cut it up and modify the script as you see fit.

Code:
'************************************************************************
'DESCRIPTION:   Search for file on remote machine
'WRITTEN BY:    Daniel M. Jones	(Geates)
'DATE:		May 9, 2012
'UPDATE:
'************************************************************************

'************************************************************************
' VARIABLE DEFINITION
'************************************************************************

set objFSO   = WScript.CreateObject("Scripting.FileSystemObject")
set objShell = WScript.CreateObject("WScript.Shell")

'************************************************************************
' FUNCTIONS
'************************************************************************
'Ping ping a computer
function ping (strComputer)
	ping = false
	set objExec = objShell.Exec("%comspec% /c ping " & strComputer & " -n 1 -w 100")

	do until objExec.Stdout.AtEndOfStream
		strLine = objExec.StdOut.ReadLine
		if (inStr(strLine, "Reply")) then ping = true
	loop
end function

'Recursively search a strDir for strQuery
function searchDir(strDir, strQuery)
	set objFolder = objFSO.GetFolder(strDir)
	for each objSuolder in objFolder.SubFolders
		strResults = strResults & searchDir(objSubFolder.Path, strQuery)
	next
	
	for each objFile in objFolder.Files
		if (inStr(objFile.Name, strQuery)) then strResults = strResults & objFile.Path & vbNewLine
	next

	searchDir = strResults
end function

'************************************************************************
' BEGIN
'************************************************************************
			
'Open a log file for writing
set objLog = objFSO.OpenTextFile("log.txt", 2, true, 0)

'Open the list of computers and read the contents into an array
set objStream = objFSO.OpenTextFile("computers.txt", 1, true, 0)
arrComputers = split(objStream.ReadAll, vbNewLine)

'Traverse array
for i = 0 to ubound(arrComputers)
	strComputer = arrComputers(i)
	if (strComputer <> "") then
		'Ping the machine to see if it's on
		if (ping(strComputer)) then
			strDir = "\\" & strComputer & "\c$\temp\"
			strFiles = searchDir(strDir, ".txt")
			objLog.Write strFiles
		end if
	end if
next

'Close log file and open in notepad
objLog.Close
objShell.Run "notepad log.txt"

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Sorry for late reply.
I tested Geate's script (Thank you so much for that)and it works, but it is finding all the files with .txt extension, as I'm searching for a specific file, if I put the file name it does not find it.
Tried to do some changes but not avail.
 
Then the file does not exist. Specifying a filename works for me.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top