trigger another script after it receive x number of files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doscmd
Posts: 2
Joined: 29 Jan 2012 09:22

trigger another script after it receive x number of files

#1 Post by doscmd » 29 Jan 2012 10:43

Hello,

I have a manual process which I update the MS SQL database with 4 files. I would like to automate this process by executing a script that will check the directory for 4 files and execute another script that will update the database.

Name and location of the files are (All files have basefile name "VEND":
d:\vendor\VEND_NAME
d:\vendor\VEND_ADDRESS
d:\vendor\VEND_PHONE
d:\vendor\VEND_NUMBER

Name and location of script to be trigger:
d:\script\dts.cmd

Script steps:
1. Find all files in d:vendor with base filename VEND
This is the command I found and tested: dir /b "d:\vendor\VEND*" | find /c /v ""

2. If there all 4 Vendor files (VEND*) found in step 1, execute the script dts.cmd

3. If there not all 4 Vendor files (VEND*) found in step 1, exit without executing the dts.cmd

Thank you in advance for your help

***Below is what I have so far, but need help to get the script to work***
===============================================
@ECHO OFF
:: Find if there are 4 Vendor files (VEND*) exist
dir /b "d:\vendor\VEND*" | find /c /v ""

:: If 4 Vendor files (VEND*) exist, run DTS script
if <<need help with syntax>>

goto RUNDTS

:: If 4 Vendor files (VEND*) NO exist, bail and exit script
ELSE
goto:EOF

:RUNDTS
d:\script\dts.cmd

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: trigger another script after it receive x number of file

#2 Post by aGerman » 29 Jan 2012 11:31

I assume you need only one line

Code: Select all

for /f %%i in ('dir /a-d /b "d:\vendor\VEND*"^|find /c /v ""') do if %%i==4 call "d:\script\dts.cmd"

The trick is to get the output of FIND into a variable. This can be done via FOR /F.

Regards
aGerman

doscmd
Posts: 2
Joined: 29 Jan 2012 09:22

Re: trigger another script after it receive x number of file

#3 Post by doscmd » 29 Jan 2012 13:38

aGerman,

Thank you. I tested the script and it works Great!

Thanks again.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: trigger another script after it receive x number of file

#4 Post by Aacini » 29 Jan 2012 22:13

I always think that simpler methods are easier to understand and modify, and run faster also:

Code: Select all

@echo off
set i=0
for %%f in (d:\vendor\VEND*) do set /A i=i+1
if %i% == 4 call d:\script\dts.cmd

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: trigger another script after it receive x number of file

#5 Post by dbenham » 29 Jan 2012 22:42

Both aGerman's and Aacini's solutions should check to see if the count is >= 4. Otherwise a 5th VEND* file will cause the solution to fail.

What if VEND_COUNTRY exists, but VEND_ADDRESS does not? It's probably safer to look for each required file explicitly:

Code: Select all

for %%f in (
  VEND_NAME
  VEND_ADDRESS
  VEND_PHONE
  VEND_NUMBER
) do if not exist d:\vendor\%%f exit /b
d:\script\dts.cmd


Dave Benham

Post Reply