Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 971
  • Last Modified:

count number of files using dos command and then email to me the count

I like to the count number of files using dos command and then email to me the count.  Not sure if dos can send email.  So, count the number of files and store the count in a file.  Thanks.
0
ewang1205
Asked:
ewang1205
  • 6
2 Solutions
 
ReneGeCommented:
Here you go

 
@echo off

REM DO NET FORGET TO CUSTOMIZE THIS SCRIPT.

SET ThePath=C:\Temp

for /f %%a in ('dir /s %ThePath% ^| FINDSTR -i "file"') DO SET FileCount=%%a

echo Total Files=%FileCount%


:SendEmail
REM YOU CAN DOWNLOAD BLAT EMAIL COMMAND LINE UTILITY AT
REM http://sourceforge.net/projects/blat/files/Blat%20Full%20Version/Blat%20v2.6.2/blat262.full.zip/download

SET Emailer=blat.exe
SET EmailTo=user@emailaddress.com
SET EmailFrom=sysadmin@emailaddress.com
SET EmailSubject=File Count for %ThePath%
SET EmailServer=smtp.server.com
SET EmailBody=Total Files for %ThePath% is:%FileCount%

"%Emailer%" -body "%EmailBody%" -to "%EmailTo%" -f "%EmailFrom%" -s "%EmailSubject% %time%" -server "%EmailServer%" -debug -timestamp -log "Email.log" >NUL

Open in new window

0
 
athar_anisCommented:
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

Open in new window


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)

EMAIL

If you are thinking of cmd (the DOS like command prompt) there are several solutions depending of how the email is about to be sent.
Sendmail is a program that you can use in shell if you send mail using a pop3/smpt server.
If you want to send mail using an exchange server, you can use mapisend which you can get from microsoft. Also, try visiting this link
CLICK HERE

0
 
ReneGeCommented:
In addition to "athar_anis", and concerning line 2:

/a must be followed by an attribute. See below.

WILL INCLUDE ALL ACCESSIBLE FILES, FOLDERS AND EXTRA DESCRIPTION LINES:
(So count will not be accurate)
dir /s "c:\" |find /c /v "" >c:\windows\filecount.txt

WILL INCLUDE ALL ACCESSIBLE FILES AND FOLDERS:
dir /s /b "c:\" |find /c /v "" >c:\windows\filecount.txt

WILL INCLUDE ALL ACCESSIBLE FILES:
dir /a-d /s /b "c:\" |find /c /v "" >c:\windows\filecount.txt

WILL INCLUDE ALL ACCESSIBLE FOLDERS:
dir /ad /s /b "c:\" |find /c /v "" >c:\windows\filecount.txt

Also, I like "athar_anis" approach, so for the fun of it, I incorporated it to my script. I'll test and compare both performances and let you know.

FYI: I did not included the DIR switches (/) in my original script because the file count is parsed from the DIR command itself, not from the count result of [find /c /v ""], which counts all lines resulted from the command before the [|].

By the way, if you prefer my solution, please also give points to "athar_anis" for his efforts.

 
@echo off

REM DO NET FORGET TO CUSTOMIZE THIS SCRIPT.

SET ThePath=C:\Temp

rem for /f %%a in ('dir /s %ThePath% ^| FINDSTR -i "file"') DO SET FileCount=%%a
for /f %%a in ('dir /a-d /s /b "%ThePath%" ^|find /c /v ""') DO SET FileCount=%%a


echo Total Files=%FileCount%


:SendEmail
REM YOU CAN DOWNLOAD BLAT EMAIL COMMAND LINE UTILITY AT
REM http://sourceforge.net/projects/blat/files/Blat%20Full%20Version/Blat%20v2.6.2/blat262.full.zip/download

SET Emailer=blat.exe
SET EmailTo=user@emailaddress.com
SET EmailFrom=sysadmin@emailaddress.com
SET EmailSubject=File Count for %ThePath%
SET EmailServer=smtp.server.com
SET EmailBody=Total Files for %ThePath% is:%FileCount%

"%Emailer%" -body "%EmailBody%" -to "%EmailTo%" -f "%EmailFrom%" -s "%EmailSubject% %time%" -server "%EmailServer%" -debug -timestamp -log "Email.log" >NUL

Open in new window

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
ReneGeCommented:
Result:
For arround 1 300 000 files on a RAID 0 Dual SSD drive

dir /a-d /s /b "%ThePath%" ^|find /c /v "" : TOOK: 1min25secs
dir /s %ThePath% ^| FINDSTR -i "file" : TOOK: 1h30mins

I did not test the precision of the results I'll let this to be tested by one of you.

So my last batch file, and including "athar_anis" find command line seems to be the best solution.

Cheers,
Rene

0
 
ReneGeCommented:
To add to my previous statment:
I did not test the precision of the results I'll let this to be tested by one of you.

MEANING:
I did not test the precision of the file count results I'll let this to be tested by one of you.

0
 
ewang1205Author Commented:
Great job! Outstanding work!
0
 
ReneGeCommented:
As they were not exactly the same:

dir /a-d /s /b "%ThePath%" ^|find /c /v "" : TOOK:1min25secs  FILE COUNT:1319444
dir /s %ThePath% ^| FINDSTR -i "file" : TOOK:1h30mins  FILE COUNT:1319960

Thinking about it, I'd say that the one that has the highest file count wins, but it takes longer.  Depending misc confige and NTFS security settings... both command lines may give you the same file count results.

Have fun!
0
 
ReneGeCommented:
Thanks ewang1205

Cheers,
Rene
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now