删除文件中的前n行(带有许多“奇怪”符号的.zip文件)

问题描述 投票:-1回答:2

此powershell代码删除文件中的前4行

(gc "old.zip" | select -Skip 4) | sc "new.zip"

old.zip文件有Unix(LF)行结尾

此代码还将文件行结尾转换为Windows(CR LF)

如何在不转换的情况下删除前4行?

由于.zip中存在许多“奇怪”符号,因此删除.zip文件中前n行的其他方法不起作用。例如,more than +4 "old.zip"> "new.zip"中的cmd不起作用等。

通过powershell这样的东西被删除但也没有问题。

你知道吗?删除.zip文件中前n行的其他方法?

powershell batch-file zip edit
2个回答
0
投票

这样的事情?

$PathZipFile="c:\temp\File_1.zip"
$NewPathZipFile="c:\temp\NewFile_1.zip"

#create temporary directory name
$TemporyDir=[System.IO.Path]::Combine("c:\temp", [System.IO.Path]::GetRandomFileName())

#extract archive into temporary directory
Expand-Archive "c:\temp\File_1.zip" -DestinationPath $TemporyDir

#loop file for remove 4 first lines for every files and compress in new archive
Get-ChildItem $TemporyDir -file | %{

    (Get-Content $_.FullName | select -Skip 4) | Set-Content -Path $_.FullName;
    Compress-Archive $_.FullName $NewPathZipFile -Update

}

Remove-Item  $TemporyDir -Recurse -Force

-1
投票

电源外壳:

(gc "old.txt" | select -Skip 4 | Out-String) -replace "`r`n", "`n" | Out-File "new.txt"

C#:

File.WriteAllText("new.txt", string.Join("\n", File.ReadLines("old.txt").Skip(4)));
© www.soinside.com 2019 - 2024. All rights reserved.