Solved

How to check if a keyword exist and if not then add it with a default value in a specific position?

Posted on 2015-02-24
7
31 Views
Last Modified: 2015-03-03
I need an script to validate if the keyword "Da" exists and if not then add it after the word "C":
with default value =0

File with error
"A"   =4
"B"   =8
"C"   =14
"E"   =7

File fixed
"A"   =4
"B"   =8
"C"   =14
"Da"    =0
"E"   =7
0
Comment
Question by:acunaara
  • 4
  • 3
7 Comments
 
LVL 35

Expert Comment

by:Kimputer
ID: 40627895
Is the input file ALWAYS alphabetical (as in your example), or does it happen like this too?

"A"   =4
"B"   =8
"C"   =14
"E"   =7
"Da" = 8

or

"Da" = 8
"A"   =4
"B"   =8
"C"   =14
"E"   =7

or

"B"   =8
"C"   =14
"E"   =7

or

"A"   =4
"B"   =8

Please think carefully if any of these situations are a possibility, or you're sure it will never happen?
0
 

Author Comment

by:acunaara
ID: 40628324
Only needed that "Da"    =0 should be exactly after "C". If "C" doesn't exists then it's not necesary to add nothing.
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40628333
Do you need to run it on one file every now and then, or you need to check a whole lot of files in one folder?

Also, you didn't comment on all the situations, that means you don't ever expect anything else below "C" ? ("Da" with other number than 0) ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 40628448
This is the current code, adjust folder name, it will scan all files ending with .txt

Const ForReading = 1
Const ForWriting = 8

scanfolder = "C:\temp1\"

Set objfso=wscript.CreateObject("Scripting.FileSystemObject")
Set folder = objfso.GetFolder(scanfolder).Files

For Each file In folder
	if lcase(right(file.name,4)) = ".txt" then
		foundc = false
		foundcd = false
		Set objTextFile = objFSO.OpenTextFile(File, ForReading)
			
			'find first
			Do Until found or objTextFile.AtEndOfStream
				strNextLine = objTextFile.Readline
				if (Left(strNextLine,3) = """C""") Then
					foundc = true
					strNextLine = objTextFile.Readline
					if (Left(strNextLine,4) = """Da""")  Then
						foundcd = true
					end if
				End If
			Loop
			objTextFile.Close
			Set objTextFile = Nothing
			
			if not foundcd and foundc then
				Set objTextFile = objFSO.OpenTextFile(File, ForReading)
				Set objTextFile2 = objFSO.CreateTextFile(File & ".bak", True)
				Do Until objTextFile.AtEndOfStream
					strNextLine = objTextFile.Readline
					objTextFile2.WriteLine(strNextLine)
					if (Left(strNextLine,3) = """C""") Then
						objTextFile2.WriteLine("""Da""    =0")
					end if
				Loop 
				objTextFile.Close
				objTextFile2.Close
				Set objTextFile = Nothing
				Set objTextFile2 = Nothing
				temp = file
				objFSO.Deletefile file
				objFSO.Movefile temp & ".bak", temp
			end if
			'if found need to process
	end if
Next

Set objfso = Nothing

wscript.echo "done"

Open in new window


If "C" is found and then "Da", skip.
If no "C" found, skip.
If "C" found, and next is not "Da", then add "Da"    =0
If this logic is not correct, please elaborate (more words explaining the situation or more examples).
0
 

Author Comment

by:acunaara
ID: 40628556
This is to run it on several servers that i have in the path C:\config\setting.cfg
Regarding all the situations.. any other string of the config file can appear under "C" or "Da" or "E" or "A" or "B". So because of that i only need to find out the string "C"
0
 
LVL 35

Expert Comment

by:Kimputer
ID: 40630568
It was already working, but here I changed the things you could have changed yourself:

Const ForReading = 1
Const ForWriting = 8

scanfolder = "C:\config\"

Set objfso=wscript.CreateObject("Scripting.FileSystemObject")
Set folder = objfso.GetFolder(scanfolder).Files

For Each file In folder
	if lcase(right(file.name,4)) = ".cfg" then
		foundc = false
		foundcd = false
		Set objTextFile = objFSO.OpenTextFile(File, ForReading)
			
			'find first
			Do Until found or objTextFile.AtEndOfStream
				strNextLine = objTextFile.Readline
				if (Left(strNextLine,3) = """C""") Then
					foundc = true
					strNextLine = objTextFile.Readline
					if (Left(strNextLine,4) = """Da""")  Then
						foundcd = true
					end if
				End If
			Loop
			objTextFile.Close
			Set objTextFile = Nothing
			
			if not foundcd and foundc then
				Set objTextFile = objFSO.OpenTextFile(File, ForReading)
				Set objTextFile2 = objFSO.CreateTextFile(File & ".bak", True)
				Do Until objTextFile.AtEndOfStream
					strNextLine = objTextFile.Readline
					objTextFile2.WriteLine(strNextLine)
					if (Left(strNextLine,3) = """C""") Then
						objTextFile2.WriteLine("""Da""    =0")
					end if
				Loop 
				objTextFile.Close
				objTextFile2.Close
				Set objTextFile = Nothing
				Set objTextFile2 = Nothing
				temp = file
				objFSO.Deletefile file
				objFSO.Movefile temp & ".bak", temp
			end if
			'if found need to process
	end if
Next

Set objfso = Nothing

wscript.echo "done"

Open in new window


It assumes you don't have any other cfg files in that folder.
0
 

Author Closing Comment

by:acunaara
ID: 40642868
It's nice!! Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

14 Experts available now in Live!

Get 1:1 Help Now