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?

1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.