Home / Microsoft / Microsoft DOS / Creating batch file to count number of files in a directory
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2  All - (Bottom) Print
Author Topic: Creating batch file to count number of files in a directory  (Read 30442 times)
ljones123
Topic Starter
Newbie



Posts: 2


« on: January 08, 2009, 10:10:19 AM »

I would like to do the above and then if the number of files is larger then what I specify, have it email me and tell me that the number of files has been exceeded. Any advice on this would be great.
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #1 on: January 08, 2009, 06:24:01 PM »

I don't think MS-DOS can email, if anyone knows different, tell please. I am like 90% sure I'm right on this.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,881

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #2 on: January 08, 2009, 06:29:31 PM »

there are probably programs that could be invoked from the command-line to E-mail.

as for counting the number of files, I'd imagine it would involve sending the output to FIND, and finding the last line with "File(s)", and then grabbing the number before that and comparing it to the value your checking for. If it is larger, you would invoke the e-mail sending program with appropriate arguments.

IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Tan_Za
Intermediate



Posts: 134

Experience: Experienced
OS: Windows 7


Starcraft and Pascal programming YAY

« Reply #3 on: January 08, 2009, 11:43:45 PM »

The attackment should do what you want, it will count files and folders in the directiory that the program is placed in. It is not as easy as you would think so I dout it to be in any book because this is really complicated batch stuff that what you are asking gets into.

Hope you like, but since the code is quite difficult the app is and exe,
Tan_Za

[attachment deleted by admin]
IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,881

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #4 on: January 09, 2009, 01:00:35 AM »

Here is my version:

the only real thing I could think of was (aside from not search subdirectories(?)) was that it included all the directories in the file count.

In order to remedy this, I used a switch on dir to exclude directories.

Code: [Select]
@echo off
title File Counter batch.
:recurse
set I=1

echo "files in folder"
cd
REM view all files, EXCEPT directories.
FOR /f "tokens=*" %%P IN ('dir /A-d /b') do (call :showfiles "%%P")
echo Filecount: %I%
REM now call on all subfolders...




:showfiles
echo %1
set /a I+=1
goto :eof


for the record, Tan_Za's code would be:


Code: [Select]
@echo off
title File Counter by Tan_Za
SET count=1
FOR /f "tokens=*" %%G IN ('dir /b') DO (call :s_do_sums "%%G")
echo Any key to continue
echo made by Tan_Za. any questions email me at:tanza.lince@gmail.com
pause >nul
GOTO :eof
:s_do_sums
echo %count%:%1
set /a count+=1
GOTO :eof


your measly EXE compression does nothing to dissuade me! MWA HA HA *cough*.

also, the recurse: label was a remnant from my almost successful attempt to iterate through subdirectories by afterwards using dir /b /ad in the for command to iterate on each folder and recurse on it with the batch file.
Turns out batch files don't like recursion.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Tan_Za
Intermediate



Posts: 134

Experience: Experienced
OS: Windows 7


Starcraft and Pascal programming YAY

« Reply #5 on: January 09, 2009, 11:55:51 PM »

Yeah batch to exe is a very easy thing to do and to reverse lol, but my code is a little complex for some peole because it sure took a while to do.

DAM you...

Not really, thats ok,
Tan_Za
IP logged
fireballs
Apprentice



Thanked: 3
Posts: 562

Code:Terminal

« Reply #6 on: January 10, 2009, 03:27:29 AM »

If you want to count all the files in the subdirectories as well (also it's smaller than the other two's) this should work:

Code: [Select]
@echo off
setlocal enabledelayedexpansion

for /f %%A in ('dir /s ^| find "File(s)"') do (set B=%%A+!B!)
set B=%B%0
set /a C=%B%
echo %C
pause
exit

FB
« Last Edit: January 10, 2009, 02:15:35 PM by fireballs » IP logged

Next time google it.
BatchFileCommand
Hopeful



Thanked: 1
Posts: 339




« Reply #7 on: January 10, 2009, 01:11:52 PM »

BC programmer, you're batch file counted the files. But then it just exited out.
IP logged

οτη άβγαλτος μεταφ βαθμολογία
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,881

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #8 on: January 10, 2009, 02:15:54 PM »

BC programmer, you're batch file counted the files. But then it just exited out.

ahh yes, just add "Pause" right before the ":showfiles" line.
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
BatchFileCommand
Hopeful



Thanked: 1
Posts: 339




« Reply #9 on: January 10, 2009, 02:55:48 PM »

It still doesn't seem to work, it pauses in the beginning before it counts the file. I need it to pause after it's counted all the files.
IP logged

οτη άβγαλτος μεταφ βαθμολογία
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,881

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #10 on: January 10, 2009, 03:18:22 PM »

don't put pause at the end of the batch, but rather in the same area that Tan_Za has placed their pause, which is right before the subroutine.

here, I also fixed a bug involving a forgotten GOTO :EOF, not sure if it was affecting the program or not, though.

Code: [Select]
@echo off
title File Counter batch.
:recurse
set I=1

echo "files in folder"
cd
REM view all files, EXCEPT directories.
FOR /f "tokens=*" %%P IN ('dir /A-d /b') do (call :showfiles "%%P")
echo Filecount: %I%
REM now call on all subfolders...



PAUSE


goto :eof
:showfiles
echo %1
set /a I+=1
goto :eof
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
rbird
Newbie



Posts: 1


« Reply #11 on: February 11, 2009, 03:18:57 AM »

What's wrong with:

dir /b /s /A-d c:\temp | find "" /v /n /c

You can use blat (www.blat.net) to send the email
« Last Edit: February 11, 2009, 04:53:37 AM by rbird » IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,881

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #12 on: February 11, 2009, 03:28:43 AM »

What's wrong with:

dir /b /s /A-d c:\temp | find "" /v /n /c

why, it's too easy, of course! redirect that to a variable and he'd be good to go :)

bit of a topic bump, but for a good reason from somebody who knows their switches  8).

EDIT: welcome to CH!
IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
TechAbhi
Greenhorn



Posts: 6


« Reply #13 on: May 16, 2010, 11:55:42 AM »

Hello All,

I would like  to count the number of files from a folder and only count the files from yesterday. And email the count to a specific email address. I would set this on a Task Scheduler to daily to get a count and see how many files are added to the folder. I tried creating a batch file
dir /a "C:xx\" |find /c /v ""

But this gives me a count of everything in the folder, i just need a count from yesterday and also i dont know how to email the count.
Please help!
IP logged
Helpmeh
Egghead



Thanked: 117
Posts: 3,608

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #14 on: May 16, 2010, 01:18:49 PM »

Hello All,

I would like  to count the number of files from a folder and only count the files from yesterday. And email the count to a specific email address. I would set this on a Task Scheduler to daily to get a count and see how many files are added to the folder. I tried creating a batch file
dir /a "C:xx\" |find /c /v ""

But this gives me a count of everything in the folder, i just need a count from yesterday and also i dont know how to email the count.
Please help!
Please make your own thread instead of bumping one from last year.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
Pages: [1] 2  All - (Top) Print 
Home / Microsoft / Microsoft DOS / Creating batch file to count number of files in a directory « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.147 seconds with 20 queries.