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
)
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%\System32\xcopy.exe "*.txt" ? /LQ | %SystemRoot%\System32\findstr.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%\System32\find.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.