troubleshooting Question

How to do something in VB script if the number of lines in the text file exceeds a value

Avatar of wilko1000
wilko1000 asked on
VB Script
14 Comments1 Solution88 ViewsLast Modified:
Hi

I have a script that already opens a file then reads lines in a text file and then does some text manipulation

Now i want it to only do this if the number of lines in the text file is greater than 37.

Sample code below

Const LogPath = "X:\Logs\"
Logfile = LogPath & "DataFix-" & FormatDateTime(Date, 1) & ".Log"

LogEvent ( "***************************************")
LogEvent ( "***************Started****************")
LogEvent ( "***************************************")

DataFix "X:\VBIN\", "X:\VOUT"

LogEvent ( "***************************************")
LogEvent ( "***************Finished****************")
LogEvent ( "***************************************")




Sub DataFix(sourceFolder,targetDir)

On Error Resume next

set objFSO = CreateObject("Scripting.FileSystemObject")   
set objDir = objFSO.GetFolder(sourceFolder)
  

for each a in objDir.Files
	
		LogEvent ("Opening " & a)

		numlines = 1
		gotHeader = False
		HeaderText_FirstLine = ""
		HeaderText_SecondLine = ""
		HeaderText_ThirdLine = ""
		IsThisBTEST = False
		IsException = False

		set objTextFile = objFSO.OpenTextFile(a)
		
		filename = objFSO.GetFileName(a)

		new1 = targetDir + "\" + left(filename, len(filename) - 4) + "_New" + right(filename, 4)	
		set w1 = objFSO.CreateTextFile(new1, True)

		
		do until objTextFile.AtEndOfStream
			strLine = objTextFile.ReadLine		


'If number of lines is greater than 37 then do below
			
	Select case left(strLine, 7)
Case "-  ACM0"	
strline = Replace (strLine, "1", "2", 1,1)
End select

			select case left(strLine, 7)					
				case "-  ACM0"
					IsThisBTEST = True
					LogEvent ("This is a valid file, continuing...")
					HeaderText_FirstLine = strLine
					If Right(strLine, 5) = "1000E" Then
						w1.WriteLine Left(HeaderText_FirstLine, 8) & "1014" & Mid(HeaderText_FirstLine, 9, Len(HeaderText_FirstLine) - 13) 
						w1.WriteLine "1000E"
						IsException = True
						numlines = numlines + 1	
					Else
						
						If Right(strLine, 5) = "AAAAU" OR Right(strLine, 5) = "AAAB6" Then
							w1.WriteLine Left(HeaderText_FirstLine, 8) & "1014" & Mid(HeaderText_FirstLine, 9, Len(HeaderText_FirstLine) - 16)&"FED"
							w1.WriteLine "1000E"
							IsException = True
							numlines = numlines + 1
						Else
							w1.WriteLine Left(HeaderText_FirstLine, 8) & "1014" & Right(HeaderText_FirstLine, Len(HeaderText_FirstLine) - 8)
						End If
					End If
				case else
					if IsThisBTEST = True then
						w1.WriteLine strLine
					end if
			end select

			if IsThisBTEST = True then

				if numlines = 2 and Len(HeaderText_FirstLine) > 0 then
					if IsException = False Then
						HeaderText_SecondLine = strLine
					else
						HeaderText_SecondLine = "1000E"	
					end If					
				end if

				if numlines = 3 and Len(HeaderText_FirstLine) > 0 and Len(HeaderText_SecondLine) > 0 then
					HeaderText_ThirdLine = strLine
					gotHeader = True
				end if

				if gotHeader = True and numlines = 31 then
					if IsException = False Then
						w1.WriteLine Left(HeaderText_FirstLine, 8) & "1003" & Right(HeaderText_FirstLine, Len(HeaderText_FirstLine) - 8)
					else
                                        
						w1.WriteLine Left(HeaderText_FirstLine, 8) & "1003" & Mid(HeaderText_FirstLine, 9, Len(HeaderText_FirstLine) - 16)&"FED"
					end if
					
				end if

				if gotHeader = True and numlines = 33 then
					if IsException = False Then
						w1.WriteLine Left(HeaderText_FirstLine, 8) & "1004" & Right(HeaderText_FirstLine, Len(HeaderText_FirstLine) - 8)
					else
                                         
						w1.WriteLine Left(HeaderText_FirstLine, 8) & "1004" & Mid(HeaderText_FirstLine, 9, Len(HeaderText_FirstLine) - 16)&"FED"
					end if
					
				end if
				
				numlines = numlines + 1
				
			end if

		loop

		w1.Close		

		if IsThisBTEST = False then
			LogEvent ("Not a valid  file - No modifications made to file.")
			objFSO.DeleteFile (new1)
		else
			LogEvent ("Completed file manipulation successfully. New file written to " & new1) 
		end if

		LogEvent ("")

		LogEvent ("Deleting original file...")
		objTextFile.Close
		objFSO.DeleteFile (a)

		LogEvent ("")

next

set objFSO = nothing

IF err.number <> 0 THEN
  LogEvent ("Error # " & CStr(Err.Number) & " " & Err.Description)
END IF

	
End Sub

'**********************************************************************
'* @name     LogEvent([])
'* @param
'* @purpose
'* @return
'*************************************************************************
sub LogEvent (sEvent)

  Dim fso
  Dim Log

  set fso = wscript.createobject("scripting.filesystemobject")
  Set Log = fso.OpenTextFile(LogFile, 8, True)
  log.writeline Time & chr(9) & sEvent
  'WScript.Echo(sEvent)
  log.close
  set fso = nothing
End Sub
ASKER CERTIFIED SOLUTION
SStory

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros