Counts line in a text file VB - visual-studio-2010

I am looking for a way to count the number of lines in a text file, excluding the CRLF which will be the very last line per say.
Any chance there is a simple code example for this?

Try this (though I found it searching the very title of your question in Google):
IO.File.ReadAllLines("C:\Users\Dan\Desktop\test.txt").Length
If you are worried about empty lines at the end, then loop from all lines until you find one with contents, and delete all of them until the end. This is easy, since:
IO.File.ReadAllLines("C:\Users\Dan\Desktop\test.txt")
Returns a vector of Strings.
Hope this helps.

Try with this:
TextBox1.Text = ""
With OpenFileDialog1
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
.Filter = "Text Files (*.txt)|*.txt"
If .ShowDialog <> DialogResult.OK Then Exit Sub
End With
Dim intLines As Integer = 0
Dim sr As New IO.StreamReader(OpenFileDialog1.FileName)
Do While sr.Peek() >= 0
TextBox1.Text += sr.ReadLine() & ControlChars.CrLf
intLines += 1
Loop
sr.Close()
MessageBox.Show(intLines, Me.Text)

basically you need to calibrate the md5#sum and then check to see if the HTML is linked to the CSS and make sure you have turned on javascript

Related

hidden space in excel

I tried almost all the methods (CLEAN,TRIM,SUBSTITUTE) trying to remove the character hiding in the beginning and the end of a text. In my case, I downloaded the bill of material report from oracle ERP and found that the item codes are a victim of hidden characters.
After so many findings, I was able to trace which character is hidden and found out that it's a question mark'?' (via VBA code in another thread) both at the front and the end. You can take this item code‭: ‭11301-21‬
If you paste the above into your excel and see its length =LEN(), you can understand my problem much better.
I need a good solution for this problem. Therefore please help!
Thank you very much in advance.
Thanks to Gary's Student, because his answer inspired me.
Also, I used this answer for this code.
This function will clean every single char of your data, so it should work for you. You need 2 functions: 1 to clean the Unicode chars, and other one to clean your item codes_
Public Function CLEAN_ITEM_CODE(ByRef ThisCell As Range) As String
If ThisCell.Count > 1 Or ThisCell.Count < 1 Then
CLEAN_ITEM_CODE = "Only single cells allowed"
Exit Function
End If
Dim ZZ As Byte
For ZZ = 1 To Len(ThisCell.Value) Step 1
CLEAN_ITEM_CODE = CLEAN_ITEM_CODE & GetStrippedText(Mid(ThisCell.Value, ZZ, 1))
Next ZZ
End Function
Private Function GetStrippedText(txt As String) As String
If txt = "–" Then
GetStrippedText = "–"
Else
Dim regEx As Object
Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = "[^\u0000-\u007F]"
GetStrippedText = regEx.Replace(txt, "")
End If
End Function
And this is what i get using it as formula in Excel. Note the difference in the Len of strings:
Hope this helps
You have characters that look like a space character, but are not. They are UniCode 8236 & 8237.
Just replace them with a space character (ASCII 32).
EDIT#1:
Based on the string in your post, the following VBA macro will replace UniCode characters 8236 amd 8237 with simple space characters:
Sub Kleanup()
Dim N1 As Long, N2 As Long
Dim Bad1 As String, Bad2 As String
N1 = 8237
Bad1 = ChrW(N1)
N2 = 8236
Bad2 = ChrW(N2)
Cells.Replace what:=Bad1, replacement:=" ", lookat:=xlPart
Cells.Replace what:=Bad2, replacement:=" ", lookat:=xlPart
End Sub

Is there a direct funtion to count numbers inside a string?

I have a string that contains digits inside. For example, "adf20j83n,m3jh2k9". Is there a direct way to count the number of digits inside the string. As in my example, it should give me "7" as an output.
Also, I have tried RegExp but it's not working in VBScript in QTP.
Btw, I'm not looking for loops and stuff like that. Just a direct way, or a suggestion to make this RegExp work in QTP.
You'll probably need to create the COM object via its ProgId:
Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\d"
re.Global = True
MsgBox "Digits: " & re.Execute("adf20j83n,m3jh2k9").Count
Output:
Digits: 7
I know it's techically what you said but it's a good way nonetheless
Try using this function:
Public Sub CountNumeric(ByVal input As String)
Dim numericCount As Integer = 0
For Each c As Char In input
If Char.IsDigit(c) Then numericCount += 1
Next
MessageBox.Show(String.Format("Number of numerics : {0}", numericCount)
End Sub
EDIT:
You could also try this:
Dim charColl As MatchCollection = Regex.Matches(input , "^\d+$")
Console.WriteLine(charColl.Count.ToString())

VBScript code to do math based on folder contents

I could use some help in troubleshooting my code, which should run a report based on files in a folder, which meet certain criteria, read specific lines from those files and sum all the read values. And do some math later on.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
relativePath = wshShell.CurrentDirectory
Set ReportPath = objFSO.GetFolder(relativePath & "\KPI_STATS\")
count = 0
Now I'd like to check each file's filename in ReportPath if it matches current month...
For Each file In ReportPath.files
If instr(LCase(file.name), LCase(Right("0" & DatePart("m",Now),2))) > 0 Then
for i=0 to n
SIDENOTE: Now I don't understand why if I run msgbox(file) for testing only it gives me whole path to the folder with filename instead of filename only.
(let's continue)...and when it does, it should read only 14th line of text (last line and there are only numbers in it) of each file and store it in variable. I use Split for reading certain lines from files and arrays to hold read values but I can use as well something like sum = sum + sum(n-1).
a = Split(objFSO.OpenTextFile(file, ForReading).ReadAll, vbCrLf)
For j = 14
If UBound(a) = j Then
And now I'd like to write those values to separate Arrays
ar(i) = Array(a(j))
End If
Next
next
count = count + 1
End If
Next
I think I will manage to add and divide all the values as soon as I have all arrays in place but there i a problem. It doesn't work :'(
Thanks a lot.

Extracting strings and writing to a new file

I was googling around but didn't find the right answer, perhaps people from here are willingly and able to help me.
I'm very new to VBS or WSH and I like to have a solution for this problem:
I'm searching for textstrings within a file without a line break (only one line). The textstrings I'm looking for start always with the same content "jpgline" and ends with the three letters "qbm". How can we extract each sentence (the strings are always 64 chars long) containg "jpgline....qbm" into a separate file.
I'm looking for a solution in Visual Basic Script as I use Windows 7.
Thanks in advance
M i k e
Use a regular expression:
Set re = New RegExp
re.Pattern = "^jpgline.*qbm$"
re.IgnoreCase = True
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile = fso.OpenTextFile("C:\path\to\input.txt")
Set outFile = fso.OpenTextFile("C:\path\to\output.txt", 2, True)
Do Until inFile.AtEndOfStream
line = inFile.ReadLine
If re.Test(line) Then outFile.WriteLine line
Loop
inFile.Close
outFile.Close
As your input file has no lines, use .ReadAll() to load its entire content into a string variable. Apply a RegExp to get all parts (Matches) defined by the pattern "jpgline.{N}qbm" where N is either 64 or 64 - the length of the pre/suffix. Ansgar has shown how to open and write to the output file.
Use the RegExp Docs to learn about .Execute and how to loop over the resulting match collection. The docs will tell you about .Test too.

vbscript - Replace all spaces

I have 6400+ records which I am looping through. For each of these: I check that the address is valid by testing it against something similar to what the Post Office uses (find address). I need to double check that the postcode I have pulled back matches.
The only problem is that the postcode may have been inputted in a number of different formats for example:
OP6 6YH
OP66YH
OP6 6YH.
If Replace(strPostcode," ","") = Replace(xmlAddress.selectSingleNode("//postcode").text," ","") Then
I want to remove all spaces from the string. If I do the Replace above, it removes the space for the first example but leave one for the third.
I know that I can remove these using a loop statement, but believe this will make the script run really slow as it will have to loop through 6400+ records to remove the spaces.
Is there another way?
I didn't realise you had to add -1 to remove all spaces
Replace(strPostcode," ","",1,-1)
Personally I've just done a loop like this:
Dim sLast
Do
sLast = strPostcode
strPostcode = Replace(strPostcode, " ", "")
If sLast = strPostcode Then Exit Do
Loop
However you may want to use a regular expression replace instead:
Dim re : Set re = New RegExp
re.Global = True
re.Pattern = " +" ' Match one or more spaces
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("O P 6 6 Y H.", "")
Set re = Nothing
The output of the latter is:
D:\Development>cscript replace.vbs
OP66YH.
OP66YH.
OP66YH.
D:\Development>
This is the syntax Replace(expression, find, replacewith[, start[, count[, compare]]])
it will default to -1 for count and 1 for start. May be some dll is corrupt changing the defaults of Replace function.
String.Join("", YourString.Split({" "}, StringSplitOptions.RemoveEmptyEntries))
Because you get all strings without spaces and you join them with separator "".

Resources