Link to home
Start Free TrialLog in
Avatar of acunaara
acunaara

asked on

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

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
Avatar of Kimputer
Kimputer

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?
Avatar of acunaara

ASKER

Only needed that "Da"    =0 should be exactly after "C". If "C" doesn't exists then it's not necesary to add nothing.
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) ?
ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"
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.
It's nice!! Thanks