Computing.Net > Forums > Disk Operating System > count the file numbers in a folder

count the file numbers in a folder

Reply to Message Icon

Original Message
Name: george (by gp)
Date: May 29, 2008 at 22:26:00 Pacific
Subject: count the file numbers in a folder
OS: xp
CPU/Ram: dell
Model/Manufacturer: dell
Comment:

Hi, I want to write a batch file to do:

if "number of files in a directory" gtr 10
( command-1.exe
command-2.exe )

I know to count the file number is
dir /b | find /c /v ""
but how to put it into the above batch commands? pls help. thanks.


Report Offensive Message For Removal


Response Number 1
Name: Mechanix2Go
Date: May 29, 2008 at 23:57:31 Pacific
Reply: (edit)

@echo off
setLocal EnableDelayedExpansion

if not %1'==' set src=%1 && goto :count

set /p src=which directory ?

:count

for /f "tokens=* delims= " %%a in ('dir /b/a-d !src!') do (
set /a C+=1
)

if !C! gtr 10 (
echo there are !C! files
echo do command1
) else (
echo there are !C! files; nothing done
)


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 2
Name: george (by gp)
Date: May 30, 2008 at 00:26:19 Pacific
Reply: (edit)

Thanks and it works.
However is there any simpler lines doing the job? similar like

if ('dir /b | find /c /v "" !src!') gtr 10
( do command1
do command2)

instead of your for-looping all files in the directory?

Thanks again.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: count the file numbers in a folder

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge