Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Creating batch file to count number of files in a directory  (Read 109070 times)

0 Members and 1 Guest are viewing this topic.

marvinengland



    Hopeful

    Thanked: 11
    Re: Creating batch file to count number of files in a directory
    « Reply #15 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>
    « Last Edit: May 16, 2010, 02:00:27 PM by marvinengland »
    USA

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Creating batch file to count number of files in a directory
    « Reply #16 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)
    « Last Edit: May 16, 2010, 04:35:20 PM by Sidewinder »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    marvinengland



      Hopeful

      Thanked: 11
      Re: Creating batch file to count number of files in a directory
      « Reply #17 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.
      « Last Edit: May 16, 2010, 05:22:09 PM by marvinengland »
      USA

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Creating batch file to count number of files in a directory
      « Reply #18 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)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Creating batch file to count number of files in a directory
      « Reply #19 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.
      I was trying to dereference Null Pointers before it was cool.

      marvinengland



        Hopeful

        Thanked: 11
        Re: Creating batch file to count number of files in a directory
        « Reply #20 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>
        USA