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
33 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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