We help IT Professionals succeed at work.
Get Started

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

wilko1000
wilko1000 asked
on
87 Views
Last Modified: 2016-09-09
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

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE