DIR – Windows CMD Command
Display a list of files and subfolders.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
Syntax DIR [pathname(s)] [display_format] [file_attributes] [sorted] [time] [options] Key [pathname] The drive, folder, and/or files to display, this can include wildcards: * Match any characters ? Match any ONE character [display_format] /P Pause after each screen of data. /W Wide List format, sorted horizontally. /D Wide List format, sorted by vertical column. [file_attributes] /A[:]attribute /A:D Folder /A:-D NOT Folder /A:R Read-only /A:-R NOT Read-only /A:H Hidden /A:-H NOT Hidden /A:A Archive /A:-A NOT Archive /A:S System file /A:-S NOT System file /A:I Not content indexed Files /A:-I NOT content indexed /A:L Reparse Point /A:-L NOT Reparse Point (symbolic link) /A:X No scrub file /A:-X Scrub file (Windows 8+) /A:V Integrity /A:-V NOT Integrity (Windows 8+) /A Show all files Several attributes can be combined e.g. /A:HD-R [sorted] Sorted by /O[:]sortorder /O:N Name /O:-N Name /O:S file Size /O:-S file Size /O:E file Extension /O:-E file Extension /O:D Date & time /O:-D Date & time /O:G Group folders first /O:-G Group folders last several attributes can be combined e.g. /O:GEN [time] /T: the time field to display & use for sorting /T:C Creation /T:A Last Access /T:W Last Written (default) [options] /S include all subfolders. /R Display alternate data streams. /B Bare format (no heading, file sizes or summary). /L use Lowercase. /Q Display the owner of the file. /N long list format where filenames are on the far right. /X As for /N but with the short filenames included. /C Include thousand separator in file sizes. /-C Don’t include thousand separator in file sizes. /4 Display four-digit years. In most recent builds of Windows this switch has no effect. The number of digits shown is determined by the ShortDate format set in the Control Panel. |
The switches above can be preset by adding them to an environment variable called DIRCMD.
For example SET DIRCMD=/O:N /S
Override any preset DIRCMD switches by prefixing the switch with –
For example DIR *.* /-S
Sorting
The default Sort Order, if you don’t specify anything with /O, on an NTFS drive will be in sort-of-alphabetical order or on a FAT USB thumb drive, then the order will be based on the order in which files were created and deleted and the lengths of their names.
Upper and Lower Case filenames:
Filenames longer than 8 characters – will always display the filename with the mixed case as entered.
Filenames shorter than 8 characters – can display the filename in upper or lower case – this can vary from one client to another (registry setting)
1 2 3 |
To obtain a bare DIR format (no heading or footer info) but retain all the details, pipe the output of DIR into FIND, this assumes that your date separator is / DIR c:\temp\*.* | FIND "/" |
Listing the full path
The command DIR /b will return just a list of filenames, when displaying subfolders with DIR /b /s the command will return a full pathname.
If you want to list the full path without including subfolders, use the FOR command as in this example script.
Errorlevels
If the file(s) were successfully listed %ERRORLEVEL% = 0
If the file was not found or bad parameters are given %ERRORLEVEL% = 1
All file sizes are shown in bytes.
DIR is an internal command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Examples List the contents of c:\demo including ALL files: dir /a c:\demo\ List the contents of c:\demo displaying only the filenames: dir /b c:\demo\ List the contents of c:\demo and H:\work notice the trailing backslashes, if either directory does not exist, this will fail and set %Errorlevel% = 1 dir c:\demo\ h:\work\ List all the Reparse Points (symbolic links) in the current users profile: dir %USERPROFILE% /a:i List the contents of c:\demo with the full path of each file (source) for %%A in ("c:\demo\*") do echo %%~fA List the contents of c:\demo, without the header/footer details: FOR /f "tokens=*" %%G IN ('dir c:\demo\*.* ^| find "/"') DO echo %%G |
Wow, superb blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is wonderful, as well as the content!
Good article! We will be linking to this great post on our website.
Keep up the good writing.
Howdy would you mind stating which blog platform you’re using? I’m going to start my own blog in the near future but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!