Results 1 to 11 of 11

Thread: VBscript to Find a File on a Network then output the Results to txt

  1. #1

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Question VBscript to Find a File on a Network then output the Results to txt

    Essentially, this is what I am aiming to do with a vbscript:

    1) It reads a text file that has all computer bios names in it
    2) When it reads it it then will look for a filename I choose located in a folder I choose also (ie. c:\windows\test.bak) -- this is on the remote PC
    3) If it finds the file, it dumps the result to a text file (which I chose its location and name) with the name of the machine and the resulting file it found (all on the same line). This is done over, say, MANY machines that dump the file to a single output text as it scans the list from #1.
    4) If it doesn't find the file OR the network path isn't found to the machine in question, it should report this in the output file listing machine name and reporting "not found" on the same line.
    5) As a bonus: is it possible to set a TIMEOUT of, say, 10sec, that if it doesn't poll a result from #1 that it then just moves to the next bios name from #1?

    I got #1 completed:

    Code:
    on error resume next
    
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile ("c:\systemlist.txt", ForReading)
    Wscript.echo objtextfile
    ' *************************************************************
    ' create the input array
    strText = objTextFile.ReadAll
    objTextFile.Close
    arrComputers = Split(strText,vbcrlf)
    ' *************************************************************
    .. but the rest I am clueless. It will read from "c:\systemlist.txt" just fine.

    Thanks Vbforums community.

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: VBscript to Find a File on a Network then output the Results to txt

    This should get you through 2 and have you ready for 3 and 4. As for 5, it shouldn't take anywhere close to 10 seconds to see if the file exists. This code will only work if it is with admistrative rights on the machines being accessed.

    Code:
    on error resume next
    
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile ("c:\systemlist.txt", ForReading)
    Wscript.echo objtextfile
    ' *************************************************************
    ' create the input array
    strText = objTextFile.ReadAll
    objTextFile.Close
    arrComputers = Split(strText,vbcrlf)
    ' *************************************************************
    
    'Loop through the computer names
    For i = 0 To UBound(arrComputers)
    	'build the path to where you think the file is
    	strFilePath = "\\" & arrComputers(i) & "\c$\windows\test.bak"
    	'Check if it is there
    	blnExists = objFSO.FileExists(strFile)
    	
    	If blnExists Then
    		'Code to write if the file does exist
    		MsgBox "File exists on " & arrComputers(i)
    	Else
    		'Code to write if the file doesn't exist
    		MsgBox "File does not exists on " & arrComputers(i)
    	End If
    Next

  3. #3

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Exclamation Re: VBscript to Find a File on a Network then output the Results to txt

    Thanks MarkT..

    Two things to add also:

    1) The search string must also be able to accept wildcards so it looks on the remote PC for "\c$\windows\test*.*"
    2) Also, when it finds something called test*.* or multiple instances of it, it will dump the results to the output file like this:

    Bios Name FILE
    ======= ===
    RGSA789 TEST01.doc
    RGSA789 TEST02.doc

    The way you have it is that it reports Either YES ITS THERE or NO ITS NOT. The part of NO ITS NOT THERE is perfect but the first part where it finds something should dump the actual filename to the output file. This is critical actually.

    To answer #5 - the reason to have a timeout of, say, 10-15second or something I can set based on milliseconds is because when you do a computer bios search over a network you are searching the DNS name of the machines: this is something we cannot control as the network admins hardcode the # of seconds the timeout is. If this vbscript can keep track of the time between detecting the files on remote PCs this would be utterly huge for us because otherwise it takes up to 2min before it timesout and thats much too long scanning for 2500+ PCs.

    Thanks MarkT and community!

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: VBscript to Find a File on a Network then output the Results to txt

    The filesystemobject does not have a wildcard search capibility. What you will need to do is loop through all the files in the folder and see if they meet your criteria. That isn't too difficult if you use regular expression to do that.
    Code:
    on error resume next
    
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile ("c:\systemlist.txt", ForReading)
    Wscript.echo objtextfile
    ' *************************************************************
    ' create the input array
    strText = objTextFile.ReadAll
    objTextFile.Close
    arrComputers = Split(strText,vbcrlf)
    ' *************************************************************
    
    Dim objFolder, objFile, TS
    Dim strDirectory, strWildCard, strPattern
    Dim strLog, blnFound, i
    Dim objRegExpr
    
    	'Create an instance of the regexp object
    	Set objRegExpr = CreateObject("VBScript.RegExp")
    	
    	strWildCard = "test*.*"
    	
    	'Update the wildcard string to define a valid regular expression
    	strPattern = Replace(strWildCard, ".", "\.")
    	strPattern = Replace(strPattern, "*", ".*")
    	strPattern = "^" & strPattern & "$"
    	strPattern = Replace(strPattern, ".*$", ".+$")
    
    	objRegExpr.Pattern = strPattern
    	objRegExpr.Global = True
    	objRegExpr.IgnoreCase = True
    
    	'Set where you will log your findings
    	strLog = "c:\mylog.txt"
    	Set TS = objFSO.OpenTextFile(strLog,ForWriting,true) 
    	
    
    For i = 0 To UBound(arrComputers)
    	'Get the directory you are searching
    	strDirectory = "\\" & arrComputers(i) & "\c$\windows\"
    	
    	'Set your found flag
    	blnFound = False
    	
    	'Check that that the directory exists. This shouldn't take  
    	'long to retrun false if the machine can't be reached.
    	If objFSO.FolderExists(strDirectory) Then
    		'Get the current folder
    		Set objFolder = objFSO.GetFolder(strDirectory)
    		
    		'Loop through all the files in the folder
    		For Each objFile In objFolder.Files
    			'Check if the file matches the wildcard search
    			If objRegExpr.Test(objFile.Name) Then
    				'Add file to log if found
    				TS.WriteLine(arrComputers(i) & vbTab & objFile.Name)
    				blnFound = True
    			End If
    		Next
    	Else
    		'Note in the log if the machine couldn't be reached
    		TS.WriteLine(arrComputers(i) & " can not be reached")
    		blnFound = True
    	End If
    	
    	'Add note if the file wasn't found
    	If not blnFound Then
    		TS.WriteLine(arrComputers(i) & vbTab & strWildCard & " not found")
    	End if
    	
    	'Add an extra line to seperate the machines
    	TS.WriteLine("")
    
    
    Next
    
    TS.Close
    Set TS = Nothing
    Set objRegExpr = Nothing
    	
    MsgBox "done"

  5. #5

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: VBscript to Find a File on a Network then output the Results to txt

    Outstanding MarkT..

    My only concern so far is what about setting a Timeout so when the script calls each machine's bios name in the list from #1 that if a result isn't found OR the network path is invalid or doesn't exist it stops checking after, say, 10sec, reports it as it does, then moves to next one. OR.. what about a hard counter per machine bios name so we set each scan in #1 to a hard count of, say, 10sec? I am unsure the script is smart enough to keep track of a network timeout but gather there is a facility to record the time between scanning and reporting on each machine. Possible?

    Really appreciate your help in this matter!

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: VBscript to Find a File on a Network then output the Results to txt

    I don't think there is a way to have the script timeout. In the script above the line
    If objFSO.FolderExists(strDirectory) Then
    checks if the directory exists. When I tested it, if the machine can't be reached it returned false after about 2 seconds. If the machine did exist but the directory was invaild it returned false almost immediately. If the machine is accessible and the directory is valid it shouldn't take more than a second or so to loop through all the files.

  7. #7

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: VBscript to Find a File on a Network then output the Results to txt

    if the machine can't be reached it returned false after about 2 seconds
    The issue here is that we're dealing with a possible network delay - a computer could theoretically be up on the network but due to lag from the PC running this script to the target machine far off on the network, its essential to have a timeout option as much as 10-15sec. 2secs is much too short a time when dealing with calling out for a response on a large corporate network. I'll have to investigate this.

  8. #8

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: VBscript to Find a File on a Network then output the Results to txt

    MarkT - I noticed that when the script completes it adds an extra line at the end of the result file:

    Code:
     **Network Path could not be resolved**
    taken from:
    Code:
    		'Note in the log if the machine couldn't be reached
    		TS.WriteLine(arrComputers(i) & " **Network Path could not be resolved**")
    		blnFound = True
    Is there a reason why thats there and how to remove it?

    Btw, still researching the "network timeout between machine names in the list file". So far you can do a timeout via a cscript command line like:

    Code:
    cscript //t:60 TEST2.vbs
    Where it runs the .vbs with a timeout of 60seconds before the script discontinues BUT this cancels the entire script.

    There has to be another method inside vbscript to query the list for a selectable amount of time before it considers the result a TIMEOUT then proceed to next machine in the list?

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: VBscript to Find a File on a Network then output the Results to txt

    Try removing this

    'Add an extra line to seperate the machines
    TS.WriteLine("")

  10. #10

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: VBscript to Find a File on a Network then output the Results to txt

    I have.. no difference. Commenting that out only removes the line seperation between results. It doesn't affect the last line of the dump which adds the useless result as mentioned.

  11. #11

    Re: VBscript to Find a File on a Network then output the Results to txt

    I have ran this script on a remote computer \\192.168.1.100\c$

    and all it gives me is files located in that folder.

    how do i combine, this script which can list all sub directories and files
    http://www.vbforums.com/showthread.php?threadid=148928.

    with this one, to search all files on a remote computer?

    and on top of that i need who is owner and last modifier of the doc

    thanks
    Last edited by PaPPy; Sep 13th, 2011 at 03:23 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width