You are not logged in.

#1 11 Mar 2016 17:04

wquatan
Member
Registered: 19 Jun 2012
Posts: 16

Process first 3 instead of skip in "for /f"

Related to
http://ss64.com/nt/for_f.html

for /f "tokens=* skip=3" %%F in ('dir %myDir% /o-d /tc /b') do del %myDir%\%%F
skips the first 3 files and processes all the others

I need just the opposite : processing the first 3 and skipping all the others

How could this been done ?

Thanks

Offline

#2 11 Mar 2016 18:20

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: Process first 3 instead of skip in "for /f"

You can count the number of files with find and then subtract three, reverse the sort in the for loop and use the resulting number in the skip field.

Or use delayedexpansion and a counter in the loop, only deleting until the counter is up to 3.

Or this will give you the first three in variables:

@echo off
set "file=%temp%\file.txt"
dir %myDir% /o-d /tc /b >"%file%"
(
set /p delfile1=
set /p delfile2=
set /p delfile3=
)<"%file%"
set delfile
del "%file%"
pause & goto :EOF

Offline

#3 11 Mar 2016 20:06

DigitalSnow
Member
From: United States
Registered: 27 Dec 2012
Posts: 24

Re: Process first 3 instead of skip in "for /f"

for /f "tokens=1,* delims=[]" %A in ('dir /b ^| find /n /v ""') do @if %A leq 3 @echo %B

Offline

#4 12 Mar 2016 00:50

wquatan
Member
Registered: 19 Jun 2012
Posts: 16

Re: Process first 3 instead of skip in "for /f"

Thank you folks !
Exactly what I needed to proceed

Offline

Board footer

Powered by