1

When I run a recursive directory search for files it outputs "File Not Found" if the folder is empty but echo %count% is outputting 0 like it should.

setlocal
set /a count=0
for /F %%N in ('dir/s/b/aa "folder\name\*.txt"^| find /c "::"') do set count=%%N
echo %count%

All I want is a batch file to count the number of text files recursively in a directory and output the result like 999 and show 0 if the folder is empty. I have tried many variations of >nul and tons of batch search scripts. I am out of unvisited Google links to look at anymore.

3 Answers 3

2
for /F %%N in ('dir/s/b/a-d "folder\name\*.txt" 2^>nul ^| find /v /c ""') do set count=%%N

There should be a caret(^) before the > to escape the redirector.

Sign up to request clarification or add additional context in comments.

Comments

1

I know it is maybe not what you are looking at but have you tried:

dir/s/c folder\name\*.txt

I think it does almost exactly what you are looking for.

4 Comments

This looks like almost exactly what I would of preferred to have lol. The only issue is it does not output plain numbers. Mine outputs a ton of information. Any idea on how to make it smaller? I added /b but then it output the full file name.
Which exact OS? /c should just output the directory name and count - can't try it myself at the moment on windows/dos as I am on Linux.
Fair enough, I am on Win7 x64. imageshack.com/a/img163/5475/318c.png no big deal, I am going to look into it some more but yeah, not quite working.
Due to not wanting to spent a ton of time on this, I am just going to go with Joey's idea and Magoo's fix. But I appreciate it, it was very close to perfect if it had worked!
1

File not found is emitted to the standard error stream. You can silence that by redirecting it with 2>nul:

for /F %%N in ('dir/s/b/aa "folder\name\*.txt" 2^>nul ^| find /v /c ""') do set count=%%N

1 Comment

This seemed like a good idea, but the one you gave me makes errors and crashes. "2> Was unexpected at this time"

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.