List the firs n line of a txt file in batch

October 13, 2011 at 02:21:28
Specs: Windows XP
I known that there is a loot of answers about this problem but i just cant find the solution. I always get some error. The real problem is i have to delete the 3 oldest folders ... so what i did is i list the folders by order of the date and i put them in txt file. so now for testing i just need to list the first 3 files of the txt file and i cant do it. Later i can put that with one del command i so on. For now i have this ...

@echo off

set /a cnt=0

for /F "delims=" %%i in (fajl.txt) do (
set /a cnt=cnt+1
if %cnt% == 3 ( goto :eof ) else ( @echo %%i )

goto :brojpak

with this i have output of just the first line, 3 times the same first line. Where is the error. Can someone help !!! :) sorry for the bad English ...

See More: List the firs n line of a txt file in batch

Report •

October 13, 2011 at 09:16:48
goto :brojpak
Why do you keep breaking out of your loop?

How To Ask Questions The Smart Way

Report •

October 14, 2011 at 06:22:34
Im not ... if i put after the ")" its the same even if there is no else ... the same error is there .... but we can put this behind because i found the solution

If i want to remain 3 newer folders and the rest of them to delete the solution is this ...

for /f %%a in ('dir /b /ad ^| find /c /v "" ') do set count=%%a

if %count% geq 3 (
for /f "skip=3 delims=*" %%i in ( 'dir /b /ad /o-d ') do (@echo %%i >> zapis.txt) )

for /f "usebackq delims=*" %%t in (zapis.txt) do rmdir /Q /S %%t
del zapis.txt

firs sort them by date but not oldest first and skip the 3 new one ... the rest of them write them in zapisi.txt and then delete the names in txt file ...


Report •

Related Solutions

Ask Question