Create a loop on a Notepad file

I have a Notepad file that at column 161 starts a new record.  What I need is to insert a return at column 160 for each row so the record is in the correct position.  Basically, every row really has two rows of data on it.  Of course, I need the code to then go to the next row and insert a return again at 160.  However, can VBA be used against a Notepad file?  What language would be used and can this even be done?
Sandra SmithRetiredAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
What applications do you have that run VBA?
If you have it, you can use Word to open the file and use all of Word's VBA methods.
You'll need some code like this:

Sub TextFile()
    Dim doc As Document
    Dim para As Paragraph
    Dim rng As Range
    Dim rng2 As Range
    Dim p As Integer
   
    Set doc = Documents.Open("C:\MyFolder\MyFile.txt")
    For p = doc.Range.Paragraphs.Count To 1 Step -1 'move backwards since the changes will disturb later numbering
        Set rng = doc.Range.Paragraphs(p).Range
        If rng.Characters.Count > 119 Then
            Set rng2 = rng.Characters(120)
            rng2.Collapse wdCollapseEnd
            rng2.Text = vbCr
        End If
    Next p
   
    doc.SaveAs Replace(doc.FullName, ".", "_new."), wdFormatDOSTextLineBreaks
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This depends on the file contents. Is it ASCII (versus being UTF16 / "Unicode") indeed?
In general, you open the text file, read a line, and write the first 160 chars as a line to a new file, then write the next 160 chars.

In PowerShell (which is most simple here) this would look this:
$file = C:\Temp\OriginalFile.txt
(get-content $file) | % {
  Write-Output $_.SubString(0, 160)
  Write-Output $_.Substring(160)
} | Out-File -Encoding ASCII $file

Open in new window

It is very similar in VBA.
0
Sandra SmithRetiredAuthor Commented:
It is a text file. Will try both suggestions and see what happens
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Sandra SmithRetiredAuthor Commented:
I have all of MS Office and want this data to end up in an ACCESS 2010 database.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Do you want to have each (new) line as an own row in Access? In that case I would combine all processes required, and use VBA code similar to above.
0
Sandra SmithRetiredAuthor Commented:
With a slight modification, this works perfectly, thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.