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

Author Topic: Create .Bat file to count files in folder  (Read 23053 times)

0 Members and 1 Guest are viewing this topic.

petreli

    Topic Starter


    Rookie

    Create .Bat file to count files in folder
    « on: December 08, 2008, 11:29:54 AM »
    What do I need to create a txt file which lists and counts files in folders. Directory and sub directory

    For Example:

    c:Test\List of Folders

    Folder Name: Number of Files

    Folder 1: 32 (files/docs)

    Folder 2: 28 (files/docs)

    Folder 3: 23 (files/docs)

    And a more tricky question, if the folder contains .docs created by a mailmerge, is it possible to return the number of letters contained within the mailmerge?

    I mailmerge would contain multiple individual letters

    diablo416



      Hopeful
      • Experience: Beginner
      • OS: Unknown
      Re: Create .Bat file to count files in folder
      « Reply #1 on: December 08, 2008, 12:18:31 PM »
      @ECHO OFF
      set c=0
      :TOP
      for /f "tokens=1*" %%a in ('dir /a * /b /s') do (
             call set /a c=%%c%%+1
      )
      CLS&ECHO %c% Files.







      diablo416



        Hopeful
        • Experience: Beginner
        • OS: Unknown
        Re: Create .Bat file to count files in folder
        « Reply #2 on: December 08, 2008, 12:20:22 PM »
        place it in the folder you want a file count in, or place it in your root folder to count all files on your system,  the /a switch includes hidden files and the /s switch includes all sub directorys

        petreli

          Topic Starter


          Rookie

          Re: Create .Bat file to count files in folder
          « Reply #3 on: December 08, 2008, 12:34:50 PM »
          Thanks Diablo

          But it doesn't work, the command just sits

          would like it to be placed in a txt file to

          petreli

            Topic Starter


            Rookie

            Re: Create .Bat file to count files in folder
            « Reply #4 on: December 08, 2008, 12:51:14 PM »
            sorry it does work

            added pause so i could see

            how do i add it to a txt file.

            and break it down so it lists the sub-directories with each amount

            petreli

              Topic Starter


              Rookie

              Re: Create .Bat file to count files in folder
              « Reply #5 on: December 08, 2008, 01:40:31 PM »
              I'm really trying

              cmd /c "dir C:\Users\Jbloggs\Desktop\bbs > c:\temp\output.txt"

              it returns:

               Volume in drive C is OS
               Volume Serial Number is xxxxxxxx

               Directory of C:\Users\JbloggsDesktop\bbs

              08/12/2008  20:01    <DIR>          .
              08/12/2008  20:01    <DIR>          ..
              08/12/2008  20:26                63 co1.bat
              03/12/2008  15:41    <DIR>          test1
              03/12/2008  16:48    <DIR>          test2
                             1 File(s)             63 bytes
                             4 Dir(s)  395,136,864,256 bytes free

              How do I tweak it, to read in a more plain english



              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: Create .Bat file to count files in folder
              « Reply #6 on: December 08, 2008, 04:20:23 PM »
              This may help:

              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              set folder=C:\Users\Jbloggs\Desktop\bbs
              for /f "tokens=* delims=" %%v in ('dir /a:d /s /b "%folder%"') do (
              for /f "tokens=* delims=" %%i in ('dir /a:-d /b "%%v" 2^>^&1') do (
              if /i %%i NEQ "File Not Found" call set /a count=%%count%%+1
              )
              echo %%v: !count! (files/docs^)
              set count=0
              )

              Be sure to set the folder variable correctly.

              Good luck. 8)

              EDIT: In the interest of accuracy and future readers, the code was modified to correct a syntax error.
              « Last Edit: December 15, 2008, 06:07:29 PM by Sidewinder »
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              petreli

                Topic Starter


                Rookie

                Re: Create .Bat file to count files in folder
                « Reply #7 on: December 09, 2008, 01:29:41 AM »
                i think may a bit more difficult than expected

                appreciate the help, sidewider. your code works, but i don't need to see the file path and want it to display in txt file

                but what i'm after is

                a simple display to a txt file

                List the directory of folders and sub folders with how much files each contains

                ie

                Folder 1 = 34
                Folder 2 = 23
                etc

                i'm learning alot for you guys.. thanks

                Sidewinder



                  Guru

                  Thanked: 139
                • Experience: Familiar
                • OS: Windows 10
                Re: Create .Bat file to count files in folder
                « Reply #8 on: December 09, 2008, 03:54:04 AM »
                A tweak here, a tweak there, soon you have a new variation of the same old song:

                Code: [Select]
                @echo off
                setlocal enabledelayedexpansion
                set folder=C:\Users\Jbloggs\Desktop\bbs
                for /f "tokens=* delims=" %%v in ('dir /a:d /s /b "%folder%"') do (
                for /f "tokens=* delims=" %%i in ('dir /a:-d /b "%%v" 2^>^&1') do (
                if /i %%i NEQ "File Not Found" call set /a count=%%count+1
                )
                echo %%~nv: !count! >> textfile.txt
                set count=0
                )

                As before, make sure the folder variable is set correctly. For a lack of imagination, the output file is labeled textfile.txt

                Quote
                And a more tricky question, if the folder contains .docs created by a mailmerge, is it possible to return the number of letters contained within the mailmerge?

                What method do you use to split out each letter? Seems like a third party program would be involved. Need more details.

                 8)
                « Last Edit: December 09, 2008, 04:13:37 AM by Sidewinder »
                The true sign of intelligence is not knowledge but imagination.

                -- Albert Einstein

                petreli

                  Topic Starter


                  Rookie

                  Re: Create .Bat file to count files in folder
                  « Reply #9 on: December 09, 2008, 06:33:58 AM »
                  Thanks Sidewinder

                  I will have to play around with the code.

                  With regards to the mailmerge. These are number of letters (.doc) contained within word.

                  To split I have the start and end of each letter identified with a flag

                  <begin> and <end>

                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: Create .Bat file to count files in folder
                  « Reply #10 on: December 09, 2008, 08:19:55 AM »
                  Quote
                  With regards to the mailmerge. These are number of letters (.doc) contained within word.

                  To split I have the start and end of each letter identified with a flag

                  <begin> and <end>

                  For this you'll either need to write an internal Word macro (VBA) or an external script using any of the COM aware script languages (VBScript, JScript, Python, REXX, etc.)

                  This link may give you some ideas about scripting Word using VBScript.

                  Good luck.  8)
                  The true sign of intelligence is not knowledge but imagination.

                  -- Albert Einstein