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

finding files

Status
Not open for further replies.

captedgar

Programmer
Dec 4, 2009
20
0
0
GB
Hi there

I'm a newbie to programming but very much willing to work hard and try. We have about 100 servers and on each servers we have the g drive and e drive. early this year there were some temp files dumped all over the 100 servers under variuos folders and subfolders so i have been assigned a task to clean this up. Any chance someone can guide me in getting an automated script where all i need to do is enter the list of servers and the file extension to search and then script would scan for the file extension on all 100 servers and each of the drives associated with it.



so far i have the following script



Rem Script created: 01.10.2009 22:24
Rem Author: captedgar

strDir = "c:\"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objDir = FSO.GetFolder(strDir)
getInfo(objDir)
Sub getInfo(pCurrentDir)

For Each aItem In pCurrentDir.Files
If LCase(Right(CStr(aItem.Name), 3)) = "tmp" Then
'wscript.Echo aItem.Name 'all c:\*.tmp
listed
End If
Next
For Each aItem In pCurrentDir.SubFolders
getInfo(aItem) 'passing recursively
Next

End Sub

regards
 
- Readin list of servers
- Connect to server
- Collect server disks
- scan disks for files


Code:
'************************************************************************
'DESCRIPTION:   Search server disks for a file(s)														
'WRITTEN BY:    Daniel M. Jones											
'DATE:			December 7, 2009											
'UPDATE:																
'************************************************************************

'on error resume next

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

dim strExt

strExt = ".txt"

set objArgs  = Wscript.Arguments
set objFSO   = WScript.CreateObject("Scripting.FileSystemObject")
set objReg   = GetObject("winmgmts:\\.\root\default:StdRegProv")
set objShell = WScript.CreateObject("WScript.Shell")

'************************************************************************
' FUNCTIONS
'************************************************************************

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

function searchDir(strDir, strQuery)
	set objFolder = objFSO.GetFolder(strDir)
	for each objSubFolder 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.Name & vbNewLine
	next
	searchDir = strResults
end function


'************************************************************************
' BEGIN
'************************************************************************


if (objArgs.count) then
	strFile = objArgs(0)
	set objFile = objFSO.OpenTextFile(strFile)
	strServers = objFile.ReadAll
	arrServers = split(strServers, vbNewLine)
end if

set objResults = objFSO.OpenTextFile("results.txt", 2, true)

for each strServer in arrServers
	if (ping(strServer)) then
		set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
		set colDisks = objWMI.ExecQuery ("Select * from Win32_LogicalDisk")
		for each strDisk in colDisks
			strUNCDisk = "\\" & strServer & "\" & Replace(strDisk.DriveLetter, ":", "") & "$\"
			strResults = searchDir(strUNCDisk, strExt)
			objResults.WriteLine strResults & vbNewLine
		next
	else
		objResults.WriteLine strServer & ": Host Unreachable"
	end if
next

Unless your have the correct rights the the server, you'll get a permission denied error.

-Geates
 
captedgar, im not vain really but the code you posted 'what i have so far' looks just like the FAQ which i posted on this forum, which i might add has recieved very low ratings, what the hell people think makes it a poor FAQ lord only knows, i am sure these coding masters have must neater ways of doing it (hmm yeah right).

anyway, my point is i dont own getInfo as a text string as such, but i think it is going a little too far to openly put your name as the author?

i admit i put my name as the author of all my scripts at work (well not at my new place as they dont like it) but for best practice it has to be recommended, i do however give credit to others when i rip something off them

 
Hi Daniel

Thanks first of all for being helpful and writing a script for me. Now i have 2 script one of yours and my own which i managed to write upto. before i start i re-iterate i'm a complete newbie and you may find me asking some silly questions.

In the above script of yours, where i can set the servers values as there are 100 of them. Also strExt in the variable definition is where i need to change the file extension if i need to search for other file extension is that right?. Also where i can change the drive to search as our files could be either in c or g drive on each of the 100 servers. Also how can i exclude certain folders during the search. Files would probably not be in ProgramFiles, Documents and Settings, Windows folders etc

If it helps, i can send you the complete script i'm using

please guide me daniel
 
Sorry Daniel

I did not intend to put my name on it. This is just a sample script which i was provided to me by another developer. I just put my name so that whoever checks it knows these scripts are the ones they need to provide help/guidance to. if it helps i'll take off the author name as true to admit this is not my script but provided to me by someone else
 
where i can set the servers values as there are 100 of them.

[red]
The script takes a list of servers as an argument. Put the list of servers in a text file, one server name per line. Drag the list file onto searchServer.vbs (or whatever you named it).
[/red]

Also strExt in the variable definition is where i need to change the file extension if i need to search for other file extension is that right?

[red]
Correct.
[/red]

Also where i can change the drive to search as our files could be either in c or g drive on each of the 100 servers.

[red]
Wrap the code from the for loop (for each strDisk in colDisks) in an IF statement that checks the disks drive letter.
[/red]
Code:
for each strDisk in colDisks
   if (inStr(strDisk.DriveLetter, "C") OR (inStr(strDisk.DriveLetter, "G")) then
     ...do code
   end if
next

Also how can i exclude certain folders during the search. Files would probably not be in ProgramFiles, Documents and Settings, Windows folders etc

[red]
this makes things more complicated because you would need to modify the searchDir function, essentially, making it unique to your program. This is a bad idea because a program should be comprised of functions and sub routines that do not depend on each other. Otherwise, a web of dependence is created and you've got yourself a Windows ME caliber script!

Instead of starting the search from the root, specify the locations of your strPath.

searchDir(strUNCDisk & "\Temp", strExt)
searchDir(strUNCDisk & "\Windows", strExt)
searchDir(strUNCDisk & "\Folder\SubFolder", strExt)
[/red]

hope this helps!
-Geates
 
Hi daniel

Thanks for answering the reply

for the first question, i meant where can i call this text file in your script. I already have a text file called servers.txt which has about 90 odd server names in it. where i can call this server.txt file in your above script. i would imagine this to be different from the results.txt as results.txt is used for displaying the results. Also can i pass more than 100 arguments into array list?
 
As I stated, the script takes a list of servers as an argument. Drag the list file onto searchServer.vbs.


Alternatively, I suppose you can change the code as below

Code:
[s]if (objArgs.count) then[/s]
    strFile = [s]objArgs(0)[/s] [red]"servers.txt"[/red]
    set objFile = objFSO.OpenTextFile(strFile)
    strServers = objFile.ReadAll
    arrServers = split(strServers, vbNewLine)
[s]end if[/s]

-Geates
 
Categar said:
>I did not intend to put my name on it. This is just a sample script which i was provided to me by another developer.

If you really want to say sorry, you have to say sorry to "another developer". Or do you really mean to say anything at all as a "programmer" on that matter? (You know "programmer" is somehow closer to academic and plagerism is a serious matter in the academic circle. But programmer at the end of it is not academic.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top