Computer Hope

Microsoft => Microsoft DOS => Topic started by: ljones123 on January 08, 2009, 10:10:19 AM

Title: Creating batch file to count number of files in a directory
Post by: ljones123 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.
Title: Re: Creating batch file to count number of files in a directory
Post by: Helpmeh 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.
Title: Re: Creating batch file to count number of files in a directory
Post by: BC_Programmer 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.

Title: Re: Creating batch file to count number of files in a directory
Post by: Tan_Za 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]
Title: Re: Creating batch file to count number of files in a directory
Post by: BC_Programmer 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:[email protected]
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.
Title: Re: Creating batch file to count number of files in a directory
Post by: Tan_Za 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
Title: Re: Creating batch file to count number of files in a directory
Post by: fireballs 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
Title: Re: Creating batch file to count number of files in a directory
Post by: BatchFileCommand on January 10, 2009, 01:11:52 PM
BC programmer, you're batch file counted the files. But then it just exited out.
Title: Re: Creating batch file to count number of files in a directory
Post by: BC_Programmer 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.
Title: Re: Creating batch file to count number of files in a directory
Post by: BatchFileCommand 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.
Title: Re: Creating batch file to count number of files in a directory
Post by: BC_Programmer 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
Title: Re: Creating batch file to count number of files in a directory
Post by: rbird 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
Title: Re: Creating batch file to count number of files in a directory
Post by: BC_Programmer 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!
Title: Re: Creating batch file to count number of files in a directory
Post by: TechAbhi 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!
Title: Re: Creating batch file to count number of files in a directory
Post by: Helpmeh 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.
Title: Re: Creating batch file to count number of files in a directory
Post by: marvinengland on May 16, 2010, 01:43:37 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!


C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @file is new since 05/15/2010"

"gpl77.bat" is new since 05/15/2010
"newsflow.bat" is new since 05/15/2010
"null" is new since 05/15/2010
"st0510.bat" is new since 05/15/2010
"teststr.txt" is new since 05/15/2010
"tgk.txt" is new since 05/15/2010
"trk.txt" is new since 05/15/2010
"val.bat" is new since 05/15/2010
"zee4.bat" is new since 05/15/2010

C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @file is new since 05/15/2010"  | find /c /v ""
10

C:\test>
C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @fname is new since 05/15/2010"  | c:\bin\wc -l
      10

C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @fname is new since 05/15/2010"  | find /c /v ""
10

C:\test>



C:\test>dir /OD  |  findstr  "05/15/2010 05/16/2010"
05/15/2010  03:57 AM                14 trk.txt
05/15/2010  04:43 AM               129 gpl77.bat
05/15/2010  09:26 PM                 0 val.bat
05/15/2010  09:56 PM               138 zee4.bat
05/15/2010  09:57 PM                 0 null
05/15/2010  09:57 PM               150 tgk.txt
05/16/2010  04:19 AM               361 st0510.bat
05/16/2010  12:54 PM                 0 newsflow.bat
05/16/2010  12:56 PM    <DIR>          ..
05/16/2010  12:56 PM    <DIR>          .
05/16/2010  01:20 PM                92 teststr.txt

C:\test>
Title: Re: Creating batch file to count number of files in a directory
Post by: Sidewinder on May 16, 2010, 04:16:41 PM

C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @file is new since 05/15/2010"

"gpl77.bat" is new since 05/15/2010
"newsflow.bat" is new since 05/15/2010
"null" is new since 05/15/2010
"st0510.bat" is new since 05/15/2010
"teststr.txt" is new since 05/15/2010
"tgk.txt" is new since 05/15/2010
"trk.txt" is new since 05/15/2010
"val.bat" is new since 05/15/2010
"zee4.bat" is new since 05/15/2010

C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @file is new since 05/15/2010"  | find /c /v ""
10

C:\test>
C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @fname is new since 05/15/2010"  | c:\bin\wc -l
      10

C:\test>FORFILES /D 05/15/2010  /C "cmd /c echo @fname is new since 05/15/2010"  | find /c /v ""
10

C:\test>


I may have been the one child left behind but why is the count 10 when there are only 9 files listed?

Shouldn't source links be provided for both forfiles and wc? The OP has WinXP with neither installed by default.

Just asking,  8)
Title: Re: Creating batch file to count number of files in a directory
Post by: marvinengland on May 16, 2010, 04:53:22 PM
Why is the count 10 when there are only 9 files listed?

Shouldn't source links be provided for both forfiles and wc? The OP has WinXP with neither installed.

Yes the above post was quick code and you Sidewinder have my permission to correct the code and post the new and better code.  Thanks for your help.

There were other counters other than wc -l. Google [ unix wc ] for further information. We post for all readers and not just the original poster.  Our code is not production code. We are not being paid. ( Many of the posters asking questions are on a payroll for some company. )  Why do we help them?

For more information about the forfiles command:  forfiles /?  and/or google [ forfiles ]. The original poster should do part of the work to solve his problem.


C:\>forfiles /?

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description:
    Selects a file (or set of files) and executes a
    command on that file. This is helpful for batch jobs.

Parameter List:
    /P    pathname      Indicates the path to start searching.
                        The default folder is the current working
                        directory (.).

    /M    searchmask    Searches files according to a searchmask.
                        The default searchmask is '*' .

    /S                  Instructs forfiles to recurse into
                        subdirectories. Like "DIR /S".

    /C    command       Indicates the command to execute for each file.
                        Command strings should be wrapped in double
                        quotes.

                        The default command is "cmd /c echo @file".

                        The following variables can be used in the
                        command string:
                        @file    - returns the name of the file.
                        @fname   - returns the file name without
                                   extension.
                        @ext     - returns only the extension of the
                                   file.
                        @path    - returns the full path of the file.
                        @relpath - returns the relative path of the
                                   file.
                        @isdir   - returns "TRUE" if a file type is
                                   a directory, and "FALSE" for files.
                        @fsize   - returns the size of the file in
                                   bytes.
                        @fdate   - returns the last modified date of the
                                   file.
                        @ftime   - returns the last modified time of the
                                   file.

                        To include special characters in the command
                        line, use the hexadecimal code for the character
                        in 0xHH format (ex. 0x09 for tab). Internal
                        CMD.exe commands should be preceded with
                        "cmd /c".

    /D    date          Selects files with a last modified date greater
                        than or equal to (+), or less than or equal to
                        (-), the specified date using the
                        "MM/dd/yyyy" format; or selects files with a
                        last modified date greater than or equal to (+)
                        the current date plus "dd" days, or less than or
                        equal to (-) the current date minus "dd" days. A
                        valid "dd" number of days can be any number in
                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.

    /?                  Displays this help message.

Examples:
    FORFILES /?
    FORFILES
    FORFILES /P C:\WINDOWS /S /M DNS*.*
    FORFILES /S /M *.txt /C "cmd /c type @file | more"
    FORFILES /P C:\ /S /M *.bat
    FORFILES /D -30 /M *.exe
             /C "cmd /c echo @path 0x09 was changed 30 days ago"
    FORFILES /D 01/01/2001
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +5/16/2010 /C "cmd /c echo @fname is new today"
    FORFILES /M *.exe /D +1
    FORFILES /S /M *.doc /C "cmd /c echo @fsize"
    FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"

C:\>

P.S. : Sidewinder with his new and better code  will answer the question: Why is the count 10 when there are only 9 files listed? 

The absolute count is not necessary.

One solution offered above does not provide an absolute count but is close enough for Government work:

dir /OD  |  findstr  "05/15/2010 05/16/2010" | wc -l
C:\test>dir /OD  |  findstr  "05/15/2010 05/16/2010" | c:\bin\wc -l
      11


Thanks for your help Sidewinder.
Title: Re: Creating batch file to count number of files in a directory
Post by: Sidewinder on May 16, 2010, 07:22:27 PM
Exactly how is forfiles /? going to help someone that doesn't have the program installed on their system?

Quote
Yes the above post was quick code and you Sidewinder have my permission to correct the code and post the new and better code.

Thank you so much Marvin for the opportunity to bail you out yet again, but I'll pass. It is well known that if you find yourself in a hole, the first thing to do is to stop digging, but noooooo, you keep digging deeper:

Quote
The absolute count is not necessary.

One solution offered above does not provide an absolute count but is close enough for Government work:

With an attitude like that, it seems logical that your previous posts have been inaccurate, misleading or even destructive.

I thought when you upgraded to Win7 and discovered forfiles that you would head over to the Win7 board and leave the DOS backwater to those with a clue. <sigh> If only.

 8)
Title: Re: Creating batch file to count number of files in a directory
Post by: BC_Programmer on May 16, 2010, 07:51:14 PM

Quote
The absolute count is not necessary.


ahh, well then the count may as well be a random integer from 1 to 50.
Title: Re: Creating batch file to count number of files in a directory
Post by: marvinengland on May 16, 2010, 09:35:49 PM
I would like  to count the number of files from a folder and only count the files from yesterday.


C:\test>type Abhi.bat
Code: [Select]
@echo  off

for /f "delims=" %%i in ('cscript //nologo c:\test\evaluate.vbs "date -1"' ) do (
set  yesterday=%%i
echo yesterday=%yesterday%
)

set Today=%DATE:~4,10%

echo Today=%Today%

dir /OD  /A-D |  findstr  "%yesterday% %Today%"

dir /OD  /A-D |  findstr  "%yesterday% %Today%" | find /c /v "" > filecount.txt

echo  type filecount.txt

type filecount.txt

rem evaluate.vbs
rem Wscript.echo eval(WScript.Arguments(0))
rem Thank Salmon Trout for evaluate.vbs

Output:

C:\test>Abhi.bat
yesterday=5/15/2010
Today=05/16/2010
05/15/2010  03:57 AM                14 trk.txt
05/15/2010  04:43 AM               129 gpl77.bat
05/15/2010  09:56 PM               138 zee4.bat
05/15/2010  09:57 PM               150 tgk.txt
05/16/2010  04:19 AM               361 st0510.bat
05/16/2010  01:20 PM                92 teststr.txt
05/16/2010  09:37 PM               135 yesterday.bat
05/16/2010  10:29 PM                 3 filecount.txt
05/16/2010  10:32 PM               466 Abhi.bat
 type filecount.txt
9
C:\test>