Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
i have file which i want to divide into 60 files and i want to create folder after every 6 files.

What I have tried:

Dim s As String = IO.File.ReadAllText("C:\Users\mks\Desktop\Newfolder\P6.A3")
       Dim counter As Integer = 0
       Dim wFile As System.IO.FileStream
       Dim byteData(), byteDatarp1() As Byte
       Dim rp As String
       Dim r As String = Regex.Replace(s.ToString, "[^1-9Z-]", "")
       '    Console.WriteLine(r)
       r = Regex.Replace(r.ToString, "[-]", "0")
       Dim elements() As String = Regex.Split(r, "Z")

       For Each element In elements
           '  Console.WriteLine(elements(counter))
           rp = elements(counter)
           byteData = Encoding.ASCII.GetBytes(rp)


           If Not Directory.Exists(Application.StartupPath + "\sample" + counter.ToString) Then

               Directory.CreateDirectory(Application.StartupPath + "\sample" + counter.ToString)

           End If

           Dim abc As String = Application.StartupPath + "\test\r" + counter.ToString + ".mod"
           Dim abc1 As String = Application.StartupPath + "\test\ra" + counter.ToString + ".mod"
           System.IO.File.WriteAllText(abc, "")
           wFile = New FileStream(abc, FileMode.Append)
           wFile.Write(byteData, 0, byteData.Length)
           wFile.Close()

           counter += 1
           For i As Integer = 0 To s.Length - 1 Step 1
               If i Mod 1 = 0 Then


               End If
               Try
                   If counter Mod 2 > 0 Then
                       Dim rp1 As String = i / 10 & " " & rp.Substring(i, 1) & Environment.NewLine
                       byteDatarp1 = Encoding.ASCII.GetBytes(rp1)
                       wFile = New FileStream(abc1, FileMode.Append)
                       wFile.Write(byteDatarp1, 0, byteDatarp1.Length)
                       wFile.Close()

                   Else
                       Dim rp1 As String = i / 10 & " -" & rp.Substring(i, 1) & Environment.NewLine
                       byteDatarp1 = Encoding.ASCII.GetBytes(rp1)
                       wFile = New FileStream(abc1, FileMode.Append)
                       wFile.Write(byteDatarp1, 0, byteDatarp1.Length)
                       wFile.Close()
                   End If


               Catch ex As ArgumentOutOfRangeException
                   Exit For
               Finally
               End Try

           Next


       Next

       MsgBox("FIlE HAS BEEN PROCEESED")
   End Sub
Posted
Updated 12-Feb-18 20:06pm
Comments
Maciej Los 13-Feb-18 1:57am    
And what's your issue?

1 solution

If you want to group files at 6 files in each group, you have to check if Modulo[^] of 6 returns zero. For example:

VB.NET
Dim countOfFiles = 60
Dim iCounter = 0
Dim iGroup = 0

Do While (iCounter<60)
	If (iCounter Mod 6)=0 Then iGroup +=1 
	Console.WriteLine("File: {0}, group: {1}", iCounter, iGroup)
	iCounter +=1
Loop


Above code prints on screen:
File: 0, group: 1
File: 1, group: 1
File: 2, group: 1
File: 3, group: 1
File: 4, group: 1
File: 5, group: 1
File: 6, group: 2
File: 7, group: 2
...
File: 52, group: 9
File: 53, group: 9
File: 54, group: 10
File: 55, group: 10
File: 56, group: 10
File: 57, group: 10
File: 58, group: 10
File: 59, group: 10


Got it?
 
Share this answer
 
v3
Comments
Member 13307200 13-Feb-18 5:00am    
Thank you very much sir. its working.. did some changes
Maciej Los 13-Feb-18 5:22am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900