Solved

edit multiple text files

Posted on 2011-02-23
8
302 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 16

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
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 will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

743 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

12 Experts available now in Live!

Get 1:1 Help Now