Avatar of pogosru
pogosru
 asked on

Text file containing 8 characters needs to changed to text file containing 4 characters

I have a text file containing a list of 8 characters that needs to be changed to a list of 4 characters. Each 8 character line needs to be split in half and the right half needs to be placed on the following line.

start file example
00010203
04050607
08090a0b
0c0d0e0f
10111213

end file example
0001
0203
0405
0607
0809
0a0b
0c0d
0e0f
1011
1213
test.txt
Microsoft Development

Avatar of undefined
Last Comment
pogosru

8/22/2022 - Mon
_tom_tom_tom_

The easiest way to do this is by using a program called 'notepad++' and recording a marco, you can find your example textfile split in lines of 4 chars attached.
test.txt
silemone

if they all have 8, use substring method.  I guess you could place in a hashmap as a key/value pair so you can then print them out.  you could also just place in a stringbuilder in the order that they were separated.



Here is the substring method:

myString.Substring(0, 4)
myString.Substring(4, 8)
margajet24


            StreamReader rdr = new StreamReader(@"C:\test.txt");
 
            List<string> data = new List<string>();
            string line;
            while (!rdr.EndOfStream)
            {
                line = rdr.ReadLine();
 
                data.Add(line.Substring(0, 4));
            }
 
            rdr.Close();
 
            StreamWriter wrtr = new StreamWriter(@"C:\test2.txt");
 
            foreach (string s in data)
            {
                wrtr.WriteLine(s);
            }
 
            wrtr.Flush();
            wrtr.Close();

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
silemone

Imports System
Imports System.IO
Imports System.Collections
                  
Dim arrText As New ArrayList()
Dim separatedText As New ArrayList()
Sub Load()
Dim objReader As New StreamReader("c:\test.txt")  'place your own address in
                              
Dim sLine As String = ""
       ' to store input from files                        

Do
    sLine = objReader.ReadLine()
    If Not sLine Is Nothing Then
        arrText.Add(sLine)
    End If
Loop Until sLine Is Nothing
objReader.Close()
End Sub

Sub splitInput()

    For each string x in ArrText
        Dim Temp As String = (String)x
        Dim firstFour As String = Temp.SubString(0,4)
        Dim secondFour As String = Temp.SubString(4, 4)
       
       arrText.Add(firstFour)
       arrText.Add(secondFour)
   Next
End Sub

then write back to file

                              
silemone

mara, he's a VB guy
silemone

oopss
marga, he's a vb guy...excuse typo
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
margajet24

yeah.. i know..

got few prob with my IDE

here's the VB version
		Dim rdr as System.IO.StreamReader = new System.IO.StreamReader("C:\test.txt")
 		Dim data as List(Of string) = new List(Of string)
 
        Dim line As String
        
        Do 
        	    line = rdr.ReadLine()
 
                data.Add(line.Substring(0, 4))
        Loop While (Not rdr.EndOfStream) 
 
        rdr.Close()
        
 		Dim wrtr As System.IO.StreamWriter = New System.IO.StreamWriter("C:\test2.txt")
 		
 		dim s as String
 		For Each s In data
 			wrtr.WriteLine(s)	
 		Next
 		
 		
 
        wrtr.Flush()
        wrtr.Close()

Open in new window

ASKER CERTIFIED SOLUTION
margajet24

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
pogosru

ASKER
Perfect! Thanks margajet24.
pogosru

ASKER
Exactlay what I wanted. Thanks!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23