I am currently working on an EDI file and now I want to load and read only a specific line on a .txt file and put that in a textbox. Here is what my data would look like
ISA*00* *00* *01*MKT71 *01*ADEV04 *160331*1001*U*00501*300000001*0*P*>~
GS*IN*MKT71*ADEV04*20160331*1001*300000001*X*005010~
ST*810*300000001~
BIG*20160316*BS13435**NONE~
REF * BM * DHL-2162693540~
REF*CR*5201~
Now I want to put only the data DHL-2162693540 located in line number 5 of the .txt file in my Textbox1.
The easiest and most straightforward way to do this is to read the first however many lines, and only keep the value of the last line read.
Here is some code that accomplishes that:
Private Sub ReadLines()
Dim MyLine As String
Dim DataLineNumber As Integer
DataLineNumber = 5
Open "C:\YourFileNameHere.txt" For Input As DataFile
Do While Not EOF(1) And LineNumber <= DataLineNumber
Line Input #DataFile, MyLine
DataLineNumber = DataLineNumber + 1
Loop
Close DataFile
MsgBox MyLine
End Sub
Related
I have a VBScript:
Dim Stuff, myFSO, WriteStuff, dateStamp
Stuff = "Whatever you want written"
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("C:\Label_1\yourtextfile.txt", 8, True)
WriteStuff.WriteLine(var1)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
which is placed in action in the key.
In the variable "var1" is read barcode reader, bar code EAN13 and after pressing a key to a text file "C:\Label_1\yourtextfile.txt"
is written to a new line with a value of "var1", ie. the bar code
2914750018247
Then again, when we will scan the bar code
2914750007463
and press the button
also will be saved in a text file.
Recording will look like this:
2914750018247
2914750007463
Of course, the scanned file "C:\Label_1\yourtextfile.txt" will be more, eg. 70 different codes but always EAN13.
How you can using VBScript copy or distribute 5 characters namely:
01824
00746
...
with previously stored all values (5 characters each) in the file "C:\Label_1\ yourtextfile.txt" and yet they all add up and save a new file txt when codes (with five characters each) will be just 70 in line?
Take a look at the below example, it processes the lines of source file and cut each line to substring:
sSrc = "C:\Users\DELL\Desktop\barcode.txt"
sDst = "C:\Users\DELL\Desktop\barcode_part.txt"
' Read content of the source file
sCont = ReadTextFile(sSrc, 0) ' ASCII
' Split source file string into array of lines
aLines = Split(sCont, vbCrLf)
' Loop through each of the lines in array
For i = 0 To UBound(aLines)
' Change the value of the element to cut substring
aLines(i) = Mid(aLines(i), 8, 5)
Next
' Join processed array into resulting string with line breaks
sCont = Join(aLines, vbCrLf)
' Write content to the destination file
WriteTextFile sCont, sDst, 0 ' ASCII
Function ReadTextFile(sPath, lFormat)
' lFormat -2 - System default, -1 - Unicode, 0 - ASCII
With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 1, False, lFormat)
ReadTextFile = ""
If Not .AtEndOfStream Then ReadTextFile = .ReadAll
.Close
End With
End Function
Sub WriteTextFile(sContent, sPath, lFormat)
' lFormat -2 - System default, -1 - Unicode, 0 - ASCII
With CreateObject("Scripting.FileSystemObject").OpenTextFile(sPath, 2, True, lFormat)
.Write sContent
.Close
End With
End Sub
I made a counter that works when you press the button as the action in VBScript.
my code:
Licznik_ID = Licznik_ID + 1
Dim Stuff, myFSO, WriteStuff, dateStamp
Stuff = "Whatever you want written"
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("c:\tmp\yourtextfile.txt", 2, True)
WriteStuff.WriteLine(Licznik_ID)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
the counter variable named "Licznik_ID"
indicated by the arrow.
It is written to the file "c:\tmp\yourtextfile.txt"
And it works well.
For each time the number increases by one and is replaced and stored in the txt file.
The file contains the number 1 and the increase in the txt file appears the number 2 and so on ...
How now download the data stored with the file "c: \tmp\yourtextfile.txt" back to the variable to use it in such a way that when you start NiceForm or button has been loaded with the contents of txt file into a variable?
Set myFSO = CreateObject("Scripting.FileSystemObject")
Licznik_ID = myFSO.OpenTextFile("C:\tmp\yourtextfile.txt").ReadAll
Licznik_ID = Licznik_ID + 1
myFSO.OpenTextFile("C:\tmp\yourtextfile.txt",2,True).Write(Licznik_ID)
FSO is kinda weird the way it does its file modes sometimes.
edit: If you want to allow this to create the file without errors if it doesn't exist, replace line 2 with this:
If myFSO.FileExists("C:\tmp\yourtextfile.txt") Then
Licznik_ID = myFSO.OpenTextFile("C:\tmp\yourtextfile.txt").ReadAll
End If
If the file doesn't exist, Licznik_ID will be Empty. Empty + 1 = 1 in vbscript.
I've got a text file of output that looks essentially like this:
SMITHERSON, SMITH|00012345|15-Jan-1999|000885340
619649339|29-Sep-2015 00:09:30|Black|JOHNERSON, JOHN
00067890|02-Dec-1996|000490365|620094551
29-Sep-2015 23:06:01|Green|DAVISON, DAVE|00086543|06-Jun-2001|000938585
226438332|28-Sep-2015 00:12:12|Yellow
Seven pieces of data, they are always in the correct order but unfortunately they run together and onto different lines. There are carriage return + line feeds at the end of each line and there aren't pipe delimiters. The individual pieces of data are never split over multiple lines - I'm having a hard time explaining so here's another example:
DATA 1|DATA 2|DATA 3
DATA 4
DATA 5|DATA 6|DATA 7
DATA 1|DATA 2|DATA 3|DATA 4
DATA 5|DATA 6|DATA 7
etc...
They will have spaces between them but each piece of data will always stay on it's own line.
And I'm trying to turn it into this:
SMITHERSON, SMITH|00012345|15-Jan-1999|000885340|619649339|29-Sep-2015 00:09:30|Black
JOHNERSON, JOHN|00067890|02-Dec-1996|000490365|620094551|29-Sep-2015 23:06:01|Green
DAVISON, DAVE|00086543|06-Jun-2001|000938585|226438332|28-Sep-2015 00:12:12|Yellow
DATA 1|DATA 2|DATA 3|DATA 4|DATA 5|DATA 6|DATA 7
DATA 1|DATA 2|DATA 3|DATA 4|DATA 5|DATA 6|DATA 7
etc.
Seven pieces of data each on their own line, but still seperated by the '|' for another piece of software to read correctly.
I am spending about one hour every day correcting the text files by hand, so I've been trying to find an example I can work from to do this for a while but have not had any luck wrapping my head around this.
This code is ok. I only tested your sample text, not big files.
It will replace line feeds with the delimiter, then convert the entire file into one big array:
Set fso = CreateObject("Scripting.FileSystemObject")
Set input = fso.OpenTextFile("input.txt", 1)
Set output = fso.OpenTextFile("output.txt", 2, True)
Dim data: data = input.ReadAll
input.Close()
data = Replace(data, vbCrlf, "|")
data = Split(data, "|")
For i=0 To UBound(data) Step 7
output.WriteLine data(i) & "|" & data(i+1) & "|" & data(i+2) & "|" & data(i+3) & "|" & data(i+4) & "|" & data(i+5) & "|" & data(i+6)
Next
output.Close()
Untested, but something like this might do it. (Essentially it copies input to output as a stream, but newlines in the input are converted to pipe characters and every seventh pipe in the output is converted to a newline)
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("D:\data\thefile.txt", 1)
Set o = fs.OpenTextFile("D:\data\combined.txt", 2, True)
pipecount = 0
Do While f.AtEndOfFile <> True
If f.AtEndOfLine = True Then
c = f.Read(2) ' Skip the CR+LF
c = "|" ' and pretend we got a pipe character
Else
c = f.Read(1)
End If
If c = "|" Then
pipecount = pipecount + 1
If pipecount = 7 Then
pipecount = 0
o.WriteLine()
Else
o.Write("|")
End If
Else
o.Write(c)
End If
End While
o.Close()
For my assignment I am making a ball game in visual basic where the player gets points for hitting the ball. I am having an issue with saving and loading a number/the highest score to a .txt file. So far, I have managed to save the name of the player and the high score to a .txt file, however it only saves/loads numbers from 0-9 and doesn't allow 2+ digit numbers. Thanks in advance. Below is the code I have:
To save to text file
Dim FileNum As Integer
'if the player's score (lblpoints) is greater than the high score (lblhighscore)
If lblpoints.Text > lblhighscore.Text Then
lblnewhigh.Visible = True
lblhighscore.Text = lblpoints.Text
lblbestname.Text = lblplyrname.Text
'to save highscore to txt file
FileNum = FreeFile()
FileOpen(FileNum, "score.txt", OpenMode.Output)
PrintLine(FileNum, lblbestname.Text)
PrintLine(FileNum, lblhighscore.Text)
FileClose(FileNum)
End If
To load from text file
Dim FileNum As Integer
'to fetch the highscore from txt file
FileNum = FreeFile()
FileOpen(FileNum, "score.txt", OpenMode.Input)
lblbestname.Text = LineInput(FileNum)
lblhighscore.Text = LineInput(FileNum)
FileClose(FileNum)
I want to only display numbers in a text box that i have. At the moment my code reads the text file and adds all the code to the textbox and not only the needed text(which are numbers).
tbRecipient.Text = My.Computer.FileSystem.ReadAllText("filepath")
if anyone can point me in the right direction and let me know how i would go around this problem, that would be great.
I have a file containing:
Steve, 017876
Alan, 098578
...
I want to list only the numbers into a text box once i have got them from the file.
To do this i am using:
Dim i As Integer
For i = 0 To cbRecipients.CheckedItems.Count - 1
My.Computer.FileSystem.WriteAllText("filepath", cbRecipients.CheckedItems.Item(i) & vbCrLf, True)
Next
frmHome.myFunction()
Then under myFunction() is:
tbRecipient.Text = My.Computer.FileSystem.ReadAllText("filepath")
You can use this function:
Function GetFileColumnContents(s_Path As String, ColumnNumber As Long, ColumnDelimiter As String, Optional s_OutputDelimiter As String) As String
On Error GoTo ErrHandler
Open s_Path For Input As #1 'Open the txt file for readin as Temporary File Number 1
Do While Not EOF(1) 'Read line bu line until end of file
Line Input #1, Mystring 'Store the line value in Mystring
GetFileColumnContents = GetFileColumnContents & s_OutputDelimiter & Split(Mystring, ColumnDelimiter)(ColumnNumber - 1) 'process the string
Loop
ErrHandler:
Close #1
End Function
Call in subrutine:
tbRecipient.Text = GetFileColumnContents("filepath", 2, ",", vbCrLf)
Edit: The linenum = linenum + 1 was not neccesary in the function (edited above)
s_Path : is path to the txt file (like "C:\Test.txt")
ColumnNumber: is the column in the text file. If the data looks like:
A, 123, red
B, 456, blue
then ColumnNumber 1 are letters, ColumnNumber 2 are numbers and 3 are colors.
(ColumnNumber - 1) is there because first part of split has index 0, but the ColumnNumber is 1
split("A, 123, red",",")(0) 'results to "A"
split("A, 123, red",",")(1) 'results to " 123"
If you want to list the items from text file column 2 and in the result separated by comma then call the function with comma as last argument:
tbRecipient.Text = GetFileColumnContents("filepath", 2, ",", ",")