Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

edit multiple text files

Posted on 2011-02-23
8
Medium Priority
?
309 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:Christian de Bellefeuille
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:Christian de Bellefeuille
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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:Christian de Bellefeuille
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 18

Accepted Solution

by:
Learnctx earned 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

636 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