1.

Solve : Testing for number of Dir's in a batchfile?

Answer»

Hi all

I am trying to test for a number of dir's within a batchfile. Basically if the number of dir's EQUALS a particular number then the script will continue, if not it shoud exit. Something like this

If (dir /AD /B| wc -l)=6 goto cont
exit

:cont
blah blah

There does not seem to be a wc -l equivalent in Windows cmd !!

I have found a utility llc.exe that will count the lines for me but I am struggling with the structure. Could I put the result into a string and do an "if string1 == 6 goto cont" ?

Your help would be appreciated

TIA

Richso
=o=Here is a native cmd solution:
(It works if "#~#" isn´t in any foldername.)

for /f "tokens=*" %%a in ('dir/b *. ^| find /v /c "#~#"') do set count=%%a

if "%count%"=="6" (goto :cont) else (ECHO ... & goto :exit)

:cont

:exit
set count=
:eof


I didn´t try wc yet.

hope this helps
uliHi uli_glueck

Yes your code almost works !

The trouble is for some reason the if statement has an extra space in it after the %count% so as you can see below you get "6 ". The if statement then fails and goes to exit. I guess I could put space after the second 6 but wondered if you knew what could cause the first space. I cant see it myself.

if "6 " == "6" (goto :cont ) else (echo ... & goto :exit )

Thanks

Richso
=o=Oh, sorry. I didn´t have this problem yet.

You can try:

if {%count%}=={6} (goto :cont) else (echo ... & goto :exit)

or

if {"%count%"}=={"6"} (goto :cont) else (echo ... & goto :exit)

I have no machine with a cmd at home, so I can't try it at the moment. There must be a workaround for this problem.
(It is a public holiday today, so I am not in the office) :-)

If this isn´t working you can do

set count=%count: =%

This will cancel eventually existing trailing spaces in the variable count.
Maybe not so elegant, but should work.

hope this helps
uli
Hi Uli

Another ISSUE with your code. It does NOT seem to like double figures. If you set the number you are matching against to say 12 like this -> {"%count%"}GEQ{"12"} and the %count% comes to say 9 then the logic falls over.

I suspect that it is only looking at the first number and as 9 is bigger than 1 it fails.

Any ideas how to RESOLVE this one please ?

Best regards


Richso
=o=Mentioning your OS would have been helpful. In any case this might work on your machine although I'm at a complete loss why it needs to be so redundant.

Code: [Select]@echo off
set count=0
for /d %%a in ('dir /a:d %cd%') do (
call set /a count=%%count%%+1
)
echo Number of sub-folders: %count%

8-)Hi Sidewinder

I thought I had mentioned my OS in my OP, anyway its Windows 2000.

The reason that its has to be "so redundant" is that the script is basically doing content deployment. I am using robocopy to do the actual deployment but beofre it can go ahead I have to know that the content is all there and the best way to do this is to test for the minimum number of dir's. I purge the source after each deployment and copy back in some static content before the new stuff gets added later.

Why dont we use a proper content deployment app I hear you ask, not my choice I can asure you !

Thanks again

Richso
=o=

The earlier code posted was incorrect. The results came up the same no matter what directory was used. This should be ok.

Code: [Select]@echo off
set count=0
for /f %%a in ('dir /a:d /b "%cd%"') do (
call set /a count=%%count%%+1
)
echo Number of sub-folders: %count%

I wasn't referring to your request as redundant; it was in reference to the code I posted.

Sorry about the confusion. 8-)Sidewinder

At the end of your code I am using:

if "%count%" GEQ "22" (goto :cont else (echo ... & goto :failed)

However, the %count% comes to 10 which means it should fail (i.e. it is less than 22) but it does'nt which suggests that only the first figure is being compared (i.e. 1 is less than 2)

What do you think ?


Regards

Richso
=o=Quote

if "%count%" GEQ "22" (goto :cont else (echo ... & goto :failed)

You could try getting rid of all those messy quotes (count has a value even if it might be zero). Also the IF statement syntax is incorrect:
Code: [Select]if %count% GEQ 22 (goto :cont) else (echo ... & goto :failed)
It's DOUBTFUL that only the first significant digit is being used in the compare.

Good luck. 8-)


Discussion

No Comment Found