Count the no. of files in a folder

February 9, 2010 at 01:37:58
Specs: Windows XP, Core2Duo/2GB
Hello everybody,

I have this .bat script which has been scheduled to be executed on a specific time of day.
The problem, is that I want to execute it as many times, as the number of files stored in a particular folder (to which this .bat Script is applied)

Can anybody help ?

Thnx in advance,
Pupli


See More: Count the no. of files in a folder

Report •


#1
February 9, 2010 at 02:05:53
maybe:
pushd partclr.fdr
for %%a in (*) do call test.bat "%%a"

Report •

#2
February 9, 2010 at 03:40:55
Hi nbrane,

May I ask you to explain to me your idea?

Looking, I find this "tip".

pre>dir/a-d/b/s "c:\Dir1\folder_Name"|find/c /v""<

I realize that this piece of code is going to give a number = count of files in a folder. Am I right?
If yes, how can I assign the value to a variable, and therefor use it in a FOR loop, in order to get my .batScript executed for the number of count

Regards,
Pupli


Report •

#3
February 9, 2010 at 13:47:28
the only way to capture outut from a command into a variable is to use a for loop, so you would need two for-loops. One to capture, and a second one to do the operations.
for /f %%a in ('dir /b/a-d' | find /v /c ""') do set count=%%a

add the /s option to "dir" only if you want a count of all files in all subdirectories. this version just gets a count of the root dir.
%count% now has the number you want:

for /L %%b in (1 1 %count%) do call test.bat %%b

note that test.bat does NOT receive the names of the files, just the counter! That is why i posted it the first way i did
(using just one for-loop and calling the batch with each fname)
but you might have reasons for wanting it this way.


Report •

Related Solutions

#4
February 10, 2010 at 00:07:21
yes you right,

the only way to capture outut from a command into a variable is to use a for loop, so you would need two for-loops.

I have one more big problem
It seems that:

for /f %%a in ('dir /b/a-d' | find /v /c ""') do set count=%%a

will count only files of type text (*.txt)
The problem is that the files that will be stored in the folder are of type *.dat

Can you help me, If I am right using this??
Am I missing anything here?

Dir /b particular_Folder>dir_p_f.txt
for /f %%a in (dir_p_f.txt) do set /a count+=1

Now, the number of files in the folder should be %count%
Can I countinue to use the second loop?
for /L %%b in (1 1 %count%) do call test.bat %%b


Report •

#5
February 10, 2010 at 00:15:52
I mean,

The above works fine, but I am not sure if I am covering everything???

For example:
I want to test, if there were any files in the folder when starting executing FirstBat.bat

************************************************
Dir /b particular_Folder>dir_p_f.txt
for /f %%a in (dir_p_f.txt) do set /a count+=1
if %count%==0 ( echo.>> FolderCounter.log No files found in the specified folder
move FolderCounter.log Dir1)
for /L %%b in (1 1 %count%) do call test.bat %%b
echo.>> FolderCounter.log %count% files werre loaded
move FolderCounter.log Dir1


Report •

#6
February 10, 2010 at 00:40:24
Definitely YES,

this piece of code:
echo.>> FolderCounter.log %count% files werre loaded
move FolderCounter.log Dir1

after:
for /L %%b in (1 1 %count%) do call test.bat %%b

will prevent the FOR loop, ???


P.S.
what if I replace (1 1 %count%) with (1,1,%count%)

It keeps executing fine, in both ways, but I've seen some syntax examples that require the usage of "comas"


Report •

#7
February 10, 2010 at 12:13:15
na, the commas don't make any difference, you can use either comma or space in the for-expression.
the only other changes are minor and not really necessary, more a matter of personal coding preferences:

if %count%==0 (
>> dir1\FolderCounter.log echo No files found in the specified folder
) else (
for /L %%b in (1 1 %count%) do call test.bat %%b
>> dir1\FolderCounter.log echo %count% files werre loaded
)

the only other thing, if there are ever any subdirectories in the dir where the .dat files are, the counter will inlcude them unless you put: /a-d onto the dir, like: dir /b /a-d


Report •

#8
February 10, 2010 at 16:04:27
Out of interest, why do you want to call test.bat as many times
as there are files, but not pass each filename to the batch file?
Seems an odd thing to do.

Report •

#9
February 10, 2010 at 23:41:51
why do you want to call test.bat as many times 
as there are files, but not pass each filename to the batch file?

Well, I thought I could make use of conditionals and error preventing more easily in this way.

Anyway, I would have been very greatful, if you show me how to achieve the other method passing each filename to the batch file.


Report •

#10
February 11, 2010 at 00:22:58
LoL!
post #1:
for %%a in (*) do call test.bat "%%a"

i think we have fallen into an infinite loop here!
(or a black hole).

pushd partclr.fdr
for %%a in (*.dat) do call test.bat "%%a"


Report •

#11
February 11, 2010 at 02:03:44
I thought that (*) was kinda "replace this with Dir\filename" :)))

Report •

#12
February 11, 2010 at 08:21:39
yes. what the FOR-loop does is:
FOR (each file, *, in the directory) SET %%a to the current filename, then do whatever else using that filename, then select the NEXT filename in the directory and REPEAT, until all files are done.
So if you have a dir with listing: file1, fileA, file9, fileX
then the FOR-loop selects each one, in succession, to be put into %%a and done-to:
%%a=file1
do-to %%a
%%a=fileA
do-to %%a
%%a=file9
do-to... ...

or maybe I'm missing something?
HTH


Report •

Ask Question