Slegnor
Slegnor

Reputation: 41

Batch: Read a txt file and output the last 10 lines a comma separated line

I have a simple batch file which reads the last 10 lines from a batch file and then outputs those 10 lines a new txt file but I need it to output as a comma separated line/string.

@echo off
for /f %%i in ('find /v /c "" ^< C:\Path To File\File.txt') do set /a lines=%%i
set /a startLine=%lines% - 10
more /e +%startLine% C:\Path To File\File.txt > Output.txt

Also, is it possible reverse the line order in the new txt file so the last line is at the beginning of the comma separated line

Example of what I'm after:

line1
line2
line3
line4

outputted as

line4, line3, line2, line1

Upvotes: 4

Views: 730

Answers (1)

user6811411
user6811411

Reputation:

This small powershell script wil do:

$Lines = Get-Content .\Lines.txt|select -last 10
($Lines[($Lines.Length-1)..0]) -join(', ')|Set-Content Lines-new.txt

To be on topic wrapped in a batch:

@echo off
Powershell -command "($Lines=GC .\Lines.txt|select -last 10);(($Lines[($Lines.Length-1)..0]) -join(' ')|Set-Content Lines-New.txt)"

Upvotes: 1

Related Questions