Solved

Extract strings from multiple text files

Posted on 2010-09-24
13
618 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 74

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 51

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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 51

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 74

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 74

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 51

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now