alexiot
Goto Top

Letzte Zeile einer .txt in eine andere .txt ausschneiden per Batch

Hallo zusammen,

zu meinem obrigen Thema habe ich leider nur in Richtung löschen und nicht in Richtung ausschneiden etwas passendes
gefunden.

Ausgangssituation:
Ich habe eine Textdatei mit Hostnames die in etwa so aufgebaut ist:
XX0258XX001
XX0268XX005
XX0252XX003

Ich möchte nun, dass die letzt Zeile der Hostname.txt in eine andere Textdatei ausgeschnitten wird.
Wie viele Hostnames in der Textdatei sind ist immer unterschiedlich.

Vielen Dank für die bisher immer sehr schnelle Hilfe!

Content-ID: 282298

Url: https://administrator.de/forum/letzte-zeile-einer-txt-in-eine-andere-txt-ausschneiden-per-batch-282298.html

Ausgedruckt am: 22.04.2025 um 09:04 Uhr

TlBERlUS
TlBERlUS 09.09.2015 aktualisiert um 08:26:47 Uhr
Goto Top
Guten Morgen,

Powershell:
$path = "Pfad"  

$arr = gc "$path\Hostname.txt"  
$arr[-1] | Out-File $path\output.txt
$arr[-1] = ""  
$arr | out-file $path\Hostname.txt


Grüße,

Tiberius
AlexIOT
AlexIOT 09.09.2015 um 13:39:16 Uhr
Goto Top
Danke für die Antwort,

aber wenn ich die output.txt lösche und das Skript wieder ausführe haut es nicht mehr hin....
Wäre schön wenn du das noch fixen könntest.

Grüße,

Alex
122990
Lösung 122990 09.09.2015 aktualisiert um 16:11:31 Uhr
Goto Top
$pathIN = 'C:\hostname.txt'  
$pathOut = 'C:\ouput.txt'  
$file = gc $pathIN
$file[0..($file.length -2)] | out-file $pathIN
$file[-1] | out-file $pathOut
Gruß grexit
TlBERlUS
TlBERlUS 09.09.2015 um 13:51:32 Uhr
Goto Top
Hast recht, da habe ich nicht zuende gedacht.

$path = "Pfad"  
[System.Collections.ArrayList] $arr =gc $path\Hostname.txt
$arr[-1] | Out-File $path\output.txt
$arr.Remove($arr[-1])
$arr | out-file $path\Hostname.txt