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
803 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
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 11

Accepted Solution

by:
Ben Personick 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Author Comment

by:paulpp
ID: 33694869
Hello QCubed,

100% perfect. Thanks so much !!!


--Paul
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33696160
Awesome!  Glad I could help!  :)
0
 
LVL 11

Expert Comment

by:Ben Personick
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 11

Expert Comment

by:Ben Personick
ID: 33706251
Sorry the above was posted to the wrong thread! XD
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now