Bat File To List and Count Files:
Explained with the Newbie in Mind
~~Vic
Ferri, WinTips and Tricks
This is an expanded tip from the archives of
Vic's WinTips&Tricks Yahoo Group:Original Question:
<snip>By trade, I am a DOS programmer. I use C++ and Powerbasic for 99.9%
of what I do. Anyway, I have come upon a need to count filenames on a CD or
DVD
*and* save that number somewhere. Does anyone know how to do that in DOS?...
. . oh, it would be nice if the file count program worked in both XP and/or
ME; but of the two, I would give XP
the first preference.
</snip>
Reply from Vic:
This is something quite simple to do using Ms-Dos. Here is one method that
should work on all Windows versions (personally, I tested this in Windows 98
and Windows XP) and which will give you more than just a number. What this
bat file will do is produce for you a Word or text document containing a
neatly numbered list of all the files and folders in the drive or directory
you specify in the command line. So you get the list of files plus the
number. The files will be listed in alphabetical order and with paths so you
know exactly where each file resides. Note, alphabetical order in MS-DOS
means that first the names of the folders will be sorted in alphabetical
order and then the names of files in each folder will be sorted in
alphabetical order.
Ok let us now create the bat file.
Right click your desktop, choose New>Text Document to produce a new text
document on your desktop. No need to name it now. Just open it up and
copy/paste the following commands:
@echo off
dir /b /o /s /v c:\projects\ | find "" /n /v >count.txt
cls
The above would produce a numbered list of all the files in c:\projects
except for hidden and system files. Substitute "c:\projects" with the
path and name of the folder or drive you wish to search.Examples:
- If you want a content list of a CD and your cd-rom drive letter is
g:, you would change the line to:
dir /b /o /s /v g:\ | find "" /n /v >count.txt
- If the folder name contains a space, enclose the path with quotation
marks. An example would be My Documents:
dir /b /o /s /v "D:\My Documents\" | find "" /n /v >count.txt
- If you would like to include all files, including the hidden and
system ones, then just add the "a" switch. ie:
dir /a /b /o /s /v c:\windows\ | find "" /n /v >count.txt
You can also change the name of the output file, count.txt to whatever
name you want. You might want to change it to count.doc if all you use is
Notepad as your text editor in Windows 9x (Windows Notepad has a capacity of
only 64 kbs) .doc will open the file, by default, with Wordpad, which has a
much greater capacity, or with whatever Word processor you have associated
with the extension .doc For many that would be Microsoft Word.
Once you finish editing the command line, save the document.
Now rename your text document with a .BAT extension. ie if you saved the
file as count.txt, rename it
to count.bat. You will be warned about changing extensions, but just ignore
and click Yes.
NOTE: Make sure you are not hiding
extensions because if you are, renaming will not work - instead of being
count.bat, your file will become count.bat.txt. To see if you are hiding
extensions, open any folder, click View> Folder
Options>View (depending on your Windows version, getting there may vary
slightly) Look for the option: "Hide extensions for known file types"
and make sure it is NOT checked.
To use the file, simply double click it. The file will be created in the
same directory you run the bat file from. So if you run it from your
desktop, look for count.txt on your desktop, after running the bat file.
A COUNT ONLY
If all you want is a dos method to know the number of files on any of your
drives or directories, this command line should do it:
dir /a /s "c:\" |find /c /v ""
This will give you a count of all the files and directory on your c: drive.
To specify a different drive, simply substitute c:\ with the drive you want
searched.
To use, you can either enter the command at an Ms-Dos prompt or use it in a
batch file.
This can be useful if you are running Windows 9x, where the Find feature is
limited to 10,000 results (these days, it is quite common to have many more
than 10,000 files on a hard disk - in my last count, there were almost
30,000 files on my c: drive)
But how about enhancing this dos method so that all you have to do is
click an icon on your desktop to get a count of all the files on your hard
drive?
To do that, open up notepad and this time, copy and paste these commands:
@echo off
dir /a /s "c:\" |find /c /v "" >c:\windows\filecount.txt
start /w c:\windows\filecount.txt
del c:\windows\filecount.txt
cls
Again, substitute c:\ with whatever drive you want to get a file count from.
Save it with a name something like filecount.bat
And that's it - now anytime you want a file count of your hard drive, double
click the bat file, and within several seconds, notepad will open up
displaying a number representing the number of files found.
If you'd rather not have the Dos box visible, you can create a shortcut to
the bat file, and then go to the Properties of the Shortcut and choose Run
Minimized and Close on Exit. You can also change the icon to something more
friendly.
If you do this, you might want to create your bat file in your C: drive and
then drag it to your desktop to create a shortcut to it (just so you won't
have to have 2 files for it on your desktop)
Now lets explain the basic command lines briefly:
dir is used to get a directory listing.
/b is a dir switch that stands for bare and is used to get a neat bare boned
line listing - lists each file name, including extension, one per line.
There in no heading information and no summary included.
/s is a dir switch that stands for sub and is used to include all sub
folders and the files they contain, as well.
/n is both a dir and find switch, as a dir switch it stands for name and
sorts the results in alphabetical order by name.
As a find switch it stands for number and in our case here is responsible
for numbering each line.
/a is a dir switch that stands for attributes and when added without
specifying any attributes, shows all files, including system and hidden.
/c is a Find switch that displays only a count of the lines that contain the
specified string.
/v as a Find switch is used to display all lines not containing the
specified string.
This explains the "'' I used it the command line. It's just a way to find
all files by using a string not likely to occur in any file name. It doesn't
have to be "", it could be anything that you feel confident wouldn't be in a
file name.
v is also a dir switch standing for verbose which gets file information -
such as displaying long file names - but this switch is relatively recent
and does not exist in earlier versions of Dos (prior to 7) It works in all
versions of Windows from 95 and up.
| is a pipe command - it takes the output of one command and uses it as
input for another. In the numbered list case, the dir command acts as output
for the find command, and the results are outputted to a file using the
greater than symbol ( >)
ACCURACY NOTES
More than likely there will be discrepancies between the number of files
reported by Dos and the number of files reported by Explorer, if you are
including all files - hidden and system. Both have their oddities.
Command.com in Ms-Dos may not report all hidden and system files with
the dir /s switch and Explorer may miscount or stumble with IE's cache
directories. So whether your count is from Windows or Dos, both may not be
totally accurate.
For Dos, you can get a more accurate count of ALL files by using the
attribute command.
Compare the results of this command to the Dir commands to see if they show
differences for you:
attrib.exe /s c:\*.* |find /c /v ""
(the above is to search your c: drive. Substitute c: with whatever drive
letter you wish to search.
If you want all the files listed in a file, then use this:
attrib.exe /s c:\*.* |find /v /n "" >count.txt
According to one of my Dos mentors, Charles Dye:
... there's a long-standing error in COMMAND.COM
which prevents DIR /S from recursing into subdirectories which
have either the Hidden or the System attributes set. This may
not explain all the differences between the DIR /S list and
whatever Explorer reports, but it will certainly explain part
of it. (Conversely, Explorer has weird quirks of its own, such
as making IE's cache directories look like a single directory.
You may never be able to completely reconcile the two....)
There's a program called DOS-FIX by a John Augustine which
patches COMMAND.COM to remove the DIR /S issue. As always,
back COMMAND.COM up before modifying it; but I've had good
luck with with this utility.
You can also use any decent file-finder to count files. My
own LOCATE correctly recurses into all subdirectories, as does
John Stockton's HUNT.
An even better solution, IMHO, is to avoid using COMMAND.COM
for anything important. If you spend a significant amount of
time at a command prompt, a professional shell like 4DOS can
be a very worthwhile investment.
<<<back to contents
Vic Ferri owns the very popular
WinTips and
Tricks and
Registry Answers. Subscribe to either and receive free Windows and Registry
Tips. He is also in charge of the
Printing Tips pages at Linda's Computer Stop. Vic has also created a program
which allows you to Lock & Hide desktop folders in Windows 9X/ME.
Read more and get the free demo here. And, he now offers a service to
convert PowerPoint presentations to .exe files which can be viewed on
computers which do not have PowerPoint installed.