Remove first 2 lines from multiple files in a directory

Welcome Forums General PowerShell Q&A Remove first 2 lines from multiple files in a directory

Viewing 8 reply threads
  • Author
    Posts
    • #211002
      Participant
      Topics: 2
      Replies: 5
      Points: 32
      Rank: Member

      Hello,

      Today is my first day playing around with powershell (used to do some scripting in bash/linux and batch scripting way long ago)

      I found some scripts that are similar to what I need and am trying to mash them together. I would like to move the first two lines from a number of files that are in a directory but can’t get this to work. Any help would be very appreciated. Thanks!

       

       Get-Content c:\test\*.txt | ForEach { Get-Content $_.Fullname | Select-Object -Skip 2 | Out-File “$($_.Directory)\$($_.BaseName)-new.txt”}
    • #211011
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      If you want to overwrite existing files you could start with something like this:

      Get-Content c:\test\*.txt | 
          ForEach-Object {
              $Content = Get-Content -Path $_.Fullname
              $Content | 
              Select-Object -Skip 2 | 
              Out-File -FilePath $_.Fullname
          }

      Please test with test files! 😉

    • #211032
      Participant
      Topics: 2
      Replies: 5
      Points: 32
      Rank: Member

      Thanks for the quick response. I’m getting the following error

       

       

       Out-File : Cannot bind argument to parameter ‘FilePath’ because it is null.
      At C:\test\test4.ps1:6 char:28
      + Out-File -FilePath $_.Fullname
      + ~~~~~~~~~~~
      + CategoryInfo : InvalidData: (:) [Out-File], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileCommand
      
      Get-Content : Cannot bind argument to parameter ‘Path’ because it is null.
      At C:\test\test4.ps1:3 char:38 
    • #211044
      Participant
      Topics: 0
      Replies: 3
      Points: -4
      Rank: Member

      <p style=”text-align: center;”></p>
      Try this:

      Get-Content c:\test\*.txt |
      ForEach {
      Get-Content -Path $_ | Select-Object -Skip 2 | Set-Content $_
      }

      here $_.FullName will not work. Because returned object is System.String not System.Io.FileInfo

    • #211053
      Participant
      Topics: 2
      Replies: 5
      Points: 32
      Rank: Member

      That also did not work, unfortunately. It seems to be spitting out the first line of the file and trying to read it for processing.

       Get-Content : Cannot find drive. A drive with the name ‘Job ‘Run_GetDataMonthlyUploadCnts’ ‘ does not exist.
      At C:\test\test4.ps1:3 char:1
      + Get-Content -Path $_ | Select-Object -Skip 2 | Set-Content $_
      + ~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : ObjectNotFound: (Job ‘Run_GetDataMonthlyUploadCnts’ :String) [Get-Content], DriveNotFoundException
      + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand 
    • #211071
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Thanks for the quick response. I’m getting the following error

      oooops, sorry. My mistake … this should work:

      Get-Content c:\test\*.txt | 
          ForEach-Object {
              $FullName = $_.Fullname
              $Content = Get-Content -Path $_.Fullname
              $Content | 
              Select-Object -Skip 2 | 
              Out-File -FilePath $FullName 
          }
    • #211080
      Participant
      Topics: 2
      Replies: 5
      Points: 32
      Rank: Member

      Still getting an error. I appreciate you helping to troubleshoot.

       

        Get-Content : Cannot bind argument to parameter ‘Path’ because it is null.
      At C:\test\test4.ps1:4 char:38
      + $Content = Get-Content -Path $_.Fullname
      + ~~~~~~~~~~~
      + CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand 
    • #211113
      Participant
      Topics: 5
      Replies: 321
      Points: 436
      Helping Hand
      Rank: Contributor
      # Skip first two lines then output all other lines
      Get-ChildItem -Path c:\test -Filter *.txt | ForEach-Object {
          $line = Get-Content -Path $_.FullName
          $line[2..$line.Length] | Out-File "$($_.Directory)\$($_.BaseName)-new.txt"
      }
      
    • #211140
      Participant
      Topics: 2
      Replies: 5
      Points: 32
      Rank: Member

      That works, thanks!

       

      -og

Viewing 8 reply threads
  • You must be logged in to reply to this topic.