I successfully reversed the order of lines in a text file using the below batch, however Original.txt has spaces (in between each line there is 1 CRLF) and Reversed.txt removes all CRLF (Carriage Return/Line Feed).
Is there a way to keep the CRLFs in Reversed.txt?
Thanks for any creative support 🙂
@echo off
ren *.txt Original.txt
setlocal EnableDelayedExpansion
rem/> Reversed.txt
for /F "delims=" %%a in (Original.txt) do (
(
echo(%%a
type Reversed.txt
) > temp.txt
move /Y temp.txt Reversed.txt > NUL
)
2
@ECHO OFF
SETLOCAL
rem/> u:Reversed.txt
for /F "tokens=1*delims=]" %%b in ('FIND "" /v /n ^<q79258953.txt') do (
(
echo/%%c
type u:Reversed.txt
) > u:temp.txt
move /Y u:temp.txt u:Reversed.txt > NUL
)
TYPE u:Reversed.txt
GOTO :EOF
For the reasons mentioned in the comments.
I used a file named q79258953.txt
on my RAMDRIVE containing some dummy data for my testing.
This obviously will not work for input file lines beginning ]
.
Here’s how I’d suggest it is done, which is an expansion of the method I mentioned in the comments. It is similar to the answer already submitted, but lines beginning with ]
will not be problematic.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
%SystemRoot%System32xcopy.exe "*.txt" ? /LQ | %SystemRoot%System32findstr.exe /R "^1>" 1>NUL || Exit /B
For %%G In ("*.txt") Do Set "orig=%%G"
Ren "%orig%" "Original.txt"
CD.>"Reversed.txt"
For /F "Delims=" %%G In ('%SystemRoot%System32find.exe /N /V "" 0^<"Original.txt"') Do (Set "Line=%%G"
(SetLocal EnableDelayedExpansion
Echo(!Line:*]=!
Endlocal
Type "Reversed.txt") 1>"temp.txt"
Move /Y "temp.txt" "Reversed.txt" 1>NUL)
Ren "Original.txt" "%orig%"
I took the liberty of adding some lines to have the original text file returned to its name after reversing it. I also added some code to exit the script if there is more than one file matching *.txt
glob.