Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add semi-colon to lines of text file via inputbox

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hi.. I have a text file that is used to control a machine. From time to time, I need to edit the file to remove some functions in the machine program. What I need is to be able to add a semicolon to the controls in the program and then reverse this action when I am finished, since the changes will be temporary. I have found scripts to enter something in the beginning of the line, but not like what I need to do. The file structure looks like this:

Code:
N0001 (D12)
N0002 X -0.987 Y 0.348
N0003 Z -0.725
N0004 G01 F7.000
N0005 (D13)
N0006 X -0.239 Y 0.212
N0007 Z -0.725
N0008 G01 F7.000

So, this is how the original file will look. In brackets (D12) is the operation of which I want to skip, so I need to place a semicolon after each "N" Number like so, until I get to the next operation.

To Skip (D12) =
Code:
N0001 ;(D12)
N0002 ;X -0.987 Y 0.348
N0003 ;Z -0.725
N0004 ;G01 F7.000

What I would like to be able to do is have an Inputbox to ask "Enter an Operation to Skip" and enter (D12) to place the semicolon where needed, which will be in all lines until the next set of brackets. Any help on this would be great! Thanks
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV ... I have been playing with this piece of code, but all I can do so far is add the semicolon before the line number. I can't find anything on how to get the semicolon in the position I need (character 7) and how to have it place it from the reference found (D12) to the next set of () since they are not in numerical order.

Code:
Const ForReading = 1
Dim words(1)
Dim msg, find

find = InputBox ("Enter a Reference Operation: ")
words(0) = find

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("c:\Temp\AddChar1.txt", ForReading)
Set outFile = objFSO.OpenTextFile("c:\Temp\AddChar2.txt", 8, True)

Do Until inFile.AtEndOfStream
    strSearchString = inFile.ReadLine
	For i = 0 To UBound(words)-1
	If InStr(strSearchString,words(i)) Then
		msg = ";" & msg & strSearchString & vbcrlf
	End If
	next
Loop

inFile.Close
outfile.WriteLine msg

WScript.Echo "Done!"
 
Is the instruction code always 6 chars? I would infer from your previous posts that it is. And are there always 4 instructions per instruction set?
Code:
If InStr(strSearchString,words(i)) Then
   for i = 1 to 4
      msg = left(strSearchString, 7) & ";" & mid(strSearchString, 8) & vbcrlf
   next
else
   msg = msg & strSearchString & vbCrLf
end if

-Geates

 
Geates, thanks for the reply. To answer your questions, Yes the semicolon would always go to the 7th char position. But No on the 4 lines per set, this is variable. The only consistent item is the next set of "( )
 
Then you'll want to read the next line to see if that char exists in the 8th position. Because this solution requires reading the next line in the file, the file position pointer will be ahead and the line will be skipped. To counter this, set a flag that instructs the scrip to read the next line. Maybe,

Code:
[COLOR=#4E9A06]readflag = true[/color]
do until inFile.AtEndOfStream

   if (readflag = true) then    
      strSearchString = inFile.ReadLine
   end if

   for i = 0 to ubound(words) - 1
      if inStr(strSearchString, words(i)) then
         do
            msg = msg & left(strSearchString, 7) & ";" & mid(strSearchString, 8) & vbcrlf
            strSearchString = infile.ReadLine
         loop until (mid(strSearchString, 8, 1) = "(")
         readflag = false
      else
         msg = msg & strSearchString & vbCrLf
         readflag = true
      end if 
   next
loop

-Geates

 
Geates, if I put this together correctly, I get an error "Input past end of file" on line 23

Code:
strSearchString = inFile.ReadLine

Complete code:

Code:
Const ForReading = 1
Dim words(1)
Dim msg, find

find = InputBox ("Enter a Reference Operation: ")
words(0) = find

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("c:\Temp2\AddChar1.txt", ForReading)
Set outFile = objFSO.OpenTextFile("c:\Temp2\AddChar2.txt", 8, True)

readflag = true
do until inFile.AtEndOfStream

   if (readflag = true) then    
      strSearchString = inFile.ReadLine
   end if

   for i = 0 to ubound(words) - 1
      if inStr(strSearchString, words(i)) then
         do
            msg = msg & left(strSearchString, 7) & ";" & mid(strSearchString, 8) & vbcrlf
            strSearchString = inFile.ReadLine
         loop until (mid(strSearchString, 8, 1) = "(")
         readflag = false
      else
         msg = msg & strSearchString & vbCrLf
         readflag = true
      end if 
   next
loop

inFile.Close
outfile.WriteLine msg

WScript.Echo "Done!"
 
yeah, that would make sense :/ You'll want to check that the file pointer is not .AtEndOfStream before reading another line.

Code:
      if inStr(strSearchString, words(i)) then
         do
            msg = msg & left(strSearchString, 7) & ";" & mid(strSearchString, 8) & vbcrlf
            [highlight #FCE94F]if not inFile.AtEndOfStream then[/highlight]  strSearchString = inFile.ReadLine
         loop until (mid(strSearchString, 8, 1) = "(")
         readflag = false
      else
         msg = msg & strSearchString & vbCrLf
         readflag = true
      end if

-Geates

 
and you'll otherwise want to bail out of the do..loop.

Code:
if inStr(strSearchString, words(i)) then
   do
      msg = msg & left(strSearchString, 7) & ";" & mid(strSearchString, 8) & vbcrlf
      [highlight #FCE94F]if not inFile.AtEndOfStream then[/highlight]
         strSearchString = inFile.ReadLine
      [highlight #FCE94F]else[/highlight]
      [highlight #FCE94F]   exit do[/highlight]
      [highlight #FCE94F]end if[/highlight]
   loop until (mid(strSearchString, 8, 1) = "(")
   readflag = false
else
   msg = msg & strSearchString & vbCrLf
   readflag = true
end if

-Geates

 
Geates, this is very close to what I want. This code works well. What would I have to add in order to change multiple process points at one time? If I were to enter "D12" to start and then continue to enter "D18", "F21" etc and then have the file update. Thanks
 
I see! I was confused at first because I thought that was already the case. A closer look shows you only do it once (dim words(1)). Well, you actually have the clockwork in place, just need a minor adjustment...

Code:
[highlight #FCE94F][s]Dim words(1)[/s][/highlight] 

find = InputBox ("Enter a Reference Operation (comma separated): ")

'Split the string, "find", into an array, "words", of strings delimited by a comma
[highlight #FCE94F]words = split(find, ",")[/highlight]

The rest is good to go

-Geates

 
Geates, maybe I am entering it wrong in the inputbox, it seems to do the first one, but not the second one I enter. Just separate by a comma, right? I enter just like this:

Code:
D7,D8

It does not change D8 lines.
 
Here is my complete code:

Code:
Const ForReading = 1
'Dim words(1)
Dim msg, find

find = InputBox ("Enter a Reference Operation (comma separated): ")

'Split the string, "find", into an array, "words", of strings delimited by a comma
words = split(find, ",")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("c:\Temp2\AddChar1.txt", ForReading)
Set outFile = objFSO.OpenTextFile("c:\Temp2\AddChar2.txt", 8, True)

readflag = true
do until inFile.AtEndOfStream

   if (readflag = true) then    
      strSearchString = inFile.ReadLine
   end if

   for i = 0 to ubound(words) - 1
 if inStr(strSearchString, words(i)) then
   do
      msg = msg & left(strSearchString, 6) & ";" & mid(strSearchString, 7) & vbcrlf
      if not inFile.AtEndOfStream then
         strSearchString = inFile.ReadLine
      else
         exit do
      end if
   loop until (mid(strSearchString, 7, 1) = "(")
   readflag = false
else
   msg = msg & strSearchString & vbCrLf
   readflag = true
end if 
Next 
Loop


inFile.Close
outfile.WriteLine msg

WScript.Echo "Done!"
 
change [tt]for i = 0 to ubound(words) - 1[/tt]
to [tt]for i = 0 to ubound(words)[/tt]

and you'll want to incorporate another flag (writeflag) to prevent line duplication
Code:
for i = 0 to ubound(words)
		if inStr(strSearchString, words(i)) then
			do
				msg = msg & left(strSearchString, 6) & ";" & mid(strSearchString, 7) & vbcrlf
				if not inFile.AtEndOfStream then
					strSearchString = inFile.ReadLine
				else
					exit do
				end if
			loop until (mid(strSearchString, 7, 1) = "(")
			readflag = false
			writeflag = false
		else
			readflag = true
			writeflag = true
		end if
	Next
	if (writeflag = true) then msg = msg & strSearchString & vbCrLf

-Geates

 
incorporating flags and nested do..loops in superfluous. Let's rewrite it

Code:
readflag = true
do until inFile.AtEndOfStream
	strLine = inFile.ReadLine
	if (mid(strLine, 7, 1) = "(") then comment = false
	for i = 0 to ubound(words)
		if inStr(strLine, words(i)) then
			comment = true 
			exit for
		end if
	Next
	if (comment = true) then strLine = left(strLine, 6) & ";" & mid(strLine, 7)
	outfile.WriteLine strLine
Loop

-Geates

 
Thank You, Geates. This works GREAT! I have a lot to learn from this code as I have not gotten into this yet. I appreciate your help & time on this.
 
Geates, I was trying to place an IF statement in the code to handle if what the user inputs is NOT found, but I must not be placing it correctly.
 
Well, seeing as how the user can search for multiple tokens (words), our solution should accommodate dynamically.

1. Create an array, arrFound, equal to the size of "words". (cannot DIM an array dynamically, so use REDIM)
2. Every time a token is found, set the corresponding element in arrFound to "true"
3. Cycle through arrFound and make a note of those tokens that were not found.
4. If any were not found, tell the user.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("c:\Temp\input.txt", 1, true, 0)
Set outFile = objFSO.OpenTextFile("c:\Temp\output.txt", 8, true, 0)

find = InputBox ("Enter a Reference Operation (comma separated): ")
words = split(find, ",")
[COLOR=#CC0000]redim arrFound(ubound(words))[/color]

readflag = true
do until inFile.AtEndOfStream
	strLine = inFile.ReadLine
	if (mid(strLine, 7, 1) = "(") then comment = false
	for i = 0 to ubound(words)
		if inStr(strLine, words(i)) then
			[COLOR=#CE5C00]arrFound(i) = true[/color]
			comment = true 
			exit for
		end if
	Next
	if (comment = true) then strLine = left(strLine, 6) & ";" & mid(strLine, 7)
	outfile.WriteLine strLine
loop 

inFile.Close
outfile.WriteLine msg

[COLOR=#204A87]for i = 0 to ubound(found)
	if (found(i) <> true) then
		tokens = tokens & words(i) & ", "
	end if
next[/color]
[COLOR=#4E9A06]if (len(tokens)) then msgbox "These tokens were not found: " & left(tokens, len(tokens) - 2)[/color]

msgbox "done"

-Geates

 
Geates, I get an error on this line:

Code:
for i = 0 to ubound(found)

Here is my complete code:

Code:
Dim msg, find

Const ForReading = 1

find = UCase(InputBox ("ENTER ONE OR MORE REFERENCES TO EXCLUDE FROM PROGRAM: " & vbCr & vbCr & "(Comma Separated - No Spaces): "))


'Split the string, "find", into an array, "words", of strings delimited by a comma
words = split(find, ",")
redim arrFound(ubound(words))


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set inFile = objFSO.OpenTextFile("C:\Temp2\80-108-01.txt", ForReading)
Set outFile = objFSO.OpenTextFile("C:\Temp2\80-108-TEMP.txt", 8, True)

readflag = true
do until inFile.AtEndOfStream
	strLine = inFile.ReadLine
	if (mid(strLine, 7, 1) = "(") then comment = false
	for i = 0 to ubound(words)
		if inStr(strLine, words(i)) then
		arrFound(i) = true
            comment = true 
			exit For
		end If
	Next
	If (comment = true) Then strLine = left(strLine, 6) & ";" & mid(strLine, 7)
	outfile.WriteLine strLine

Loop

inFile.Close
outfile.WriteLine msg

for i = 0 to ubound(found)
	if (found(i) <> true) then
		tokens = tokens & words(i) & ", "
	end if
next
if (len(tokens)) then msgbox "These tokens were not found: " & left(tokens, len(tokens) - 2)

MsgBox "Done!"
 
You defined arrFound, so use it !

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top