Solved

Batch file (or VB) to check a directory, if the directory is not empty, it will run a command for blat.exe

Posted on 2010-09-16
7
810 Views
Last Modified: 2012-05-10
Hello,

I need a batch file or vb script that will check a directory, and if the directory is not empty it will run a command (blat.exe command) or it can run another batch file, which is just fine with me.

And after it's done running the blat.exe command, i'd like it to remove all files from the directory it was just inspecting.

--Paul
0
Comment
Question by:paulpp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:ahmadpj
ID: 33689387
VB has some statement to work with files and directories, some code like this may be the solution:


If Dir("c:\backup", vbDirectory) <> "" Then   'means there is a directory (named "backup") in drive C
 If Dir("c:\backup\*.*", vbNormal) <> "" Then   'means that directory is not empty (but don't check hidden or system files)
   Shell "c:\blat.exe", vbNormalNoFocus
   DoEvents
   Kill "c:\backup\*.*"
 end if
end if


0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 33689525
The ahmadpj's code is almost OK... with a possible exception. Note that using Shell command to launch blat.exe does not wait until blat.exe terminates execution, so if blat.exe is doing something with the files in the c:\backup directory probably it will fail because as soon as blat.exe is launched, the files are deleted (instruction Kill).

So I would recommend the use of a "Shell and Wait" function for VB. There are so many variables of this function. This is one of the best for me:
http://www.cpearson.com/excel/ShellAndWait.aspx

Using it you're sure that blat.exe has ended when you kill the files.

Hope that helps.
0
 
LVL 12

Accepted Solution

by:
Ben Personick (Previously QCubed) earned 500 total points
ID: 33690042
here it is in real batch code instead of psuedo code.  Pls See attached.

ECHO OFF

::- Set variables here:
SET "CheckDir=C:\Test\another folder\EmptyFolder"
SET "DirStatus=EMPTY"
SET "BlatCmd=C:\Admin\Blat.exe"
SET "MailServer=your smtp server's FQDN or IP address"
SET "MailTo=YourName@yourDomain.com"
SET "Sender=YourName@yourDomain.com"
SET "Subject=Your Subject"
SET "Body=Your Text for the main part of the email goes here"

::- Check if the directoyr is Empty, if it is go to the :End-Script Lable
DIR "%CheckDir%\*" /A:-D /B 2>&1 | FIND /I "File Not Found" > nul && GOTO End-Script

::- If the directory was nto empty the script will now execute these lines

::-  Blat command with variables as defined above
"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%

::- Delete All files from CheckDir
DEL /Q /F "%CheckDir%\" *

::- Where the script will go to if the directory was empty
:End-Script
Echo The Directory is empty, the script now moved to this location
GOTO :EOF

Open in new window

0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 2

Author Comment

by:paulpp
ID: 33694869
Hello QCubed,

100% perfect. Thanks so much !!!


--Paul
0
 
LVL 12
ID: 33696160
Awesome!  Glad I could help!  :)
0
 
LVL 12
ID: 33706246
Here I buit this script for you, it can be tightened up but this is a working script to accomplish what you need.

Please note that the script uses "BLAT.exe" which is a free third party tool you can use to send email from the command line. =)

::----------------------------------------------------------------------------::
:: Script: CheckTaskRunTime.bat												  ::
:: Version: 1.0																  ::
:: Copyright: Ben Personick													  ::
:: Date: 2010-09-17															  ::
:: 																			  ::
:: Desc: If a task int he list is Running too long email someone.			  ::
:: 																			  ::
::----------------------------------------------------------------------------::
:Begin-Script
	@ECHO OFF
	::- Set Variables Here.
	SET TaskList="Google Software Updater","GoogleUpdateTaskMachineCore"
	SET "StatusToMatch=Ready"
	SET "MaxMinuts=30"
	SET "DEBUG=OFF"
	SET "BlatCmd=C:\Admin\Blat.exe"
	SET "MailServer=your smtp server's FQDN or IP address"
	SET "MailTo=YourName@yourDomain.com"
	SET "Sender=YourName@yourDomain.com"
	SET "Subject=Nothing"
	SET "Body=Nothing"

	::- Loops through the list of Tasks calling the Main Routine "Start-Task-Check" For Each.
	FOR %%L IN (%TaskList%) DO CALL :Start-Task-Check "%%~L"

	::- Skip the Subroutines and heads to the lable near the end of the script.
	GOTO End-Script

	::- Subroutines Go Here.
	GOTO :EOF
	:Start-Subroutines
		GOTO End-Task-Check
		:Start-Task-Check
			::For Debugging Set Debug=ON
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO ---&ECHO.&ECHO "%~1"
			
			::- Here we loop through the output of SCHTasks For the task, and send it for comparison.
			FOR /F "tokens=2,4,6 delims=, skip=1" %%a IN ('SCHTasks /Query /FO CSV /v /TN "%~1" ^| FIND /I "%StatusToMatch%" ') DO CALL :Start-Compare-Time "%~1" "%%~c"&IF /I "%DEBUG%"=="ON" ECHO.&ECHO Back at Task-Check-Loop, The Task: "%%~na" Has status: "%%~b" and last started at: "%%~c"
			IF /I "%DEBUG%"=="ON" ECHO. &ECHO Finished Checking Task "%~1"
			GOTO :EOF
		:End-Task-Check
		GOTO End-Compare-Time
		:Start-Compare-Time
			SET "RawTime=%~2"
			IF /I "%DEBUG%"=="ON" ECHO "%~2"
			
			::- Cuts up the Task Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/: " %%A IN ("%RawTime%") DO SET "TskYear=%%C"&SET "TskMMDD=%%A%%B"&SET "TskHH=%%D"&SET "TskTime=0%%E%%F%%G"&SET "TskMM=1%%E"&SET "TskSS=1%%F"&IF /I "%%G"=="PM" SET "TskTime=%%E%%F%%G"&SET /A "TskHH=12+(%%D)"
			IF "%TskMMDD:~3,1%"=="" (SET "TskDate=%TskYear%0%TskMMDD%") ELSE (SET "TskDate=%TskYear%%TskMMDD%")
			
			::- Cuts up the Current Time into manageble peices.
			FOR /F "Tokens=1-7 Delims=/:. " %%A IN ("%DATE% %TIME: =0%") DO SET "NowDate=%%D%%B%%C"&SET "NowTime=%%E%%F%%G"&SET "NowMM=1%%F"&SET "NowSS=1%%G"&SET /A "NowHH=1%%E-100"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO TskDate="%TskDate%" TskHH="%TskHH%" TskMM="%TskMM%" TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO NowDate="%NowDate%" NowHH="%NowHH%" NowMM="%NowMM%" NowSS="%NowSS%"&ECHO.
			
			::- Finds the Fifference from the Time started to the current Time.
			FOR %%l IN ("HH","MM","SS") DO CALL SET /A "Dif%%~l=(60+(%%Now%%~l%%)-(%%Tsk%%~l%%))%%%%60"
			IF /I "%DEBUG%"=="ON" ECHO ECHO DifHH="%DifHH%"&CALL ECHO SET /A "DifHH=(%DifHH%)%%%%24"
			CALL SET /A "DifHH=(%DifHH%)%%%%24"
			
			::- Debug Lines to check Variables
			IF /I "%DEBUG%"=="ON" ECHO.&ECHO.&ECHO TskHH="%TskHH%" TskMM="%TskMM%" TskSS="%TskSS%"
			IF /I "%DEBUG%"=="ON" ECHO NowHH="%NowHH%"	NowMM="%NowMM%"	NowSS="%NowSS%"
			IF /I "%DEBUG%"=="ON" ECHO -----------	-----------	-----------
			IF /I "%DEBUG%"=="ON" ECHO DifHH="%DifHH%"	DifMM="%DifMM%"	DifSS="%DifSS%"&ECHO.
			
			::- Checks if the Time the task was running is too large, and Calls the Function to Send the email:
			IF /I %DifMM% GEQ %MaxMinuts% ( CALL :Start-Mail "%~1" ) ELSE ( IF /I %DifHH% Gtr 0 ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%TskDate%" NEQ "%NowDate%" ( CALL :Start-Mail "%~1" ) ELSE ( IF /I "%DEBUG%"=="ON" ECHO The Task has Run Less than 30 Minutes.) ) )
			GOTO :EOF
		:End-Compare-Time
		GOTO End-Mail
		:Start-Mail
			IF /I "%DEBUG%"=="ON" ECHO Begining Mailing via Blat!
			SET "Subject=ATTENTION: Task %~1 Running At Least %MaxMinuts% Minutes!"
			SET "Body=The Task %~1 Has Been Running for at least %DifHH% Hours %DifMM% Minutes and %DifSS% Seconds.  The Task was Started on %TskDate% at %TskTime%.  This Alert was Generated on %NowDate% at %NowTime%"&ECHO.
			IF /I "%DEBUG%"=="ON" ECHO "%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
			"%BlatCmd%" - -body "%Body%" -subject "%Subject%" -to %MailTo% -Mailfrom %Sender% -Server %MailServer%
		GOTO :EOF
		:End-Mail
	:End-Subroutines
:End-Script
IF /I "%DEBUG%"=="ON" ECHO The Script Has Completed!
IF /I "%DEBUG%"=="ON" PAUSE
GOTO :EOF

Open in new window

0
 
LVL 12
ID: 33706251
Sorry the above was posted to the wrong thread! XD
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question