Remove top 6 lines of a text file

June 7, 2009 at 16:22:27
Specs: Windows XP
I have a batch script that can remove the top 6 lines of a txt file.

@echo off
setLocal EnableDelayedExpansion


for /f "skip=6" %%A in (c:\batch\1.txt) do ( echo %%A >> # )
del "c:\batch\1.txt"
for /f %%A in (#) do ( echo %%A >> c:\batch\1.txt)
del #

i need to be able to delete the top 6 lines of all txt files in a directory

*.txt does not seem to work as it searches for a file actualy called *.txt

also how would i delete the same file i opened? could i create a varible to hold the correct filename?


See More: Remove top 6 lines of a text file

Report •


#1
June 7, 2009 at 18:54:01
then use dir with /S option.

Report •

#2
June 8, 2009 at 00:46:56
if i put comands in the ( ) including set varibles it seems to take them as a string rather then there set value

Report •

#3
June 8, 2009 at 04:55:09
You can use either of these:

for /f "tokens=* delims= " %%a in ('dir/b *.txt')

for %%t in (*.txt)

but NOT this:

for /f %%i in (*.txt)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
June 8, 2009 at 08:28:19
Thanks for the explanation, came up with below

@echo on
setLocal EnableDelayedExpansion

cd C:\

for /f "tokens=* delims=" %%a in ('dir/b *.txt') do (
echo %%a >> C:\temp.txt
for /f "tokens=* delims=" %%i in (%%a) do (
echo %%i >> #
del %%a
for /f %%k in (#) do ( echo %%k >> %%a)
del #
)
)
del temp.txt


seems a bit long winded, if someone can one line it that would be great :)


Report •

#5
June 8, 2009 at 08:45:51
sorry above was wrong, no skip and delete in the wrong place

@echo on
setLocal EnableDelayedExpansion

cd C:\

for /f "tokens=* delims=" %%a in ('dir/b *.txt') do (
echo %%a >> C:\temp.txt
for /f "tokens=* delims=" %%i in (%%a) do (
echo %%i >> C:\temp2.txt
del %%a
for /f "skip=6" %%k in (C:\temp2.txt) do ( echo %%k >> %%a)

)
del "C:\temp2.txt"
)
del "c:\temp.txt"


Report •

#6
June 8, 2009 at 09:14:49
the above does not work on files with spaces, any ideas on how to solve this?

Report •

#7
June 8, 2009 at 09:47:05
As ever, spaces in file names are a bad idea.

==============================
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims=" %%a in ('dir/b *.txt') do (
@echo off > #
for /f "skip=6 tokens=* delims=" %%i in (%%a) do (
echo %%i >> #
)
copy # "%%a" > nul
)
del #


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#8
June 8, 2009 at 11:37:47
thanks M2G i output the %%a to another file with "" around it then called it back in.

now lets me run on files with spaces


Report •

#9
June 8, 2009 at 11:48:49
How bout showing us your final version.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#10
June 11, 2009 at 03:35:09
This is my final version

::___________________________________________________________________
::REMOVE TOP 5 LINES
::
cd c:\

echo.
echo please wait untill screen shuts
echo.

::-------------------------------------------------------------------
del #
del c:\test.txt

for /f "tokens=* delims=" %%a in ('dir/b *.txt,*.csv,*.dat,*.xy') do (
echo "%%a" >> c:\test.txt
)

for /f "tokens=* delims=" %%g in (C:\test.txt) do (
for /f "skip=5 tokens=* delims= usebackq" %%i in (%%g) do (
echo %%i >> #
)
copy # %%g > nul
del #
echo File %%g Finished
)
::--------------------------------------------------------------------

del c:\test.txt

::end of remove 5 lines
::___________________________________________________________________________


Report •


Ask Question