Solved

edit multiple text files

Posted on 2011-02-23
8
307 Views
Last Modified: 2012-05-11
this is two parts, I want to delete the first 5 rows in every text file in directory c:\path\textfiles then add "Title" to the first line. there are 318 text files all with different names.
0
Comment
Question by:ntr2def
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 10

Expert Comment

by:cdebel
ID: 34966091
Here's your code.  Call this from your macro...

Make sure that you set the path properly, and if your text file has different extension than .TXT, change it in the following code:

 
Public Sub ManipFiles()
    Dim sPath As String
    Dim aFile
    Dim sFileTxt As String
    Dim InFile, OutFile
    Dim nLines As Integer
    
    sPath = "C:\TEMP\"
    
    aFile = Dir(sPath & "*.TXT")
    While aFile <> ""
        InFile = sPath & aFile
        OutFile = sPath & "TMP.TXT"
        
        Open InFile For Input As #1
        Open OutFile For Output As #2
        
        nLines = 0
        Print #2, "Title"
        While Not EOF(1)
            nLines = nLines + 1
            Input #1, sFileTxt
            If nLines >= 5 Then
                Print #2, sFileTxt
            End If
        Wend
        Close #1
        Close #2
        Kill InFile
        Name OutFile As InFile
        
        aFile = Dir
    Wend
End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:cdebel
ID: 34966096
You may also add a line at the end to know when the code has finished to run, because if you run that over 318 files, depending on their size, it might take a while.

MsgBox "Job Done"
0
 
LVL 31

Expert Comment

by:Helen Feddema
ID: 34966118
I'm not sure why this is in the Excel zone.  Do you plan to run the code from Excel?  What type of files are these (comma-delimited, fixed-width, other)?
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 31

Expert Comment

by:Helen Feddema
ID: 34966122
I was going to post some code using the FileSystemObject, but the code above should do the job.
0
 
LVL 1

Author Comment

by:ntr2def
ID: 34966123
I meant to have placedit in the powershell zone only
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34966473
sorry, i gave the solution in the language that i know.   I could have done it in VB Script too with FileSystemObject... but i've no clue how to do this in Powershell.  I've never done that before so if you know that Helen, go ahead...
0
 
LVL 17

Accepted Solution

by:
Learnctx earned 500 total points
ID: 34966628
There's a stack of ways you can do it. But here's one, pretty basic. Skips the first 4 lines of the file and replaces the 5th line with the text you want at the top of the file. Other ways could be to import the file, remove the first 5, insert a new line, etc.
GCI c:\path\textfiles -Filter *.txt | % {
   $arrTemp = GC $_.fullname | Select -Skip 4
   $arrTemp[0] = "Title"
   $arrTemp | Set-Content $_.Fullname
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:ntr2def
ID: 34970598
worked perfectly thank you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question