1

I tried the following code in a .bat file to count the number of files in a directory:

for /f %%a in ('dir /A-D /B /S | find /C /V ""') do set FILECOUNT=%%a
echo %FILECOUNT%
pause

However, it doesn't work and doesn't even pause. It instead flashes something like ": was unexpected at this time". If I just write

dir /A-D /B /S | find /C /V ""
pause

It works fine and displays the number of files but I want to save this number into a variable. What am I doing wrong?

Vika Marquez
  • 353
  • 1
  • 3
  • 12
  • 2
    Already answered http://stackoverflow.com/questions/11004045/batch-file-counting-number-of-files-in-folder-and-storing-in-a-variable – ziddarth Feb 02 '16 at 23:44

1 Answers1

4

You need to escape the pipe, so

@echo off
for /f %%a in ('dir /A-D /B /S ^| find /C /V ""') do set FILECOUNT=%%a
echo %FILECOUNT%
pause
Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • 2
    @J.Doe if my answer solved your problem, you should click the check to the left of it so people searching on google for the same problem can find it. – Dennis van Gils Feb 02 '16 at 23:46