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
30 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

19 Experts available now in Live!

Get 1:1 Help Now