paulpp
asked on
Batch file (or VB) to check a directory, if the directory is not empty, it will run a command for blat.exe
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
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
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello QCubed,
100% perfect. Thanks so much !!!
--Paul
100% perfect. Thanks so much !!!
--Paul
Awesome! Glad I could help! :)
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. =)
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
Sorry the above was posted to the wrong thread! XD
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