I have managed to read the csv file that contains hundreds of lines(delivery orders). My intent is to read every line(delivery order) from the csv file and save the files separately then encrypt them differently.
In each delivery order, there is delivery order number, buyer's code and warehouse's code and much more. I have managed to pick the trio from the first row and saved one file using a concatenation of the three.
My challenge is doing that for all the rows and saving in different files.
The encryption algorithm takes one files and gives two file outputs. One output file should be named 'buyer_code.ext' and the other 'warehouse_code.ext'
Please with the saving of the iteration and saving each file.
Thanks.
This is my code so far:
splitting Button_click:
Expand|Select|Wrap|Line Numbers
- Private Sub SplitFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SplitFolder.Click
- SplitFolder.Font = New Font(SplitFolder.Font, FontStyle.Italic)
- Dim openFileDialog1 As New OpenFileDialog()
- 'Setup the open dialog.
- openFileDialog1.FileName = ""
- openFileDialog1.Title = "Choose a file to encrypt"
- openFileDialog1.InitialDirectory = "C:\"
- openFileDialog1.Filter = "All Files (*.*) | *.*"
- 'Find out if the user chose a file.
- If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
- MainModule.strFileToEncrypt = openFileDialog1.FileName
- txtDestinationEncrypt.Text = MainModule.strFileToEncrypt
- Dim iPosition As Integer = 0
- Dim i As Integer = 0
- 'Get the position of the last "\" in the OpenFileDialog.FileName path.
- '-1 is when the character your searching for is not there.
- 'IndexOf searches from left to right.
- While MainModule.strFileToEncrypt.IndexOf("\"c, i) <> -1
- iPosition = MainModule.strFileToEncrypt.IndexOf("\"c, i)
- i = iPosition + 1
- End While
- Dim sr As StreamReader = New StreamReader(MainModule.strFileToEncrypt)
- Dim strLine As String = String.Empty
- Do While sr.Peek() >= 0
- strLine = String.Empty
- strLine = sr.ReadLine
- Dim reader As StringReader = New StringReader(strLine.ToString())
- Dim currentRow(1000) As String
- Dim j As Integer
- 'For index As Integer = 0 To strLine.Length - 1
- currentRow(j) = reader.ReadLine()
- MessageBox.Show(currentRow(0))
- ' MessageBox.Show(currentRow(1))
- Dim currentField As String = currentRow(j)
- Dim delivery_order_number As String = currentField.Split(",")(0)
- delivery_order_number = delivery_order_number.Replace("""", "").Trim()
- delivery_order_number = delivery_order_number.Replace("/", "").Trim()
- MsgBox(delivery_order_number)
- Dim buyer_code As String = currentField.Split(",")(1)
- buyer_code = buyer_code.Replace("""", "").Trim()
- MsgBox(buyer_code)
- Dim warehouse_code As String = currentField.Split(",")(2)
- warehouse_code = warehouse_code.Replace("""", "").Trim()
- MsgBox(warehouse_code)
- txtFileDestination.Text = delivery_order_number + "_" + buyer_code + "_" + warehouse_code
- ' Set a variable to the My Documents path.
- Dim path As String = Directory.GetCurrentDirectory
- Dim outputFile As String = path & "\" & txtFileDestination.Text
- MsgBox(outputFile)
- If System.IO.File.Exists(outputFile) = False Then
- System.IO.File.Create(outputFile).Dispose()
- End If
- Dim objWriter As New System.IO.StreamWriter(outputFile, True)
- objWriter.WriteLine(currentRow(j))
- objWriter.Close()
- j += 1
- Exit Do
- Loop
- End If
- End Sub
Expand|Select|Wrap|Line Numbers
- Private Sub EncryptString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EncryptString.Click
- EncryptString.Font = New Font(EncryptString.Font, FontStyle.Italic)
- Call SplitFolder_Click(sender, e)
- strFileToEncrypt = txtFileDestination.Text
- txtDestinationEncrypt.Text = strFileToEncrypt
- ' Debug.Print(strFileToEncrypt)
- Dim iPosition As Integer = 0
- Dim i As Integer = 0
- 'Get the position of the last "\" in the OpenFileDialog.FileName path.
- '-1 is when the character your searching for is not there.
- 'IndexOf searches from left to right.
- ' While strFileToEncrypt.IndexOf("\"c, i) <> -1
- 'iPosition = strFileToEncrypt.IndexOf("\"c, i)
- ' i = iPosition + 1
- ' End While
- '
- strOutputEncrypt = strFileToEncrypt.Substring(iPosition + 1)
- strOutputEncrypt2 = strFileToEncrypt.Substring(iPosition + 1)
- 'Assign S the entire path, ending at the last "\".
- Dim S As String = strFileToEncrypt.Substring(0, iPosition + 1)
- 'Replace the "." in the file extension with "_".
- strOutputEncrypt = Me.strOutputEncrypt.Replace("."c, "_"c)
- strOutputEncrypt2 = Me.strOutputEncrypt2.Replace("."c, "_"c)
- 'The final file name. XXXXX.encrypt
- txtDestinationEncrypt.Text = S + Me.strOutputEncrypt + ".encrypt"
- txtDestinationEncrypt2.Text = S + Me.strOutputEncrypt2 + "_encrypted" + ".txt"
- 'End If
- Dim txtPassEncrypt As String = "this_is_the_key"
- txtFileToEncrypt.Text = txtPassEncrypt
- 'Start the timer'
- Dim start_time As Date = Date.Now
- 'Declare variables for the key and iv.
- 'The key needs to hold 256 bits and the iv 128 bits.
- Dim bytKey As Byte()
- Dim bytIV As Byte()
- 'Send the password to the CreateKey function.
- bytKey = CreateKey(txtPassEncrypt)
- 'Send the password to the CreateIV function.
- bytIV = CreateIV(txtPassEncrypt)
- 'Start the encryption.
- EncryptOrDecryptFile(strFileToEncrypt, txtDestinationEncrypt.Text, _
- bytKey, bytIV, CryptoAction.ActionEncrypt)
- EncryptOrDecryptFile(strFileToEncrypt, txtDestinationEncrypt2.Text, _
- bytKey, bytIV, CryptoAction.ActionEncrypt)
- 'End the timer'
- Dim stop_time As Date = Date.Now
- Dim elapsed_time As TimeSpan = stop_time.Subtract(start_time)
- MsgBox(elapsed_time.ToString())
- End Sub