Solved

Extract strings from multiple text files

Posted on 2010-09-24
13
637 Views
Last Modified: 2012-05-10
Hi,

I have an application that works based on an "optionfile.app" on each server. This is actually a txt file that can be opened with notepad. The path of the optionfile is the same in all servers, something like "C:\App\optionfile.app".

I need a script to pull two specific lines from every optionfile.app from every server. Assume that the optionfile is something like this:

Communication   tcpip
Port   1500
TCPaddress    10.0.0.4
ServerName     server1
Passwordaccess     password

From every optionfile, I need to extract these two lines:

TCPaddress    10.0.0.4
ServerName     server1

and output to a txt or csv file, does not matter. I've done some research that I can extract this with VB.net and using RegEx but this will run on every server (Windows 2003, 2008 both x86 and x64) so I don't think a .NET solution is reliable.

Any help and direction appreciated. Thanks.
0
Comment
Question by:BatuhanCetin
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 33753915
Why would .NET be unreliable? I believe it comes with or can be installed on 2003 and I know it comes with 2008.

In any event, I don't think you need regex for something like this--a simple InStr should suffice. Here is a VBScript example:
Dim inFilename
Dim outFilename
Dim fso
Dim inFile
Dim outFile
Dim line
Dim output
Dim nothingFound


inFilename = "C:\App\optionfile.app"
outFilename = "C:\App\searchresults.txt"


nothingFound = True

Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.OpenTextFile(outFilename, 2, True)   ' Change to 8 for appending to output instead of overwriting

If fso.FileExists(inFilename) Then

    Set inFile = fso.OpenTextFile(inFilename, 1)

    While Not inFile.AtEndOfStream
        line = inFile.ReadLine
        
        If InStr(line, "TCPaddress") > 0 Or InStr(line, "ServerName") > 0 Then
            outFile.WriteLine(line)
            nothingFound = False
        End If
    Wend
    
    If nothingFound Then
        outFile.WriteLine("No results found!")
    End If
    
    inFile.Close
    outFile.Close
Else
	outFile.WriteLine("Could not locate input file!")
End If

Open in new window

0
 
LVL 11

Author Comment

by:BatuhanCetin
ID: 33786738
Thanks kaufmed, this is working exactly as I want on a single server. I need to pull this info from many servers, so I need to modify "inFilename" parameter to meet the following;

1) Because I will get this info from multiple servers, the path must be smething like;

inFilename = "\\" & serverlist & "\c$\App\optionfile.app"

and serverlist will be a txt file that includes server names each per line. VBS must connect to each machine from that list at a time.

2) Also on some servers the name of the optionfile.app file is modified like "optionfileV1.app" or "old_optionfile.app". These are renamed by server admins so I can't tell exactly. So I will need to search for;

*optionfile*.app

How can I do this? Thanks.
0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 33788148
Don't know if you are interested, but here's a little BAT file that should do the job.  When I tested it here it produced the output below.  Edit the two SET's near the top as needed.

Server:[server1], File:[\\server1\c$\apps\optionfileV1.app]
TCPaddress    10.0.0.4
ServerName     server1
Server:[server2], File:[\\server2\c$\apps\optionfileV2.app]
TCPaddress    10.0.0.5
ServerName     server2

~bp
@echo off
set ServersFile=c:\temp\EE26497242\servers.txt
set OutputFile=c:\temp\EE26497242\output.txt
if exist "%OutputFile%" del "%OutputFile%"
for /F "usebackq tokens=*" %%A in ("%ServersFile%") do (
  for %%B in ("\\%%~A\c$\apps\*optionfile*.app") do (
    echo Server:[%%~A], File:[%%~B]>>"%OutputFile%"
    findstr /i /c:"tcpaddress" /c:"servername" "%%~B">>"%OutputFile%"
  )
)

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Author Comment

by:BatuhanCetin
ID: 33788394
Wow, thanks that's wonderful. I don't know a lot about batch scripting so I didn't understand much of the code but it works :)

However I will wait for kaufmed if he can show me a way to do it with VBS, then split the points. Thanks again.
0
 
LVL 11

Author Comment

by:BatuhanCetin
ID: 33788608
Oh billprew, I was messing around with your script and I realized the script hangs (most probably retrying in background) when it can't reach a server. How can I get past this? I need it to continue to the following server if it can't reach the current server. The reasons might be the server is shutdown, the service account does not have permission to access "C$" on that machine, cannot resolve DNS name, etc..

Can we set something like a timeout? Each *.app file takes at most 10-15 sec to be processed. Can we tell the script to continue to next server if it can't get the app file in that time? Preferebly write an error message ("Access Failed") to the output file?
0
 
LVL 11

Author Comment

by:BatuhanCetin
ID: 33788643
After I wrote my last comment, I see that the script actually moved on to the next server, after waiting about 5-10 minutes.

This is good news but if possible a timeout like 10 sec and an error message would be useful.
0
 
LVL 11

Author Comment

by:BatuhanCetin
ID: 33788813
I'm sending multiple comments but the problem appears to be something totally different from a timeout issue.

In my first run against the server list, it get the info from the first server in the list, waited for 10 minutes, then jumped to the 428nd server in the list, continued and finished successfully from there on.

In my second run, after getting the info from 1st server, it jumped to the 3rd server, then some server else.

It seems to skip servers randomly. There are servers that the script got the lines at the first run, but not the second and vice versa.

I also tried to run the script against a single server which seems to be failed in both bulk runs and it worked!

Do you have any idea of this random jumps?
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 33789542
Probably the next step would be to add a test as a server is read from the text file to try and make sure it's a valid server, is online, and that the share exists.  We can add some code to use PING etc to do some basic checking on the server being up.

~bp
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33790102
>>  However I will wait for kaufmed if he can show me a way to do it with VBS

My post was VBS  :)
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 33790258
I may have misinterpreted that comment  :\

I any event, I believe this is what you are after. I don't have any network shares to test on, but I believe it to be accurate. I must admit, though, billprew's solution is pretty slick--especially considering it's a batch file and only takes 10 lines...  Nice  :)
Dim serverFilename
Dim inFilename
Dim outFilename
Dim fso
Dim serverFile
Dim inFile
Dim outFile
Dim line
Dim output
Dim nothingFound

serverFilename = "C:\serverlist.txt"

nothingFound = True

Set fso = CreateObject("Scripting.FileSystemObject")
Set outFile = fso.OpenTextFile(outFilename, 2, True)   ' Change to 8 for appending to output instead of overwriting

If fso.FileExists(serverFilename) Then
	Set serverFile = fso.OpenTextFile(serverFilename, 1)
	
	While Not serverFile.AtEndOfStream
		inFilename = serverFile.ReadLine()
		
		If fso.FileExists(inFilename) Then
			Set inFile = fso.OpenTextFile(inFilename, 1)
			
			outFile.WriteLine(inFilename)

			While Not inFile.AtEndOfStream
				line = inFile.ReadLine

				If InStr(line, "TCPaddress") > 0 Or InStr(line, "ServerName") > 0 Then
					outFile.WriteLine(VbTab & line)
					nothingFound = False
				End If
			Wend

			If nothingFound Then
				outFile.WriteLine(VbTab & "No results found!")
			End If

			inFile.Close
		Else
			outFile.WriteLine(VbTab & "Could not locate input file!")
		End If
	Wend
Else
	outFile.WriteLine("Could not locate server list file!")
End If

outFile.Close

Open in new window

0
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 250 total points
ID: 33804668
Let's see if this helps a bit...

~bp
@echo off
set ServersFile=c:\temp\EE26497242\servers.txt
set OutputFile=c:\temp\EE26497242\output.txt
if exist "%OutputFile%" del "%OutputFile%"
for /F "usebackq tokens=*" %%A in ("%ServersFile%") do (
  ping /n 1 %%A | find "TTL">nul
  if %ERRORLEVEL% EQU 0 (
    for %%B in ("\\%%~A\c$\apps\*optionfile*.app") do (
      echo Server:[%%~A], File:[%%~B]>>"%OutputFile%"
      findstr /i /c:"tcpaddress" /c:"servername" "%%~B">>"%OutputFile%"
    )
  )
)

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34990917
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question