Counting files in folder and compare with the number.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stg68
Posts: 4
Joined: 07 Sep 2011 01:14

Counting files in folder and compare with the number.

#1 Post by stg68 » 09 Sep 2011 18:12

Hello,

I am trying to count files in the folder and then compare with the number. Cannot make it to work.
Your help will be appreciated:

SET TotalExpected=8

setlocal enabledelayedexpansion

SET count=dir "C:\temp" /a-d /b/s |find /v /c "::"

ECHO %count%>totalfiles.log

IF %TotalExpected% GTR %COUNT% GOTO ERROR1

Thanks!

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Counting files in folder and compare with the number.

#2 Post by trebor68 » 10 Sep 2011 00:13

Your code is wrong.
row: SET count=dir "C:\temp" /a-d /b/s |find /v /c "::"

Here the correct code:

Code: Select all

SET TotalExpected=8

setlocal enabledelayedexpansion

for /f %%a in ('dir "C:\temp" /a-d /b/s ^|find /v /c "::"') do set count=%%a

ECHO %count%>totalfiles.log

IF %TotalExpected% GTR %COUNT% GOTO ERROR1

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Counting files in folder and compare with the number.

#3 Post by dbenham » 10 Sep 2011 00:20

trebor68 posted the solution seconds before I was ready to post the same solution :lol:

There are 2 additional very minor points:

1) The search for all lines not containing "::" will work, but it is easier to simply search for lines that do not contain the empty string "". (I'm not totally sure why this works, but it is a generic way of preserving all non empty lines using FIND)

2) You are enabling delayed expansion, but you are not using it. Perhaps you are using it elsewhere in your script :?:


Dave Benham

stg68
Posts: 4
Joined: 07 Sep 2011 01:14

Re: Counting files in folder and compare with the number.

#4 Post by stg68 » 10 Sep 2011 10:55

Thank to dbenham and trebor68!

PS I will remove enabling delayed expansion

Post Reply