1.

Solve : Execute batch script depending on no. files in folder?

Answer»

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,
Puplithe code fragment to get the count of files in a folder:
Code: [Select]dir/a-d/b/s "c:\test"|find/c /v""Quote from: Prince_ on February 09, 2010, 03:58:20 AM

the code fragment to get the count of files in a folder:
Code: [Select]dir/a-d/b/s "c:\test"|find/c /v""
Not sure if that works, but this should do the trick.

Dir /b FOLDERNAME>dir.txt
for /f %%a in (dir.txt) do set /a count+=1
echo Number of files in the folder is %count%.
Pause > nulQuote from: Pupli on February 09, 2010, 02:45:59 AM
Number of files stored in a particular folder ?


C:\batch>type countfiles.bat
Code: [Select]@echo off

dir *.txt | wc -l
Output:
C:\batch>countfiles.bat
101 files

C:\batch>

p.s. We need to see the code for "C:\Dir1\job.bat" in order to modify to use the above count information.
Quote from: Pupli on February 09, 2010, 02:45:59 AM
Number of files stored in a particular folder?

NAME
wc -l (line count ) - lines in files
SYNOPSIS
wc [OPTION]... [FILE]...
DESCRIPTION
Print byte, word, and newline counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input.

-l, --lines
print the newline counts

http://linux.about.com/library/cmd/blcmdl1_wc.htmwc is a very good Linux/Unix utility but in view of the fact that the OP says

Quote
I have this .bat script

and POSTED in an "MS-DOS" forum, I think they would be well advised to get a version compiled for Win32, for example the one contained in the excellent GNU Core Utils.
Code: [Select]Dir /b FOLDERNAME>dir.txt
for /f %%a in (dir.txt) do set /a count+=1
echo Number of files in the folder is %count%.
Pause > nul
Yes HELPMEH ,
This snippet of code, will do the trick.
But, what if we want to test the %count% variable???

For example:

Code: [Select]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

I think that:
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 being executed correctly.

I am right For /l %%b in (1,1,%count%) do ...

Just a little mistake. No biggie. Can you please tell me the difference

Quote
(1,1,%count%) vs. (1 1 %count%)
Becouse, it keeps executing fine in both ways !!!


P.S.
...and what about the IF condition inside the FOR loop, ... am I coding it the right way?


Thnx,
PupliQuote from: Pupli on February 10, 2010, 05:22:45 AM
Can you please tell me the difference

No difference. The documentation shows a comma but in fact it works with just a space.

Quote
and what about the IF condition inside the FOR loop, ... am I coding it the right way?

It isn't inside a loop.
Quote
It isn't inside a loop.


Oh no,
So, I have conditioned nothing regarding %count%==0

Can anyone help?
Quote
I just want my batch to test if %count%==0, and if true, just generate a .log file to explain this, and after it, exit (suspend execution)

Can it be:

Code: [Select]dir /b /a-d 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
) else (
for /L %%b in (1 1 %count%) do call test.bat %%b
echo.>> FolderCounter.log %count% files werre loaded
move FolderCounter.log Dir1
)
Is this code a valid batch script language?
Code: [Select]dir /b /a-d particular_Folder>dir_p_f.txt
set count=0
for /f %%a in (dir_p_f.txt) do (set /a count+=1)
if %count% EQU 0 (
echo No files found in the specified folder>>FolderCounter.log
) else (
for /L %%b in (1 1 %count%) do call test.bat %%b
echo %count% files were loaded>>FolderCounter.log
)
move FolderCounter.log Dir1
Tidied up a bit; corrected spelling of 'were'.
Thank you Salmon Trout,

Thnx again



Discussion

No Comment Found