Vb6 Performing an operation based on each line of a text file - algorithm

I have a text file with a series of commands each one is on a diferent line what I need to do is go through the text file line by line and for each line perform a set of operations. How would I loop through line by line?
Example:
Text file contains:
johndoe.log
Apples and organes.log
monkies and zebras.log
script would grab line 1(johndoe.log)
create a new text file named johndoe.log
go to line two
create a new text file named apples and organes.log
etc... until the text file is complete
I know how to do everything except the loop that performs an operation on each line of the text file :(
and I know its oranges, typoed and went with it.

In classic VB6:
Dim LineData as String
Dim FileHandle as Integer
FileHandle = FreeFile
Open "C:\Test.txt" For Input As #FileHandle
Do While Not EOF(FileHandle)
Line Input #FileHandle, LineData
' Do whatever with LineData
Loop
Close #FileHandle
Or you can look at the FileSystemObject

Related

Inserting Lines into a Non-Text File

I am trying to insert lines into the middle of a non-text file (the extension of the file is "dxf"). I am using vbscript to do this.
Everywhere I have look, I come across the FileSystemObject.OpenTextFile. However, when I try to use this on a dxf file, it is causing an error: Exception 80070057 (I believe this is an invalid file) .
Here is my code:
Dim file
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fileexists(dxfFile$) Then
Set file = fso.OpenTextFile(dxfPath, ForAppending, True)
file.WriteLine("<PORTLIST TESTING>ASDFLKJ")
file.Close
End If
dxfFile$ is not a valid VBscript variable name; use dxfFile, file or dfxPath (consistently)
FileExists is a method of the FileSystemObject; you need to call fso.FileExists
Neither dxfFile, nor dfxPath, nor ForAppending are defined
Calling .OpenTextFile with an undefined/empty first/filespec parameter throws an error 5 - Invalid procedure call or argument
You can't insert lines by appending them; modifying files 'in the middle' is especially clumsy in VBScript; loading the whole file into memory, editing, writing it back may work for you
.DFX file come in ASCII or binary format; if the latter, you can't use the FileSystemObject (see ADODB.Stream)

Validate text file using VB

Could you please advice me on getting the VB script for my below mentioned scenario!!!
Scenario:
VB script needs to open the text file and reads all the lines in the text file but main conditions are for example if the text file named read.000 so the VB script opens the file named read.000 then the validation of the text file should starts with reading the first line of the text file and that first line in the text file is same as the file name read.000 so VB script validation should exactly match first line of file with text file name(read.000). also, the file name can be any read.001 or read.002 whatever the file name it should be same inside the file which present in first line of the file.
if the condition satisfied then only it needs to go for second validation.
Once file name validation is satisfied then VB script needs to validate the text file structure for example, in the text file read.000 the second line starts "01 John lagoon Canada"
In the above example 01 -> represent serial number which should be only two characters if more than two characters then validation get fail
John -> represents first name which should be only 4 characters if its more than 4 then validation needs to get fail same applicable for lagoon.
Could you please advice me on the above request with VB script.
Well hope that i'm getting your problem right, i'll do something like this:
Dim fso, rfile, line
Set fso = CreateObject("Scripting.FileSystemObject")
Set rfile = fso.OpenTextFile(pathtoyourfile, 1)
Do While rfile.AtEndOfStream <> true
line = rfile.ReadLine
if line = fso.GetFileName(pathtoyourfile) then
do your next Validation or something else'must be speficied
else
do something or end 'must be speficied
End if
Loop
rfile.close

Convert a PDF to .txt gives me an empty .txt file

Hi I'm trying to read a pdf in Ruby, first of all I want to convert it into a txt. path is the path to the PDF, The point is that I get a .txt file empty, and as someone told me is a pdftotext problem, but I don't know how to fix it.
spec = path.sub(/\.pdf$/, '')
`pdftotext #{spec}.pdf`
file = File.new("#{spec}.txt", "w+")
text = []
file.readlines.each do |l|
if l.length > 0
text << l
Rails.logger.info l
end
end
file.close
What's wrong with my code? Thanks!
It's not possible to extract text from every PDF. Some PDF files use a font encoding that makes it impossible to extract text with simple tools such as pdftotext (and some PDF files are even completely immune to direct text extraction with any tool known to me -- in these cases you'll have to apply OCR first to have a chance to extract text...).
So if you test your code with the same "weird" PDF file all the time, it may well happen that you're getting frustrated over your code while in reality the fault lies with the PDF.
First make sure that the commandline usage of pdftotxt works well with a given PDF, then test (and develop further) your code with that PDF.
The problem is you are opening the file in write ("w") mode, whuch truncates the file. You can see a table of file modes and what they mean at http://ruby-doc.org/core-1.9.3/IO.html.
Try something like this, it uses a pdftotext option to send the text to stdout to avoid creating a temporary file and uses blocks for more idiomatic ruby.
text = `pdftotext #{path} -`
text.split.select { |line|
line.length > 0
}.each { |line|
Rails.logger.info(line)
}
You would need to open the txt file with write permission.
file = File.new("#{spec}.txt", "w")
You could consult How to create a file in Ruby
Update: your code is not complete and looks buggy.
Cant say what is path
Looks like you are trying to read the text file to which you intend to write file.readlines.each
spell check length you have it l.lenght
You may want to paste the actual code.
Check this gist https://gist.github.com/4160587
As mentioned, your code is not working because you are reading and writing to the same file.
Example
Ruby code file_write.rb to do the file write operation
pdf_file = File.open("in.txt")
output_file = File.open("out.txt", "w") # file to which you want to write
#iterate over input file and write the content to output file
pdf_file.readlines.each do |l|
output_file.puts(l)
end
output_file.close
pdf_file.close
Sample txt file in.txt
Some text in file
Another line of text
1. Line 1
2. Not really line 2
Once your run file_write.rb you should see new file called out.txt with same content as in.txt You could change the content of input file if you want. In your case you would use pdf reader to get the content and write it to the text file. Basically first line of the code will change.

Vbscript seek to end of file to make a tail command script

I've writen a simple tail command using vbscript. It works fine except for very large files where it has to read through the entire file to get the last 10 lines. Is there a way to seek to the end of the file and then read backwards for ten lines?
I am afraid that seeking backwards is impossible in VBS TextStream, but instead of reading through the entire file you can seek to a position eg. 1K before EOF and then read the rest of the file, displaying only the last 10 lines.
EDIT: I'm adding some sample code to illustrate the idea:
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.GetFile(filePath)
set stream = file.OpenAsTextStream(1, -2)
pos1KBeforeEnd = file.Size-1024
if pos1KBeforeEnd<0 then pos1KBeforeEnd=0
stream.Skip pos1KBeforeEnd

How to parse one text file to multiple files based on data contained within

I have an enormous text file that I'd like to parse into other files - I think the only thing I have on this box (company computer) to use is VBS, so here's my question:
I have text file with a bunch of network configurations that looks like this:
"Active","HostName","IPAddress"
!
.......
end
This repeats itself throughout the file, but obviously for each configuration different data will occur within the "..." It always says "Active" for each configuration as well.
I want to create save files of the type HostName.cfg for each configuration and save all of the text between and including the ! and "end" . The line with the three quoted attributes doesn't need to be copied.
I'm still learning VBS so I'd appreciate any help in the matter. Thanks!
Here are some useful file reading functions and statements:
Freefile
Returns an integer which you should assign to a variable and then use in an Open statement.
Open <string> For Input As <integer>
Opens the specified file using the specified file handle. Don't use the same file handle twice without closing it in between.
Line Input #<integer>, <variable>
Reads one line of the file into a string.
Input #<integer>, <variable>, <variable>, <variable>
Reads one line of the file delimited by commas into several variables.
I once had to read a text file while omitting starting and ending lines. Though I didn't need to write it anywhere, FSO is simple enough that you will be able to figure it out. Here's some code for reading a file and link for writing to file via FSO: link
'Create a FSO object and open the file by providing the file path
Dim fso, f, filePath, line
filePath = "test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filePath)
'Skip through the first two lines
f.readline
f.readline
'Read till the end of file, if the line is not "end", split it based on ","
while not f.AtEndOfStream
line = f.readline()
if line <> "end" then
arr = split(line, ",")
'Write the arr to file
end if
wend
f.Close

Resources