********************************************
* Description: Windows batch scripting tips
* Date: 05:59 PM EST, 09/06/2017
********************************************
<1> Turn on/off script execution process within the command line:
|
|__ CMD> @echo off
|
|__ CMD> @echo on
<2> Comment comparison of "::" and "REM":
|
|__ REM: all the commented content will show up during script execution.
|
|__ "::": the commented content will not show up when script got executed from command line.
<3> Print a blank line on command line:
|
|__ Sometimes, command line output should be friendly readable by seperated by blank line.
|
|__ CMD> echo.
|
|__ CMD> echo[
|
|__ CMD> echo(
<4> Hidden the errors thrown out by command:
|
|__ CMD> forfiles /P %LOG_LOCATION% /M Critical_Log_PS_* /D -1 /C "cmd /c Del @FILE"
|
| CAUTION: when above command can not find the target files to delete, error will show up "System could not find sepacific files to delete".
|
|__ CMD> forfiles /P %LOG_LOCATION% /M Critical_Log_PS_* /D -1 /C "cmd /c Del @FILE" > nul 2> nul
<5> Condition "if" can not includes multiple commands, calling a seperated functin as work aroudn:
|
|__ CMD> if %var_count% LSS %~2
| (
| echo %filedatetime% > %LOG_LOCATION%\Critical_Email_PS_Status_%~1.txt
| echo.>> %LOG_LOCATION%\Critical_Email_PS_Status_%~1.txt
| echo Critical Alert for Process Scheduler - %~1 in critical state >> %LOG_LOCATION%\Critical_Email_PS_Status_%~1.txt
| %email_client% %LOG_LOCATION%\Critical_Email_PS_Status_%~1.txt -to %criticalEmail% -subject "CRITICAL:%server%:ManTech - %~1 Process Scheduler in Critical State"
| )
|
|
|__ CMD> if %var_process_count% LSS %~2 ( call :func_email_log %var_process_schedule_name% %var_process_count% )
<6> When sending email via msmtp, there should be a blank line between address section and main body section. CAUTION: no space in between "echo" and ">>":
|
|__ CMD> echo to: support@emeralit.com > email.txt
echo from: sqlmail@emeralit.com >> email.txt
echo subject: Test Email >> email.txt
echo.>> email.txt
type email_body.txt >> email.txt
msmtp -t < email.txt
<7> Sometimes, script functions normally on command line, but failed on task scheduler. One of the reasons is that the referred external script is not indicated with full path:
|
|__ CMD> for /f "delims=" %%a in ('sqlplus -s / as sysdba @file_archivelog_sequence.sql') do @set var_sequence=%%a
|
|__ CMD> for /f "delims=" %%a in ('sqlplus -s / as sysdba @%var_script_location%\file_archivelog_sequence.sql') do @set var_sequence=%%a
<7> In Windows OS, if the full path containing space, then doule quote are needed when issue from batch script:
|
|__ CMD> C:\"Program Files"\7-Zip\7z.exe a -m0=lzma2 -mmt6 -t7z -mx=9 -p%var_7zip_password% %var_dump_directory%\expdp.dmp.7z %var_dump_directory%\expdp.dmp
<8> When Windows batch scripts running a .SQL file, an "exit" is needed within the .SQL file. Otherwise, the process will be hung.
<9> Fetching second column from one batch command output:
|
|__ CMD> for /F "tokens=1,2,3,4,5" %%A in ('"tasklist | find "oraagent.exe""') DO ( taskkill /f /pid %%B > %var_logName% )
Your Comments