tags:

views:

1143

answers:

3

I'll be honest I feel dumb about this one. I am trying to just remove the first line of about 5000 text files before importing them.

I am still very new to powershell so not sure what to search for or how to approach this. My current concept using pseudo-code

set-content file (get-content unless line contains amount)

However, I can't seem to figure out how to do something like contains. I tried a couple of other methods, but feel like I am chasing my tail for an easy problem.

Thanks for any help.

+2  A: 

Not the most efficient in the world, but should work:

get-content $file |
    select -Skip 1 |
    set-content "$file-temp"
move "$file-temp" $file -Force
Richard Berg
When I try to run this it seems that it errors out on the -skip. Could that maybe be from a different version?
percent20
-Skip is new to Select-Object in PowerShell 2.0. Also, if the files are all ascii then you might want to use set-content -enc ascii. If the encodings are mixed, then it gets trickier unless you don't care about the file encoding.
Keith Hill
I installed powershell 2.0 and it is working now.
percent20
A: 

Using variable notation, you can do it without a temp file.

${C:\file.txt} = ${C:\file.txt} | select -skip 1
function Remove-Topline ( [string[]]$path, [int]$skip=0 ) { if ( (Test-Path $path -PathType Leaf) -like 'false') { throw "invalid filename" } else { ls $path | % { iex "${$($_.fullname)} =${$($_.fullname)} | select -skip $skip" } } }

hoge
A: 

Hello for me also -skip didn't work, so my workaround is $LinesCount = $(get-content $file).Count get-content $file | select -Last $($LinesCount-1) | set-content "$file-temp" move "$file-temp" $file -Force

Mariusz Biesiekierski