1

Hi I am trying to delete the first 3 lines from the top of multiple .txt files with a batch file. The first and second lines contain text and the third is blank. I am trying to find a way to delete the lines according to their line number.

e.g Line 1 ABCD     
    Line 2 EFG
    Line 3
    Line 4 cool this works
    Line 5 line of text
    Line 6 line of text
    Line 7 
    Line 8 line of text

I have used the code below which works however, I have to give the variables as "keywords" and i want to use line numbers as variables. Also, for some reason the cursor is always creating a blank line at the top of each "new.txt" file made.

@ECHO OFF
SETLOCAL
FOR %%i IN (C:\source\*.txt) DO (
TYPE "%%i"|FINDstr /l /v "ABCD  EFG" >> C:\newfiles\%%~ni.new
)
GOTO :EOF
Jonas
  • 1,105
  • 1
  • 15
  • 21
  • To delete the first 3 lines, use: `> "C:\newfiles\file.new" (for /F usebackq^ skip^=3^ delims^=^ eol^= %%L in ("C:\source\file.txt") do echo(%%L)` – aschipfl Feb 29 '16 at 08:51
  • 1
    I gave it a bash however, It returns a blank file. I'll continue researching. – Jonas Feb 29 '16 at 09:20
  • `more +3 input.txt>output.txt`, but it replaces TABs with spaces (which may or may not be a problem) – Stephan Feb 29 '16 at 09:28
  • 1
    The Output is blank. Could possibly be the TABs.. – Jonas Feb 29 '16 at 09:43
  • 1
    `more /E +4 C:\a\*.txt > C:\b\newfile.new` gives me the desired result minus the multiple file part XD. – Jonas Feb 29 '16 at 10:02
  • `@ECHO OFF SETLOCAL FOR %%i IN (C:\awe\*.eml) DO ( TYPE "%%i"| more /E +4 >> C:\5500\%%~ni.eml ) goto :eof` got the job done. it removes the first 4 lines and then copys them all to C:\5500\ instead of to one file like all the other answers, Much thanks to Stephan. – Jonas Mar 02 '16 at 09:53

1 Answers1

2

Give a try for this code :

@echo off
set InPutfile=InputFile.txt
set OutputFile=OutPutfile.txt
(
    for /F "usebackq skip=3 delims= eol=" %%L in (`Type "%InPutfile%"`) do echo %%L
)>%OutputFile%
Start "" %OutputFile%
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 1
    `for /F` would skip all empty lines, e.g. line 7 in given example. Moreover, `… eol="` would _choke down_ all lines with leading `"` double quote. – JosefZ Feb 29 '16 at 10:51