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 108414 times)

0 Members and 1 Guest are viewing this topic.

ljones123

    Topic Starter


    Newbie

    Creating batch file to count number of files in a directory
    « 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.

    Helpmeh



      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: Creating batch file to count number of files in a directory
    « 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.
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    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 #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.

    I was trying to dereference Null Pointers before it was cool.

    Tan_Za



      Intermediate
    • Starcraft and C programming
      • Experience: Experienced
      • OS: Windows 7
      Re: Creating batch file to count number of files in a directory
      « 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]

      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 #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:[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.
      I was trying to dereference Null Pointers before it was cool.

      Tan_Za



        Intermediate
      • Starcraft and C programming
        • Experience: Experienced
        • OS: Windows 7
        Re: Creating batch file to count number of files in a directory
        « 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

        fireballs



          Apprentice

        • Code:Terminal
        • Thanked: 3
          Re: Creating batch file to count number of files in a directory
          « 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 »
          Next time google it.

          BatchFileCommand



            Hopeful
          • Thanked: 1
            Re: Creating batch file to count number of files in a directory
            « 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.
            οτη άβγαλτος μεταφ βαθμολογία

            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 #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.
            I was trying to dereference Null Pointers before it was cool.

            BatchFileCommand



              Hopeful
            • Thanked: 1
              Re: Creating batch file to count number of files in a directory
              « 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.
              οτη άβγαλτος μεταφ βαθμολογία

              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 #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
              I was trying to dereference Null Pointers before it was cool.

              rbird

              • Guest
              Re: Creating batch file to count number of files in a directory
              « 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 »

              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 #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!
              I was trying to dereference Null Pointers before it was cool.

              TechAbhi



                Greenhorn

                Re: Creating batch file to count number of files in a directory
                « 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!

                Helpmeh



                  Guru

                • Roar.
                • Thanked: 123
                  • Yes
                  • Yes
                • Computer: Specs
                • Experience: Familiar
                • OS: Windows 8
                Re: Creating batch file to count number of files in a directory
                « 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.
                Where's MagicSpeed?
                Quote from: 'matt'
                He's playing a game called IRL. Great graphics, *censored* gameplay.