Solved

edit multiple text files

Posted on 2011-02-23
8
303 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
  • 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
 
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now