-2

i have a directory in windows server in which several directories are there which i have sorted while listing. Now i need to find first 2 directories from that list. can anyone please help me with DOS command?

vishal
  • 247
  • 1
  • 2
  • 9
  • Please, show what you have, otherwise we will have to solve your first task first (that was already solved) before we can help you with the second one. 'A list' is quite abstract. This can be a file, an environment variable, just some output on a screen.... please be clear. – GolezTrol May 27 '15 at 12:31
  • posted working code as an answer , let me know if your requirement has any change – prudviraj May 27 '15 at 14:33

2 Answers2

0

here is the code you wanted

change the Directory path according to your requirement:

@echo off setLocal EnableDelayedExpansion c: cd c:\ set /a count=0 for /f %%A in ('DIR /A:D /B') do ( set /a count+=1 if !count! LEQ 2 ( echo !count!.Directory name %%A ) )

Output of above script tested output :

c:>first_twofiles.bat 1.dir name $Recycle.Bin 2.dir name Automation_Framework

prudviraj
  • 3,634
  • 2
  • 16
  • 22
0

Without looping:

cd /d "x:\source\path"
for /f "delims=[] tokens=1,2*" %%i in ('dir /b/ad ^| find /N /V ""') do @if %%i LEQ 2 echo %%j  

The ordinal is available via %%i if needed. Note that the dir /b gets rid of the '.' and '..' entries and junctions or other links.

user1016274
  • 4,071
  • 1
  • 23
  • 19