Write a program to create a file that consists of only 2 fields from another text file with 6? (VB2012) - vb.net

I'm using
Dim sw As IO.StreamWriter = IO.File.CreateText("Justices1.txt")
to create the file but I'm having trouble writing the code that will take only specific parts of the original "Justices.txt" file and put it into "Justices1.txt"
The first line of the original Justices file looks like this:
Henry,Baldwin,Andrew Jackson,PA,1830,1844
And I'm trying to get it to this in the file I'm creating (Justices1):
Henry Baldwin,PA
Sorry if this is a stupid question- I'm new to this.

I'd normally suggest using a StringBuilder but I figure your app won't be burning too many CPU cycles with something simple like this:
Private Sub FormatJusticesFile(filePath As String, formattedFilePath As String)
IO.File.WriteAllLines(formattedFilePath,
From line In IO.File.ReadAllLines(filePath)
Let terms = line.Split(","c)
Select "(" & IO.Path.GetFileNameWithoutExtension(filePath) & "): " & terms(0) & " " & terms(1) & "," & terms(3))
End Sub
Usage:
FormatJusticesFile("c:\Justices1.txt", "c:\Justices2.txt")

Related

Open a file in a new instance of program

All;
I have a bit of code I've written that opens a design blueprint when I scan a bar code. It works well enough, but I'd like to open a new instance of the design software (Solidworks) and have the print display in the new instance. Right now, no matter how many Solidworks instances I have open, the print will only open in the first instance started.
The line commented out below is the line that works, just not in the right instance. The line below that is what I'd expect to work, but it returns a 'file not found' even though the path to solidworks and the print path are both correct.
Any explanation as to why this isn't working would be much appreciated as I'm obviously very new at this...and have no idea what I'm doing.
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim barcode As String = tb_barcode.Text
Dim filename As String = tb_barcode.Text
'Add File Extension to end of path
Dim ext As String = ".SLDDRW"
'Split job number from detail number in barcode textbox
barcode = Split(tb_barcode.Text, ".")(0)
filename = Split(tb_barcode.Text, ".")(1)
'- This works, just in primary instance
'System.Diagnostics.Process.Start("G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext)
'- This does not work
System.Diagnostics.Process.Start("'C:\Program files\Solidworks Corp\Solidwork\SLDWORKS.exe' 'G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Catch
MessageBox.Show("File Not Found")
End Try
End Sub
Sorry for naive approach but shouldn't there be a comma in Process.Start between 2 arguments?
Start(String, String)
Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component. docs
Why don't you use the Application.ExecutablePath.That returns the Application's path with its full name. Then your code should be
System.Diagnostics.Process.Start(Application.Executablepath, "G:\Fixtures\" & barcode & "\Details\" & barcode & " DET " & filename & ext + "'")
Also make sure that the second string argument is a valid path.

VB.net SQL output loop rename file error on next loop

new to the stack here. I am developing some tools for work that allow us to pull information from a database and then operate on it with additional custom code. Since I need the output to not have a header, I want to have one file (as defined by schema.ini) that I will temporarily write the data into then copy and rename it before starting again. Using VS2017, when I debug it spits out the error that it cannot find RawC.txt after the first iteration. I cannot seem to figure out why. It is probably something simple but I have been unable to locate online. Can anyone help me out here? Below is the block I am having trouble with:
For each of the three files, output the sorted list. Schema has the correct format
For index = 0 To 2
If index = 0 Then
whicharr = arrSt(1)
ElseIf index = 1 Then
whicharr = arrSe(1)
ElseIf index = 2 Then
whicharr = arrVi(1)
End If
stopFile = "SELECT " & complist(index) & " INTO [Text;Database=" & TMPath & "].[RawC.txt] FROM [" & whicharr & "] ORDER BY " & complist(index)
cmd = New OleDbCommand(stopFile, conn)
cmd.ExecuteNonQuery()
'Now Copy and rename this file
My.Computer.FileSystem.CopyFile(TMPath & "\RawC.txt", TMPath & "\" & whicharr & ".txt")
'And delete the old RawC.txt file
My.Computer.FileSystem.DeleteFile(TMPath & "\RawC.txt")
Next
The final output from this loop should be three unique files that I will pass to another code that will perform some math on it.

Use combobox in savepath

first of all: Sorry for the not so clear title. I didn't know a better way to descripe my question.
I'm building a application that has to save user-specified data to a sdcard on a plc.
I already found out how to connect to that plc but am still working on the saving part.
For the testing i just used:
ds.WriteXml("C:\" & DateTimePicker1.Text & ".xml")
I think it's possible to change it to \192.168.2.16\SDcard\filename but that's not very flexible.
What i would like to have is the ability to take the value from a combobox and use that as the ip adress.
What is the best way to do this? as i don't think it's a simpe thing like making the savepad
(\" & comboIP.selectedvalue & "\Sdcard\" & DateTimePicker1.Text & ".xml") Unfortunately, the SD card is still on it's way so i can't test it yet..
Thanks in advance!
ds.WriteXml("C:\" & comboIP.Text & "\SDCard\" & DateTimePicker1.Text & ".xml")
That works just fine.
You don't really need the SDCard in hand to test this out.
You can simply create temporary variables before the WriteXML function call, set a breakpoint on them, and ensure that they are the correct values beforehand.
e.g.:
Dim sSelectedIP As String = comboIP.Text
Dim sDateTimePicker As String = DateTimePicker1.Text
Dim sCompleteDirectory As String = "C:\" & sSelectedIP & "\SDCard"
If My.Computer.FileSystem.DirectoryExists(sCompleteDirectory) = False Then
My.Computer.FileSystem.CreateDirectory(sCompleteDirectory)
End If
ds.WriteXml(sCompleteDirectory & "\" & sDateTimePicker & ".xml")

AppendText won't append to the next line in a text file / vb

I'm making a program that lets you add student info to an existing CSV text file but whenever I use append text it adds the new info to part of the last line and then a new line.
I want it to do this:
John Doe,29,Male
John Doe,29,Male
It does this instead:
John Doe,29,MaleJo
hn Doe,29,Male
Note: there isn't actually an empty line between each set of info, I just wanted it to be easy to read when I posted it here.
Here is the code for that section:
Dim swVar As IO.StreamWriter = IO.File.AppendText(frmMain.fileName)
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()
Any help would be appreciated, thanks!
A handy tool in VS2010 (and others) is snippets - right click Insert Snippets... - lots of code patterns for typical tasks. In your case here is a modified snippet:
Sub AddToFile(textToAdd As String, filePath As String)
My.Computer.FileSystem.WriteAllText(filePath, textToAdd, True)
End Sub
You may want to check/add a vbNewLine to the text being added since one is not automatically added as with WriteLine.
Not a direct reply to your stated problem, but an alternate method.
See if something like this works better:
IO.File.AppendText(frmMain.fileName, vbNewLine & txtName.Text & "," & txtAge.Text & "," & gender & vbNewLine)
AppendText will open write and close all in one operation so there's no need for a separate streamwriter. It also doesn't add newlines so those must be added separately
Unless of course you are doing a series of writes to the same file then something like this would probably be more appropriate:
Dim swVar As New IO.StreamWriter(frmMain.fileName, True)
'write your lines here
swVar.WriteLine(txtName.Text & "," & txtAge.Text & "," & gender)
swVar.Close()

Issue with an LPR Command in VB

I am creating a VB app which will "move" xls reports from a directory to a ReportSafe app. I am also working in an existing VB app which does just that, so I am using it for reference.
It isn't as simple as moving files from one directory to another, because ReportSafe requires an lpr command to tell it (ReportSafe) which file to pick up.
Here is what I have so far:
Imports System.IO
Module Module1
Sub Main()
''Declarations
Dim Files As ArrayList = New ArrayList()
Dim FileName As String
''Write All Files in *directory* to ReportSafe
Files.Clear()
Files.AddRange(Directory.GetFiles(*directory*))
For Each FileName In Files
Dim RPname As String
Dim RealName As String
RPname = FileName.ToString
RealName = "/"
RealName = RealName & RPname.Remove(0, 34)
Dim a As New Process
a.StartInfo.FileName = "C:\Windows\system32\lpr.exe"
a.StartInfo.Arguments = "-S*ServerName* -Plp -J" & Chr(34) & RealName & Chr(34) & " " & Chr(34) & RPname & Chr(34)
a.StartInfo.UseShellExecute = False
Next
End Sub
End Module
The whole lpr command/arguments are throwing me for a loop. I'm not sure if my question is specific to ReportSafe, and if that's the case, I may be out of luck here. I have pulled this code from the already existing app which moves reports to ReportSafe, and adjusted for my own use, but no luck so far.
FYI, I had to turn on LPR Monitor services to obtain to the lpr.exe
Questions:
What are the proper arguments to pass through to this lpr command?
Is there a problem with the logic that is causing the issue?
I continued to tinker and look at my reference code and discovered some flaws in logic:
For one, the report name I was passing did not include the complete file path.
Another thing is that I never started the process with a.Start(). Rookie mistakes for sure... haha