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
==================
Limit Number of Results in DIR output
Moderator: DosItHelp
Re: Limit Number of Results in DIR output
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
Re: Limit Number of Results in DIR output
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
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
Re: Limit Number of Results in DIR output
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.
Decide what you want it to do and *then* ask for that.
Re: Limit Number of Results in DIR output
@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.
Dave Benham
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
Re: Limit Number of Results in DIR output
Very clever, Dave.
Using your code then robbitok's next request could work by the following (untested):
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
Re: Limit Number of Results in DIR output
A huge thanks to you both for the advice, can confirm this produces the results perfectly.