473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
0 1127

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

Similar topics

11
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file...
4
by: Cordula's Web | last post by:
Hello, here's a strange bug (?) I've came across (using Python 2.2): # loop_1 for line in file: if some_condition(line): break do_something() # loop_2
6
by: paul calvert | last post by:
I hope somewhere here has encountered and solved a similar problem in the past. 1) on a new Win2000 PC: installed Visual C++ 6.0 download & install single file Service Pack 5.0 2) try to...
4
by: JCO | last post by:
Can somebody help me move scripts into a separate file (ie JavaScript) so that I can simply call the script from the body... and have it execute. I've done simple (couple to several lines) ones...
4
by: Michael | last post by:
Hi When I New a web site, the default coding model is code-separation. I can uncheck the "place code in separate file" checkbox when I add a new WebForm, and VS2005 will remember this setting. ...
8
by: MLH | last post by:
Am trying to import 20,000+ lines of text in a file FTP'd from a UNIX platform to windows via FTP session in a DOS box. About 2000 records have multiple lines in them separated by CRLF's. ...
14
by: VincentWiebe | last post by:
Ok well this is the script that i am using to save a normal textbox. Private Sub CmdAdd_Click() Dim strNameScript As String strNameScript = "RuneScapePalScript" +...
4
by: Prodian | last post by:
The outputBox.Text is NOT getting double the lines, the Text file is getting every line of output double. The output is correct everything is working fine, but its writing everything twice in my log...
14
by: lashaw | last post by:
This program display a names. If the name you type match the one asked to type, the program tells you the phone number of the name. The problem is it only list the last name in the file. How to get...
2
by: Peter9588 | last post by:
I am trying to read in every line from a given file and check (amongst other things) that the 3rd character of every line is a ';' (a semi colon). The files i make to go into my database needs to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.