Then, write the last row extracted from the first line of the new text, use the findstr statement to filter out the first and last lines of the original text, and add the first row extracted after the last line.
CopyCode The Code is as follows: @ echo off
: Purpose: to swap the first and last rows
: Train of thought:
: Extract the content of the first and last rows (if the first and last rows are empty rows, the extracted content of the first and last non-empty rows is not empty ),
: Write the last row extracted from the first line of the new text, use the findstr statement to filter out the first and last lines of the original text, and then in the last line
: Add the first row to be extracted.
: Because findstr has restrictions on processing the last row, an empty row is generated after the last row to ensure extraction accuracy.
: Note: Make sure that test.txt contains no sensitive characters, no duplicate lines, and the first line cannot be blank.
::
::
Copy test.txt test. Bak> NUL
Echo.> test.txt
Set num = 0
Setlocal enabledelayedexpansion
For/F "delims =" % I in (test.txt) Do (
Set STR = % I
Set/a num + = 1
If! Num! Equ 1 Set Var = % I
)
> Test_.txt echo. % STR %
Findstr/B/E/V/C: "% STR %"/C: "% var %" test.txt> test_.txt
> Test_.txt echo. % var %
Move test. Bak test.txt
Start test_.txt
Goto: EOF
@ echo off
: it can process empty first behavior and duplicate row content.
: Note: when the number of rows is counted by findstr/n, the last empty row is ignored.
:: therefore, if the last empty row exists, ensure that the number of empty rows is greater than 1.
findstr/n. * test.txt> tmp1.txt
for/F "tokens = 1 * delims =:" % I in (tmp1.txt) do (
If % I equ 1 Set STR = % j
set line = % I & set Var = % j
)
tmp2.txt echo % line %: % var %
findstr/B/V/C: "1:"/C: "% line %: "tmp1.txt> tmp2.txt
Echo 1: % STR %> tmp2.txt
echo.> tmp2.txt
Cd.> result.txt
for/F "tokens = 1 * delims =:" % I in (tmp2.txt) DO> result.txt echo. % j
Del/Q tmp1.txt tmp2.txt
Start result.txt