How to Open Current MOCA Server Log file via batch script in Windows

In my opinion, if you have to do a task daily or multiple times a day, you might as well write a script for it — It is not being lazy, it is being efficient. This way you can focus your energy on what’s important. Even if it is a task as simple as opening the latest standard output log file for MOCA Server hosted on a Windows Server.

This script will help you understand how you can use %LESDIR% or %MOCA_ENVNAME% environment variables to your benefit regardless of the operating system you’re running MOCA Server on. There is always a rptab file that you can utilize to automate and validate processes, be it removing obsolete files or taking backups. Going through this route will ensure that the instance defined is matched against the rptab file and moving a batch or shell script from one instance to another will continue to work and not cause an undesired effect. To read more about the rptab file, please see my blog:

RedPrairie/JDA WMS — rptab file

@ECHO off
SETLOCAL
CLS
:: Initial Variables
:: Set Start time
SET startdate=%date:~10,4%-%date:~4,2%-%date:~7,2%
:: End
SET /A ERRORLEVEL=0
:: End Variables
ECHO.
ECHO Date is %startdate%
:: Enter BY Instance Name
ECHO.
SET /p ENVNAME= Please enter MOCA Instance name that you want to open stdout logfile for:
IF DEFINED ENVNAME (
ECHO MOCA Instance Name: %ENVNAME%
) ELSE (
GOTO :invalidselection
)
::Validate if instance exists in rptab
ECHO.
ECHO Validate if instance exists in rptab file
FOR /f "usebackq tokens=1-3 delims=;" %%a IN ("c:\ProgramData\RedPrairie\Server\rptab") DO (
IF /I %ENVNAME% == %%a (

SET MOCA_ENVNAME=%%a
SET LESDIR=%%b
SET MOCA_REGISTRY=%%c
)
)
ECHO.
ECHO Reading RPTAB File:
ECHO Instance Name: %MOCA_ENVNAME%
ECHO LES Directory: %LESDIR%
ECHO Registry File: %MOCA_REGISTRY%
IF "%MOCA_ENVNAME%"=="" GOTO :notinrptab
:: Validate Successful
ECHO.
ECHO Found instance in RPTAB File. Proceeding...
:: Check if %LESDIR%\log Directory Exists
ECHO.
IF EXIST %LESDIR%\log (
ECHO Directory %LESDIR%\log exists. Proceeding...
) ELSE (
GOTO :leslogmissing
)
IF EXIST C:\Progra~2\Notepad++\notepad++.exe (
SET uc_text_exe=C:\Progra~2\Notepad++\notepad++.exe
) ELSE (
SET uc_text_exe ="notepad"
)
::Get Full Path for Standard Output Logfile
ECHO.
FOR /f "tokens=*" %%a IN ('dir %LESDIR%\log\moca.%MOCA_ENVNAME%-stdout.%startdate%.log /b /s') DO SET ucstdoutlogfile=%%a
IF DEFINED ucstdoutlogfile (
ECHO Path for Today's Standard Output Logfile is: %ucstdoutlogfile%
%uc_text_exe% %ucstdoutlogfile%
GOTO :endofscript
)
ECHO.
ECHO INFO: MOCA Instance was not restarted today. Finding latest log file:
FOR /f "tokens=*" %%a IN ('dir %LESDIR%\log\moca.%MOCA_ENVNAME%-stdout.*.log /b /s /o-d') DO SET ucstdoutlogfile=%%a && goto :openstdfile
:openstdfile
IF DEFINED ucstdoutlogfile (
ECHO Path for Latest Standard Output Logfile is: %ucstdoutlogfile%
%uc_text_exe% %ucstdoutlogfile%
) ELSE (
msg %USERNAME% /time:3 ERROR: Logfile not found!
)
ECHO End of script...
GOTO :endofscript
::ERROR MESSAGES:invalidselection
ECHO.
ECHO.
msg %USERNAME% /time:3 ERROR: Not a valid argument.
SET /A ERRORLEVEL=1
GOTO :endofscript
:leslogmissing
ECHO.
ECHO.
msg %USERNAME% /time:3 ERROR: Directory %LESDIR%\log not found.
SET /A ERRORLEVEL=1
GOTO :endofscript
:stdoutfilemissing
ECHO.
ECHO.
msg %USERNAME% /time:3 ERROR: Standard Output file missing...
SET /A ERRORLEVEL=1
GOTO :endofscript
:notinrptab
ECHO.
ECHO.
msg %USERNAME% /time:3 ERROR: No records found in RPTAB file for Environment.
SET /A ERRORLEVEL=1
goto :endofscript
:endofscript
ECHO Exiting...
ECHO ERRORLEVEL is: %ERRORLEVEL%
ENDLOCAL
EXIT /b %ERRORLEVEL%

Sample Screenshots:

This script will even work if MOCA Server has been running for a few days:

I hope this blog helps you understand how to utilize rptab file and reduce manually redundant task(s) by automating tasks through scripts that do not require any intervention. Writing batch or bash scripts will allow you to reduce the chances of error when it comes to more complex tasks. I am hoping this new year brings you and the processes that you do more towards automation.

If you have any questions, please do not hesitate to reach out to me.

Below is the batch script. You are free to experiment with it if you want to try it for personal use:

--

--

--

I am an experienced IT Consultant who has a knack to solve complex problems. Want to connect? Reach out to me on: http://www.linkedin.com/in/ali-ahmed-jdawms

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is Hadoop Distributed File System (HDFS)?

The Secret of Z-index

Set up a Bitcoin-Core Full Node with a custom data directory

ITAM Solution: 4 Tips For Choosing The Right Pick For Your Business Needs

ITAM Solution: 4 Tips For Choosing The Right Pick For Your Business Needs

Flexbox Gotchas Reloaded

We just launched Loot.game

Azure — Windows Update Management for on-premises VMware VMs with snapshots

AWS Timestream, ECS & Prometheus Metrics

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Ahmed

Ali Ahmed

I am an experienced IT Consultant who has a knack to solve complex problems. Want to connect? Reach out to me on: http://www.linkedin.com/in/ali-ahmed-jdawms

More from Medium

How to perform subdomain based routing using AWS with an application load balancer and Elastic…

4 Ways to Install Firefox 98.0 on Ubuntu / Linux Mint / AlmaLinux & Fedora

Install Firefox 98.0 on ubuntu

Installing odoo v14/v15 on Ubuntu 20.04 VMWare

How to Install and configure free SSL/TLS certificate for Tomcat using Let’s Encrypt on Ubuntu.