Home / Microsoft / Microsoft DOS / Execute batch script depending on no. files in folder
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: Execute batch script depending on no. files in folder  (Read 757 times)
Pupli
Topic Starter
Rookie



Posts: 14


« on: February 09, 2010, 02:45:59 AM »

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
IP logged
Prince_
Beginner



Thanked: 5
Posts: 63


« Reply #1 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""
IP logged
Helpmeh
Egghead



Thanked: 116
Posts: 3,583

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #2 on: February 09, 2010, 04:38:02 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 > nul
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
BillRichardson
Intermediate



Thanked: 15
Posts: 194


« Reply #3 on: February 09, 2010, 08:26:46 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.
« Last Edit: February 09, 2010, 08:46:38 AM by BillRichardson » IP logged

Bill Richardson
BillRichardson
Intermediate



Thanked: 15
Posts: 194


« Reply #4 on: February 09, 2010, 08:33:39 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.htm
IP logged

Bill Richardson
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #5 on: February 09, 2010, 12:36:13 PM »

wc 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.
IP logged

Pupli
Topic Starter
Rookie



Posts: 14


« Reply #6 on: February 10, 2010, 02:31:04 AM »

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 ??? ::)
IP logged
Helpmeh
Egghead



Thanked: 116
Posts: 3,583

Experience: Experienced
OS: Windows XP


Roar.

1
« Reply #7 on: February 10, 2010, 04:40:18 AM »

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

Just a little mistake. No biggie.
IP logged

Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.
Pupli
Topic Starter
Rookie



Posts: 14


« Reply #8 on: February 10, 2010, 05:22:45 AM »

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,
Pupli
IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #9 on: February 10, 2010, 11:22:48 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.
IP logged

Pupli
Topic Starter
Rookie



Posts: 14


« Reply #10 on: February 11, 2010, 12:17:38 AM »

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?
 :)
« Last Edit: February 11, 2010, 12:38:03 AM by Pupli » IP logged
Salmon Trout
Prodigy



Thanked: 501
Posts: 7,365

Computer: Specs
Experience: Guru
OS: Linux variant

1
« Reply #11 on: February 11, 2010, 11:26:13 AM »

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'.
IP logged

Pupli
Topic Starter
Rookie



Posts: 14


« Reply #12 on: February 12, 2010, 01:01:47 AM »

Thank you Salmon Trout,

Thnx again

:)
IP logged
Pages: [1] - (Top) Print 
Home / Microsoft / Microsoft DOS / Execute batch script depending on no. files in folder « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.121 seconds with 21 queries.