Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

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
0
paulpp
Asked:
paulpp
1 Solution
 
ahmadpjCommented:
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
 
Luis PérezSoftware Architect in .NetCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
paulppAuthor Commented:
Hello QCubed,

100% perfect. Thanks so much !!!


--Paul
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Awesome!  Glad I could help!  :)
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Sorry the above was posted to the wrong thread! XD
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now