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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

skip to next line if character exists in certain column 1

Status
Not open for further replies.
Feb 12, 2009
2
US
I'm trying to grab the first 6 characters of the first line in a text file, but only if that line does NOT have a certain character in column 42. Below are the first couple of lines of text from that file. My script needs to skip line 1 in this case because it contains a minus in column 42 instead of a plus.

0333I610627Company Name 090312000186032-00000
7941I610627Company Name 090312000186032+00000

The following code works, except that it doesn't skip that invalid first line the way I want:

Code:
Option Explicit

Const ForReading = 1
Const ForWriting = 2  'set text file for writing  
Const ForAppending = 8

Dim objFSO   ' File System Object 
Dim objFile, objFile1, objFile2
Dim ts   ' output file object  
Dim strReportFileName  ' output file  
Dim BegstrCharacters, EndstrCharacters, strCharacters
Dim strNextLine, BegstrLine, EndstrLine, EndstrLine2
Dim objShell, filesys, searchstring, strContents

strReportFileName = "c:\temp\report.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")  
Set ts = objFSO.CreateTextFile(strReportFileName)  
ts.Close  
Set ts = objFSO.OpenTextFile(strReportFileName, ForWriting)  

If objFSO.FileExists("f:\company\file.txt") Then
 Set objFile = objFSO.OpenTextFile("f:\company\file.txt", ForReading)
End If 

If NOT IsEmpty(objFile) then
	BegstrCharacters = objFile.Read(6)
    strNextLine = objFile.ReadLine
    If Len(strNextLine) > 0 Then
        BegstrLine = BegstrCharacters
    End If

objFile.Close

Set ts = objFSO.OpenTextFile(strReportFileName, ForAppending)  
	ts.write "BegInv:  " & BegstrLine & vbCrLf
ts.Close

End If

Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad " & strReportFileName
 
Code:
strLine = "0333I610627Company Name   090312000186032-00000"
if right(left(strLine,42),1) <> "X"
 strVal = left(strLine,6)
end if

^ where "X" is the character you are looking for in col 42. If the value of "X" is not equal to the value you are looking for "strVAL" will be equal to the first 6 characters of the line.



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
 
Code:
...
If Not IsEmpty(objFile) Then
  Do
    strNextLine = objFile.ReadLine
  Loop Until Mid(strNextLine, 42, 1) = "+" Or objFile.AtEndOfStream
  If Not objFile.AtEndOfStream Then
    BegstrLine = Left(strNextLine, 6)
  End If
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Or,for a regular Expressions variant:

...
If Not IsEmpty(objFile) Then
With CreateObject("vbscript.regexp")
.Pattern = "^(.{6}).{35}[^-]"
.MultiLine = True
BegstrLine = .Execute(objFile.ReadAll)(0).SubMatches(0)
End With
End If
...
 
Thanks, all of you! I ended up going with PHV's solution, and it worked perfectly! For whatever reason, I couldn't wrap my head around the other answers.
Now I'm going to try and make it write this to an Excel spreadsheet instead of a text file, but I won't bother you all unless/until I get stumped again. Thanks again.
 
PHV,strongm -> cool, thanks for the other ways to do this.

I always like seeing how other people do the same things I do...although they all give the same result in the end..

holmes - to explain my method...

I am taking 42 characters from the left side of the string

str = left(strLine,42)

then grabbing 1 character from the right side

str2 = right(str,1)

str2 will either be the character in pos 42

that can be writting into one statement:

str2 = right(left(strLine,42),1)

if str2 is a "+", get the left 6 chars from the original values

strVal = left(strLine,6)






TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top