38

I am very new to this. Please help me

I was trying to write a batch file program to count number of files in a folder and assign that to a variable and display it to verify that it has been stored please help me with the syntax,

thank you in advance -VK

infused
  • 23,247
  • 13
  • 66
  • 76
user1452157
  • 389
  • 1
  • 3
  • 3

10 Answers10

61

I'm going to assume you do not want to count hidden or system files.

There are many ways to do this. All of the methods that I will show involve some form of the FOR command. There are many variations of the FOR command that look almost the same, but they behave very differently. It can be confusing for a beginner.

You can get help by typing HELP FOR or FOR /? from the command line. But that help is a bit cryptic if you are not used to reading it.

1) The DIR command lists the number of files in the directory. You can pipe the results of DIR to FIND to get the relevant line and then use FOR /F to parse the desired value from the line. The problem with this technique is the string you search for has to change depending on the language used by the operating system.

@echo off
for /f %%A in ('dir ^| find "File(s)"') do set cnt=%%A
echo File count = %cnt%

2) You can use DIR /B /A-D-H-S to list the non-hidden/non-system files without other info, pipe the result to FIND to count the number of files, and use FOR /F to read the result.

@echo off
for /f %%A in ('dir /a-d-s-h /b ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%

3) You can use a simple FOR to enumerate all the files and SET /A to increment a counter for each file found.

@echo off
set cnt=0
for %%A in (*) do set /a cnt+=1
echo File count = %cnt%
InteXX
  • 5,576
  • 6
  • 37
  • 62
dbenham
  • 123,415
  • 27
  • 239
  • 376
  • How do you specify the dir in which you want to count in the first method?? – LoukMouk Aug 09 '18 at 13:56
  • 3
    @LoukMo - The answer assumes current directory. To specify a different folder, simply put the path after the DIR command. – dbenham Aug 09 '18 at 14:11
13
@echo off
setlocal enableextensions
set count=0
for %%x in (*.txt) do set /a count+=1
echo %count%
endlocal
pause

This is the best.... your variable is: %count%

NOTE: you can change (*.txt) to any other file extension to count other files.....

Alexander Vogt
  • 17,491
  • 13
  • 49
  • 66
Steve IT
  • 164
  • 1
  • 8
  • This works well, but note that files with the `System` or `Hidden` attribute (eg. `attrib +s MyFile.txt`) are counted with this script but are not listed with `dir`! – AlainD Apr 05 '20 at 14:43
  • 1
    This is conceptually identical to the 3rd option in [my answer](https://stackoverflow.com/a/11005300/1012053) from 3 years earlier. Why post an identical answer? – dbenham Feb 05 '21 at 16:37
8

The mugume david answer fails on an empty folder; Count is 1 instead of a 0 when looking for a pattern rather than all files. For example *.xml

This works for me:

attrib.exe /s ./*.xml | find /v "File not found - " | find /c /v ""

Community
  • 1
  • 1
Milan
  • 614
  • 5
  • 6
7

The fastest code for counting files with ANY attributes in folder %FOLDER% and its subfolders is the following. The code is for script in a command script (batch) file.

@for /f %%a in ('2^>nul dir "%FOLDER%" /a-d/b/-o/-p/s^|find /v /c ""') do set n=%%a
@echo Total files: %n%.
InteXX
  • 5,576
  • 6
  • 37
  • 62
green
  • 335
  • 2
  • 10
7

This might be a bit faster:

dir /A:-D /B *.* 2>nul | find /c /v ""
`/A:-D` - filters out only non directory items (files)
`/B`    - prints only file names (no need a full path request)
`*.*`   - can filters out specific file names (currently - all)
`2>nul` - suppresses all error lines output to does not count them
Andry
  • 1,707
  • 21
  • 26
2

Change into the directory and;

attrib.exe /s ./*.* |find /c /v ""

EDIT

I presumed that would be simple to discover. use

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "batchfile.bat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

I run this and the variable output was holding this

D:\VSS\USSD V3.0\WTU.USSD\USSDConsole\bin\Debug>attrib.exe /s ./*.*   | find /c /v "" 13

where 13 is the file count. It should solve the issue

phuclv
  • 32,499
  • 12
  • 130
  • 417
mugume david
  • 627
  • 9
  • 18
  • The OP did not ask to count files in sub-folders, so I don't see the reason for `/s` option. The OP did ask for the result to be stored in a variable, and your code does not do that. – dbenham May 18 '13 at 16:02
1

for /F "tokens=1" %a in ('dir ^| findstr "File(s)"') do echo %a

Result:

C:\MyDir> for /F "tokens=1" %a in ('dir ^| findstr "File(s)"') do @set FILE_COUNT=%a

C:\MyDir> echo %FILE_COUNT%
4   // <== There's your answer
abelenky
  • 61,055
  • 22
  • 102
  • 154
1
FOR /f "delims=" %%i IN ('attrib.exe ./*.* ^| find /v "File not found - " ^| find /c /v ""') DO SET myVar=%%i
ECHO %myVar%

This is based on the (much) earlier post that points out that the count would be wrong for an empty directory if you use DIR rather than attrib.exe.

For anyone else who got stuck on the syntax for putting the command in a FOR loop, enclose the command in single quotes (assuming it doesn't contain them) and escape pipes with ^.

trapper_hag
  • 731
  • 8
  • 14
1

I have used a temporary file to do this in the past, like this below.

DIR /B *.DAT | FIND.EXE /C /V "" > COUNT.TXT

FOR /F "tokens=1" %%f IN (COUNT.TXT) DO (
IF NOT %%f==6 SET _MSG=File count is %%f, and 6 were expected. & DEL COUNT.TXT & ECHO #### ERROR - FILE COUNT WAS %%f AND 6 WERE EXPECTED. #### >> %_LOGFILE% & GOTO SENDMAIL
)
1

With a for loop:

FOR /F %i IN ('dir /b /a-d "%cd%" ^| find /v /c "?"') DO set /a count=%i
echo %count%

Without/avoiding for loop:

(dir /b /a-d ^| find /v /c "?") | (set /p myVar=& cmd /c exit /b %myVar%)
set count=%errorlevel%
echo %count%

Tested in Win 10 cmd

Zimba
  • 2,051
  • 15
  • 21
  • 1
    The first one is already shown, but the second one is interesting! But could be simplified to `dir /b /a-d | find /v /c "" | (set /p cnt= & cmd /c exit /b %cnt%)`. Btw to get it working in a batch file, the last part has to be changed to `cmd /c exit /b %%cnt%%` – jeb Sep 17 '21 at 21:27
  • Hehe I knew you'd like it; I haven't seen anyone do it before :) – Zimba Sep 18 '21 at 20:50