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

Change ReadLine to include Mid in addition to Left 1

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
0
0
US
I've got working code (thanks to help from this forum) that will write to a file the first 42 characters of each line in numerous files. Works like a charm. However, I now have a need to also write characters from 10 other areas of each line.

For instance, in the code below, i need to modify GetContents like so:
From:
GetContents = Left(objReadFile.ReadLine,42)

To:
GetContents = Left(objReadFile.ReadLine,42) & "|" & Mid(objReadFile.ReadLine,893,6) & "|" '& Mid(objReadFile.ReadLine,900,6) & "|" & Mid(objReadFile.ReadLine,907,6) & "|" & Mid(objReadFile.ReadLine,914,6) & "|" & Mid(objReadFile.ReadLine,921,6) & "|" & Mid(objReadFile.ReadLine,928,6) & "|" & Mid(objReadFile.ReadLine,935,6) & "|" & Mid(objReadFile.ReadLine,942,6) & "|" & Mid(objReadFile.ReadLine,949,6) & "|" & Mid(objReadFile.ReadLine,956,6)

Or create a new variable (in addition to GetContents) for each Mid and then include pipe delimiting in the WriteLine.

However, in both cases, I'm receiving an error, "Input past end of file", which I suspect is due to the fact that not every line in the file has characters in those Mid points. Is there a way to code around that so I can just capture the empty character space?

Code:
  If fso.FolderExists(Path_11101) Then
    Set Folder = fso.GetFolder(Path_11101)
      For Each File In Folder.files
         If (inStr(File.Name, "14OP"))  Then
			   'msgbox "Other\" & strDate & "-Download"
			   Set objReadFile = FSO.OpenTextFile(File,ForReading)
			      Do until objReadFile.AtEndofStream
					GetContents = Left(objReadFile.ReadLine,42)
					Set objTextFile = FSO.OpenTextFile(LogPath & "Search_Saig_Folders_ISIR_All_Heald_" & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".txt", 8, True)
					'objTextFile.Writeline FSO.GetFolder(Folder) & "\" & strDate & "-Download" & "|" & FSO.GetFileName(File.Name)
					objTextFile.Writeline "11101-" & GetContents
					Set objTextFile = nothing	   
	     		  Loop
		 End If	   

	Next
  End If
 
The problem with this line:
Code:
GetContents = Left(objReadFile.ReadLine,42) & "|" & Mid(objReadFile.ReadLine,893,6) & "|" '& Mid(objReadFile.ReadLine,900,6) & "|" & Mid(objReadFile.ReadLine,907,6) & "|" & Mid(objReadFile.ReadLine,914,6) & "|" & Mid(objReadFile.ReadLine,921,6) & "|" & Mid(objReadFile.ReadLine,928,6) & "|" & Mid(objReadFile.ReadLine,935,6) & "|" & Mid(objReadFile.ReadLine,942,6) & "|" & Mid(objReadFile.ReadLine,949,6) & "|" & Mid(objReadFile.ReadLine,956,6)
is that each time you see objReadFile.ReadLine, the script reads the next line in the file. I don't believe this is what you are looking for.

Instead, read a line from the file and assign it to a variable; use the variable in place of the readline statements.
Something like:
Code:
myLine = objReadFile.ReadLine
GetContents = Left(myLine,42) & "|" & Mid(myLine,893,6) & "|" '& Mid(myLine,900,6) & "|" & Mid(myLine,907,6) & "|" & Mid(myLine,914,6) & "|" & Mid(myLine,921,6) & "|" & Mid(myLine,928,6) & "|" & Mid(myLine,935,6) & "|" & Mid(myLine,942,6) & "|" & Mid(myLine,949,6) & "|" & Mid(myLine,956,6)

I'd also suggest checking the length of the line before trying to extract substrings.
 
That worked! Makes sense...thank you very much!
 
The Mids are getting the first 6 chars of each 7 char interval starting at 893. What does the line from char 893 look like? Is it something that [tt]replace()[/tt] could take care of?


Code:
myLine = objReadFile.ReadLine
strLeft = left(myLine, 42)

myLine893 = mid(myLine, 893) 'This essentially cuts off the first 892 chars so we work with a smaller (easier) value
strMid = replace(myLine, some_character, with "|")

GetContents = strLeft & "|" & strMid

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top