Silly question - why would my script not be writing the last portion to a log file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Silly question - why would my script not be writing the last portion to a log file?

#1 Post by SIMMS7400 » 18 Oct 2016 10:42

Hi Folks -

Something is stumping me and I believe it's rather simple.

I have a batch script that executes two child scripts. Each child script are existed with EXIT / B 0 or EXIST /B 1 depending return codes of the operation they perform.

The children scripts are as follows:

Code: Select all

CALL PEMCOA7_FCSTSEQ_EMP.cmd PEMCOA7 Employee PEMFIN_FCSTSEQ_EMP.mxl
CALL PEMCOA7_FCSTSEQ_ADMIN.cmd PEMCOA7 Admin PEMFIN_FCSTSEQ_ADMIN.mxl


Then, then process returns to the parent script where it performs a TYPE invoking a function (CALL :MERGE_MAXL)

Everything up to here works perfectly fine.

However, after the function completes, the script does not spool the remaining redirections to the log file.

Here is my script:

Code: Select all

@ECHO OFF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Script Name: PEMFIN_FCSTSEQ.cmd
::--
::-- Description: Used for the Generic Job Application
::                Executes PEMFIN_FCSTSEQ.cmd on the Essbase Server       
::                 
::--
::-- Parameters:  Call ENV.cmd to get environment variables to determine
::                login info, database, application, etc.
::--       
::-- Author:    
::-- Date:       
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::-- Set working directory as script path --::

cd /d %~dp0

::-- Set Environment File --::
CALL _env.cmd

::-- Set Path Variables --::

SET GJALOGPATH=%1
SET GJAERRORPATH=%2

SET intrapath=%MAINPATH%%LOGPATH%%GJALOGPATH%
SET errorintrapath=%MAINPATH%%ERRORPATH%%GJAERRORPATH%


FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%")   do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)

::-- Set Log and Error Files --::

SET logfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%~n0.log
SET errorfile=%errorintrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%~n0.log

::-- Prepare Log and Error File Directories --::

FOR %%f IN ( %MAINPATH%%LOGPATH%%GJALOGPATH%* )     DO ( DEL %%f )
FOR %%f IN ( %MAINPATH%%ERRORPATH%%GJAERRORPATH%* ) DO ( DEL %%f )

echo ********************************************************>>%logfile%
echo Begin %~n0 Process at %TIME%                        >>%logfile%
echo ********************************************************>>%logfile%

CALL PEMCOA7_FCSTSEQ_EMP.cmd PEMCOA7 Employee PEMFIN_FCSTSEQ_EMP.mxl
CALL PEMCOA7_FCSTSEQ_ADMIN.cmd PEMCOA7 Admin PEMFIN_FCSTSEQ_ADMIN.mxl

SET myError2=%errorlevel%
IF %myError2%==0 goto MAXLF

echo ********************************************************>>%logfile%
echo Error Encountered in Begin %~n0 Process               >>%logfile%
echo ********************************************************>>%logfile%

goto AbnormalExit

:MAXLF
echo ********************************************************>nul
echo Merge MaxL Standard Output into %~n0                    >nul
echo ********************************************************>nul

::-- Merge MaxL Outputs into parent script --::
   ::-- Ability to differentiate between PEMFIN and PEMCOA7 --::
   ::-- Merges process MaxL based on like characters --::

SET "WCS1=*PEMCOA7_FCSTSEQ_*_MaxL.log"
SET "WCS2=*PEMFIN_FCSTSEQ_*_MaxL.log"
SET "WCS3=PEMFIN_FCSTSEQ.log"

CALL :MERGE_MAXL %WCS1% %WCS2% %WCS3%

:NormalExit
echo ********************************************************>>%logfile%
echo %~n0 - Completed Successfully                           >>%logfile%
echo ********************************************************>>%logfile%

echo ********************************************************>>%logfile%
echo Normal Exit - %~nx0                                     >>%logfile%
echo ********************************************************>>%logfile%
date /t                                                      >>%logfile%
time /t                                           >>%logfile%

::-- Archive Log Files --::

FOR %%f IN (%MAINPATH%%LOGPATH%%GJALOGPATH%*) DO (
   IF NOT EXIST "%MAINPATH%%LOGPATH%%GJALOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
      MKDIR "%MAINPATH%%LOGPATH%%GJALOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )   
COPY /Y %%f "%MAINPATH%%LOGPATH%%GJALOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\%%~nf_%timestamp%%%~xf"
)

EXIT /B 0

:AbnormalExit
echo ********************************************************>>%errorfile%
echo  %~n0 results in Abnormal Exit                          >>%errorfile%
echo ********************************************************>>%errorfile%
echo Please Check the log file for errors                    >>%errorfile%

echo ********************************************************>>%errorfile%
echo Abnormal Exit - %~nx0                                   >>%errorfile%
echo ********************************************************>>%errorfile%
date /t                                                      >>%errorfile%
time /t                                                      >>%errorfile%

::-- Archive Error Files --::
 
FOR %%f IN (%MAINPATH%%ERRORPATH%%GJAERRORPATH%*) DO (
   IF NOT EXIST "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
      MKDIR "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )   
COPY /Y %%f "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\%%~nf_%timestamp%%%~xf"
)

EXIT /B 1

:::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::: Funtions Below Here ::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::

:MERGE_MAXL
   
PING -n 2 127.0.0.1>nul
IF EXIST "%MAINPATH%%LOGPATH%%GJALOGPATH%%1" (

   PUSHD %MAINPATH%%LOGPATH%%GJALOGPATH%
   FOR /R %%A in ( %1 ) DO (
   TYPE "%%A"
   )>>%date:~-4,4%%date:~-10,2%%date:~-7,2%_%3
   POPD
)
   PUSHD %MAINPATH%%LOGPATH%%GJALOGPATH%
   FOR /R %%A in ( %2 ) DO (
   TYPE "%%A"
   )>>%date:~-4,4%%date:~-10,2%%date:~-7,2%_%3
   POPD

PING -n 2 127.0.0.1>nul
GOTO :EOF



The portion of code that's not getting redirected to the log file is the portion following the Function:

Code: Select all

echo ********************************************************>>%logfile%
echo %~n0 - Completed Successfully                           >>%logfile%
echo ********************************************************>>%logfile%

echo ********************************************************>>%logfile%
echo Normal Exit - %~nx0                                     >>%logfile%
echo ********************************************************>>%logfile%
date /t                                                      >>%logfile%
time /t                                           >>%logfile%


Is there anything you see in my script that prevents that portion from being written to my log file?

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Silly question - why would my script not be writing the last portion to a log file?

#2 Post by pieh-ejdsch » 18 Oct 2016 14:10

Hi Simms,

mainpath and logpath isn't set.

1st you can reduce the varLength DateLog

Code: Select all

set "DateLog=%date:~-4,4%_%date:~-10,2%%date:~-7,2%"


2nd Use Echo on if you wantto see script at work and Log it.

Code: Select all

@echo off
:: ...

set debug=1
:: ...
CALL PEMCOA7_FCSTSEQ_ADMIN.cmd PEMCOA7 Admin PEMFIN_FCSTSEQ_ADMIN.mxl

if defined DEBUG Echo ON
 rem check if NO ERROR
  rem use CALL to log and see
IF not errorlevel 1 call MAXLF 3>>%errorfile%
Exit /b 0

call AbnormalExit 3>>%errorfile%
Exit /b 1

:MAXLF
>nul (
echo ********************************************************
echo Merge MaxL Standard Output into %~n0                   
echo ********************************************************
)

:: ...

:NormalExit
>&3 echo ********************************************************
>&3 echo %~n0 - Completed Successfully                           
>&3 echo ********************************************************

>&3 echo ********************************************************
>&3 echo Normal Exit - %~nx0                                     
>&3 echo ********************************************************
>&3 date /t                                                     
>&3 time /t                                           

::-- Archive Log Files --::

FOR %%f IN (%MAINPATH%%LOGPATH%%GJALOGPATH%*) DO (
   IF NOT EXIST "%MAINPATH%%LOGPATH%%GJALOGPATH%%dateLOG%" (
      MKDIR "%MAINPATH%%LOGPATH%%GJALOGPATH%%dateLOG%"
   )   
COPY /Y %%f "%MAINPATH%%LOGPATH%%GJALOGPATH%%dateLOG%\%%~nf_%timestamp%%%~xf"
)

EXIT /B 0

:AbnormalExit
>&3 echo ********************************************************
>&3 echo  %~n0 results in Abnormal Exit                         
>&3 echo ********************************************************
>&3 echo Please Check the log file for errors                   

>&3 echo ********************************************************
>&3 echo Abnormal Exit - %~nx0                                   
>&3 echo ********************************************************
>&3 date /t                                                     
>&3 time /t                                                     

::-- Archive Error Files --::
 
FOR %%f IN (%MAINPATH%%ERRORPATH%%GJAERRORPATH%*) DO (
   IF NOT EXIST "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%dateLOG%" (
      MKDIR "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%dateLOG%"
   )   
COPY /Y %%f "%MAINPATH%%ERRORPATH%%GJAERRORPATH%%dateLOG%\%%~nf_%timestamp%%%~xf"
)

EXIT /B 1



You also can see all the script in ECHO ON mode and Log the Messages into his own Files with Handle 3.
Normally you can use the Handle 2 to log all Errors into Errorfile and Log Handle 1 into Logfile.
But in Handle one this Message is not to see when this script run, in Echo ON to. eg it send to Handle 3 and Log third Handle.

Phil

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Silly question - why would my script not be writing the last portion to a log file?

#3 Post by SIMMS7400 » 18 Oct 2016 14:53

Hi Phil -

Thank you for your reply!

So, I set MAINPATH, LOGPATH and ERRORPATH in the _env.cmd called at the top of the script.

Post Reply