Batch to delete the first line of a text file without creating a new file -
is there solution question? see lot of questions deleting first line of text file of them need create new text file. need because text file updating new lines (by second batch file) if script creates new text file can accidently delete new lines old text file.
can delete first line of text file without creating new one? if not, why?
- using only batch.
- blank lines shouldn't preserved (if possible).
- especial characters
!
can deleted.
if don't care preserving blank lines, !
, (
, )
, or ^
, can run input file through for
loop, storing each line in separate variable, merging variables newline character @ end of each one.
@echo off setlocal enabledelayedexpansion cls set counter=0 /f "delims=" %%a in (input.txt) ( set line[!counter!]=%%a set /a counter+=1 ) set /a counter-=2 set lf=^ /l %%a in (1,1,!counter!) set sheet=!sheet!!line[%%a]!!lf! set /a counter+=1 /l %%a in (!counter!,1,!counter!) set sheet=!sheet!!line[%%a]! echo !sheet!>input.txt
however, if want preserve blank lines , special characters, there few tricks can throw in, overall idea same.
@echo off setlocal enabledelayedexpansion cls :: findstr /n puts line numbers @ start of each line, allow preserve blank lines /f "tokens=1 delims=:" %%a in ('findstr /n "^" input.txt') set line_counter=%%a ::set /p preserves special characters <input.txt ( /l %%a in (1,1,!line_counter!) set /p line[%%a]= ) set /a line_counter-=1 set lf=^ :: not delete 2 blank lines above line. /l %%a in (2,1,!line_counter!) set sheet=!sheet!!line[%%a]!!lf! set /a line_counter+=1 /l %%a in (!line_counter!,1,!line_counter!) set sheet=!sheet!!line[%%a]! echo !sheet!>input.txt
Comments
Post a Comment