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
35 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 36

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 36

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

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 36

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

617 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