[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

edit multiple text files

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
ntr2def
Asked:
ntr2def
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Christian de BellefeuilleProgrammerCommented:
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
 
Christian de BellefeuilleProgrammerCommented:
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
 
Helen FeddemaCommented:
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
Helen FeddemaCommented:
I was going to post some code using the FileSystemObject, but the code above should do the job.
0
 
ntr2defAuthor Commented:
I meant to have placedit in the powershell zone only
0
 
Christian de BellefeuilleProgrammerCommented:
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
 
LearnctxEngineerCommented:
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
 
ntr2defAuthor Commented:
worked perfectly thank you
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now