Limit Number of Results in DIR output

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rabbitok
Posts: 3
Joined: 14 Feb 2012 15:29

Limit Number of Results in DIR output

#1 Post by rabbitok » 14 Feb 2012 15:36

G'day,

Have built media server and am seeking to output the recently added media from multiple external drives to a .txt list, could anyone advise how to limit the number of results of the DIR output to say 10 or 20 results, this way the resulting list would list the most recently added files...

I have limited experience with DOS based .bat commands so any pointers to better syntax are welcome !

===============

@ ECHO OFF
cd /d A: && Dir /B /O:-D /P > "C:\Documents and Settings\MEDIA\Desktop\!_The Recent Media List.txt"
EXIT

==================

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Limit Number of Results in DIR output

#2 Post by foxidrive » 14 Feb 2012 18:45

I changed the filename so it wouldn't wrap in my editor.

Code: Select all

@echo off
set "file=C:\Documents and Settings\MEDIA\Desktop\!Recent Media.txt"
set c=0
dir a:\ /b /o:-d /-p > "%file%"

for /f "delims=" %%a in ('type "%file%"') do call :next "%%a"
pause
goto :EOF

:next
set /a c=c+1
if %c% GTR 10 goto :EOF
echo %1

rabbitok
Posts: 3
Joined: 14 Feb 2012 15:29

Re: Limit Number of Results in DIR output

#3 Post by rabbitok » 14 Feb 2012 20:17

Wow that was Quick, Yes that works perfectly : )

A huge Thank-you !

The next step would be to add additional (multiple) Hard drives to the List (i.e A, B , J, K, Q, R)

Is that also possible ?

Have tried a number of edits to achieve that result , though your Code is already a way beyond knowledge of the capabilities of DOS :?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Limit Number of Results in DIR output

#4 Post by foxidrive » 14 Feb 2012 22:18

Adding extra features often changes batch code in large ways so Don't ask for extra features every time you get code that works.

Decide what you want it to do and *then* ask for that.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Limit Number of Results in DIR output

#5 Post by dbenham » 14 Feb 2012 23:15

@foxidrive - I understand your irritation

Here is an interesting way to do the original request very efficiently without any temp file, without using CALL, and without using delayed expansion. The interesting trick is to intentionally cause a divide by 0 error when you reach the N'th line by dividing by the count modulo N.

Code: Select all

@echo off
setlocal
set cnt=0
for /f "delims=" %%A in ('dir a:\ /b /o-d') do (
  echo %%A
  set /a "cnt+=1, 1/(cnt%%20)" 2>nul || goto :break
)
:break


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Limit Number of Results in DIR output

#6 Post by foxidrive » 15 Feb 2012 02:27

Very clever, Dave. :)

Using your code then robbitok's next request could work by the following (untested):

Code: Select all

@echo off
setlocal

for %%z in (a b j k q r) do call :list %%z
pause
goto :EOF

:list
echo drive %1:\
echo.
set cnt=0
for /f "delims=" %%a in ('dir %1:\ /b /o-d') do (
  echo %%a
  set /a "cnt+=1, 1/(cnt%%10)" 2>nul || goto :break
)
:break
echo.
pause

rabbitok
Posts: 3
Joined: 14 Feb 2012 15:29

Re: Limit Number of Results in DIR output

#7 Post by rabbitok » 20 Feb 2012 23:05

A huge thanks to you both for the advice, can confirm this produces the results perfectly.

Post Reply