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
maybe:
pushd partclr.fdr
for %%a in (*) do call test.bat "%%a"
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 countRegards,
Pupli
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=%%aadd 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.
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 *.datCan 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+=1Now, 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
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
Definitely YES, this piece of code:
echo.>> FolderCounter.log %count% files werre loaded
move FolderCounter.log Dir1after:
for /L %%b in (1 1 %count%) do call test.bat %%bwill 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"
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
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.
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.
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"
I thought that (*) was kinda "replace this with Dir\filename" :)))
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