By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 1,998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

How do i loop an array(csv) and save every line in separate file

P: 7
I working on a system that encrypts and decrypts delivery orders from the sender to the recipient.

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
  1. Private Sub SplitFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SplitFolder.Click
  2.         SplitFolder.Font = New Font(SplitFolder.Font, FontStyle.Italic)
  3.         Dim openFileDialog1 As New OpenFileDialog()
  4.         'Setup the open dialog.
  5.         openFileDialog1.FileName = ""
  6.         openFileDialog1.Title = "Choose a file to encrypt"
  7.         openFileDialog1.InitialDirectory = "C:\"
  8.         openFileDialog1.Filter = "All Files (*.*) | *.*"
  9.  
  10.         'Find out if the user chose a file.
  11.         If openFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
  12.             MainModule.strFileToEncrypt = openFileDialog1.FileName
  13.             txtDestinationEncrypt.Text = MainModule.strFileToEncrypt
  14.  
  15.             Dim iPosition As Integer = 0
  16.             Dim i As Integer = 0
  17.  
  18.             'Get the position of the last "\" in the OpenFileDialog.FileName path.
  19.             '-1 is when the character your searching for is not there.
  20.             'IndexOf searches from left to right.
  21.             While MainModule.strFileToEncrypt.IndexOf("\"c, i) <> -1
  22.                 iPosition = MainModule.strFileToEncrypt.IndexOf("\"c, i)
  23.                 i = iPosition + 1
  24.             End While
  25.  
  26.             Dim sr As StreamReader = New StreamReader(MainModule.strFileToEncrypt)
  27.             Dim strLine As String = String.Empty
  28.  
  29.             Do While sr.Peek() >= 0
  30.                 strLine = String.Empty
  31.                 strLine = sr.ReadLine
  32.                 Dim reader As StringReader = New StringReader(strLine.ToString())
  33.                 Dim currentRow(1000) As String
  34.                 Dim j As Integer
  35.  
  36.                     'For index As Integer = 0 To strLine.Length - 1
  37.                 currentRow(j) = reader.ReadLine()
  38.                         MessageBox.Show(currentRow(0))
  39.                         ' MessageBox.Show(currentRow(1))
  40.                 Dim currentField As String = currentRow(j)
  41.                     Dim delivery_order_number As String = currentField.Split(",")(0)
  42.                     delivery_order_number = delivery_order_number.Replace("""", "").Trim()
  43.                     delivery_order_number = delivery_order_number.Replace("/", "").Trim()
  44.  
  45.                     MsgBox(delivery_order_number)
  46.                     Dim buyer_code As String = currentField.Split(",")(1)
  47.                     buyer_code = buyer_code.Replace("""", "").Trim()
  48.                     MsgBox(buyer_code)
  49.                     Dim warehouse_code As String = currentField.Split(",")(2)
  50.                     warehouse_code = warehouse_code.Replace("""", "").Trim()
  51.                     MsgBox(warehouse_code)
  52.  
  53.                     txtFileDestination.Text = delivery_order_number + "_" + buyer_code + "_" + warehouse_code
  54.                     ' Set a variable to the My Documents path.
  55.                     Dim path As String = Directory.GetCurrentDirectory
  56.                     Dim outputFile As String = path & "\" & txtFileDestination.Text
  57.                     MsgBox(outputFile)
  58.                     If System.IO.File.Exists(outputFile) = False Then
  59.                         System.IO.File.Create(outputFile).Dispose()
  60.                     End If
  61.                     Dim objWriter As New System.IO.StreamWriter(outputFile, True)
  62.                     objWriter.WriteLine(currentRow(j))
  63.                     objWriter.Close()
  64.  
  65.                 j += 1
  66.                 Exit Do
  67.  
  68.             Loop
  69.  
  70.         End If
  71.     End Sub
Encryption button:
Expand|Select|Wrap|Line Numbers
  1. Private Sub EncryptString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EncryptString.Click
  2.         EncryptString.Font = New Font(EncryptString.Font, FontStyle.Italic)
  3.         Call SplitFolder_Click(sender, e)
  4.  
  5.         strFileToEncrypt = txtFileDestination.Text
  6.         txtDestinationEncrypt.Text = strFileToEncrypt
  7.         ' Debug.Print(strFileToEncrypt)
  8.  
  9.         Dim iPosition As Integer = 0
  10.         Dim i As Integer = 0
  11.  
  12.         'Get the position of the last "\" in the OpenFileDialog.FileName path.
  13.         '-1 is when the character your searching for is not there.
  14.         'IndexOf searches from left to right.
  15.         ' While strFileToEncrypt.IndexOf("\"c, i) <> -1
  16.         'iPosition = strFileToEncrypt.IndexOf("\"c, i)
  17.         ' i = iPosition + 1
  18.         '  End While
  19.         '
  20.         strOutputEncrypt = strFileToEncrypt.Substring(iPosition + 1)
  21.         strOutputEncrypt2 = strFileToEncrypt.Substring(iPosition + 1)
  22.  
  23.         'Assign S the entire path, ending at the last "\".
  24.         Dim S As String = strFileToEncrypt.Substring(0, iPosition + 1)
  25.  
  26.         'Replace the "." in the file extension with "_".
  27.         strOutputEncrypt = Me.strOutputEncrypt.Replace("."c, "_"c)
  28.         strOutputEncrypt2 = Me.strOutputEncrypt2.Replace("."c, "_"c)
  29.         'The final file name.  XXXXX.encrypt
  30.         txtDestinationEncrypt.Text = S + Me.strOutputEncrypt + ".encrypt"
  31.         txtDestinationEncrypt2.Text = S + Me.strOutputEncrypt2 + "_encrypted" + ".txt"
  32.         'End If
  33.         Dim txtPassEncrypt As String = "this_is_the_key"
  34.         txtFileToEncrypt.Text = txtPassEncrypt
  35.  
  36.  
  37.         'Start the timer'
  38.         Dim start_time As Date = Date.Now
  39.  
  40.         'Declare variables for the key and iv.
  41.         'The key needs to hold 256 bits and the iv 128 bits.
  42.         Dim bytKey As Byte()
  43.         Dim bytIV As Byte()
  44.         'Send the password to the CreateKey function.
  45.         bytKey = CreateKey(txtPassEncrypt)
  46.         'Send the password to the CreateIV function.
  47.         bytIV = CreateIV(txtPassEncrypt)
  48.  
  49.         'Start the encryption.
  50.         EncryptOrDecryptFile(strFileToEncrypt, txtDestinationEncrypt.Text, _
  51.                              bytKey, bytIV, CryptoAction.ActionEncrypt)
  52.         EncryptOrDecryptFile(strFileToEncrypt, txtDestinationEncrypt2.Text, _
  53.                                  bytKey, bytIV, CryptoAction.ActionEncrypt)
  54.  
  55.         'End the timer'
  56.         Dim stop_time As Date = Date.Now
  57.         Dim elapsed_time As TimeSpan = stop_time.Subtract(start_time)
  58.         MsgBox(elapsed_time.ToString())
  59.  
  60.  
  61.     End Sub
Apr 26 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.