2

So I'm going to count files in a directory including all subfolders and get a output as a number of how many files it found within the directory. The problem is I want to exclude all the folders that has the name error. Those files within that folder I do not want to be counted.

Simply...

for /r "\\server\share\folderA" %%G in (*.*) do (
set /A count=count + 1
)    
echo %count%

Under folderA there are plenty of subfolders that I'm counting, but also the "error" folders that I do not want to count.

So I'm trying the following...

Create a temp file called exclude.txt and type error to that file

if not exist "c:\temp" mkdir "c:\temp" 2>nul
if not exist c:\temp\exclude mkdir c:\temp\exclude 2>nul
echo error> %excludefolder%\exclude.txt

Now I want to combine this somehow. Basically do something like this...

for /r "\\server\share\folderA" %%G in (*.*) EXCLUDE: c:\temp\exclude\exclude.txt do (
set /A count=count + 1
)    

But I'm aware this will not work and I'm not sure how to work it out. Does anyone know? Thanks!

MadsTheMan
  • 705
  • 1
  • 10
  • 32

2 Answers2

3

Use DIR /S /B /A-D to iterate all files. The output includes the full path to each file.

Pipe that result to FINDSTR /L /I /V "\\error\\ to filter out paths that contain \error\. This will also exclude any folders within an error folder. The search could be modified to exclude only 'error' but include children of 'error'.

Pipe that result to FIND /C /V "" to count the number of files (lines).

dir /s /b /a-d | findstr /liv "\\error\\" | find /c /v ""

The above will simply display the count.

If you want to capture the count in a variable, then use FOR /F to parse the output of the above command. Unquoted poison characters like | must be escaped when used in FOR /F.

@echo off
for /f %%N in ('dir /s /b /a-d^|findstr /liv "\\error\\"^|find /c /v ""') do set count=%%N
echo count=%count%
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thanks for an great answer @dbenham. This seems to do the job. Just for the record, can I exclude more folders by using this setup? For example if I could add `"\\error\\" "\\anotherfolder\\"` after `/liv`? – MadsTheMan Feb 19 '16 at 13:40
  • 1
    @MadsTheMan - Almost like that. You would put all the exclusion folders in one string, delimited by spaces: `findstr /liv "\\error\\ \\anotherFolder\\"`. If the folder name includes spaces, then you would need multiple \C:string options: `findstr /iv /c:"\\error\\" /c:"\\another folder\\"`. Read the built in help using `help findstr` or `findstr /?`. Also, you might be interested in reading about undocumented features at http://stackoverflow.com/q/8844868/1012053 – dbenham Feb 19 '16 at 14:48
  • Thanks for the input, just perfect. And that link - gold value. – MadsTheMan Feb 19 '16 at 14:56
2

You can exclude from count the folder containing the string "error" :

@echo off
set count=0

for /r "\\server\share\folderA" %%a in (*.*) do (
  echo %%~pa | find /i "error" || set /A count+=1
)    
echo %count%

It's a good way if you sure that you just have ONE Error folder. If you have another folder containing the string "error" it will be excluded too from count.

SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • Thanks for your input. Worked like a charm on smaller folders, but when I tried it in a folder with 10k + files it went super slow. I have no idéa why, but I did learn something anywho. Again, thanks! – MadsTheMan Feb 19 '16 at 13:43
  • 2
    This method is slow because: **1.** It use a pipe that requires to run _two copies_ of `cmd.exe` (a 400 KB size file), one for each side of the pipe. **2.** It additionally run `find.exe`, a 16 KB size file. And the main reason: **3.** It execute previous steps _one time for each file name_! A faster approach would be to check if the name contain the search word using just internal commands: `set "name=%%~Pa" & if "!name:error=!" equ "%%~Pa" set /A count+=1`. If you want to test this method, don't forget to include `setlocal EnableDelayedExpansion` line at beginning of the program. – Aacini Feb 19 '16 at 20:25
  • @aacini Thanks for your explanation, I needed that. I'm going to test it and see how it works. :) – MadsTheMan Feb 22 '16 at 05:54