Validate text file using VB - validation

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

Related

batch files to add or replace a word/line in a text file

I made this batch file to write in a text file:
rem Saved in E:\Downloads\dat\currentstate.bat
#echo off
#echo Current State> currentstate.txt
#echo League>> currentstate.txt
So the output right now in the txt is:
Current State
League
1st batch file: How can I make a batch file in order to add the word "camera" after the last line of any txt file. I would like to run the new batch file and my output to be:
Current State
League
Camera
2nd batch file: How can I make a batch file this time to replace everything in the first line of any txt file (in my example it happens to be "Current State") with the word "workbook" so the output to be:
workbook
League
Camera
I searched a lot in the forum/web but i couldn't find answers to my questions. Sorry for the double questioning and thanks in advance.
I am using FART for it.
no, it means Find and Replace Text
simply do this on your batch file
fart.exe [filename] "old text" "replacing text"
do the " ignore any [ and ]
http://fart-it.sourceforge.net/

Reading Code inside Action in QTP

I want to read the code written inside an action in QTP,just as we read text file using FileSystem Object.
Is there any way to read code written in action line by line ?
So basically an action is attached to a QTP-Testcase. When you save this test case at a certain location, the code you have written is saved inside the directory named action1 or whatever action you were editing. In this folder you will find a file named script.AVCHD file. Open this in notepad and you will find your code inside it. Now all you got to do is write a simple visual basic, (or just any script you like) that will open this file and readall of the content into a variable. see if this helps. thanks.
For each test there will script.mts inside the Action1 folder.
get the script text line
filename = "C:\YourUFTTest\Action1\script.mts"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
msgbox f.ReadLine
Loop
f.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.

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

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

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

Resources