ROBOCOPY walways returning code of "0"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

ROBOCOPY walways returning code of "0"

#1 Post by SIMMS7400 » 11 Nov 2017 06:33

Hi Folks -

I'm running into an issue where ROBOCOPY is always returning a "0".

Code: Select all

>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO Perform File Purge Maintenance                                       
ECHO ********************************************************
ECHO.
)

ECHO Perform File Purge Maintenance
PING -n %DELAY% 127.0.0.1>nul

::-- Paths are added to below function call - age is set in function --::

CALL :PURGE ""%INTRAPATH%","%ERRORINTRAPATH%","%InboxPath%\Archive\""

Code: Select all

:PURGE
SETLOCAL ENABLEDELAYEDEXPANSION

SET "SOURCE=%~1"
SET "TEMP=%TEMP%\~%random%"
IF NOT EXIST "%TEMP%" MD "%TEMP%"

FOR %%D IN ( %SOURCE:,= % ) DO (
	SET "DIR=%%~D"
	ROBOCOPY "!DIR:~0,-1!" "%TEMP%" /E /MOVE /MINAGE:7
	echo %errorlevel%
)
Does anyone know why?

64 bit operating system
5.1.1027 is ROBO version

Thanks!

Edit : 11/11/2016 7:52am EST

If I just make it a simple ROBOCOPY without the forloop and function, it works fine and I get the desired return codes. Is there something in the function/for loop causing this behavior then? I can't seem to see it.
Last edited by SIMMS7400 on 11 Nov 2017 07:11, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: ROBOCOPY walways returning code of "0"

#2 Post by ShadowThief » 11 Nov 2017 07:01

How are you checking what robocopy is returning?

Also, without testing your code, I'm going to preemptively blame the setlocal enabledelayedexpansion at the top of :PURGE for creating a local scope inside of the function.

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

Re: ROBOCOPY walways returning code of "0"

#3 Post by SIMMS7400 » 11 Nov 2017 07:15

ShadowThief wrote:How are you checking what robocopy is returning?

Also, without testing your code, I'm going to preemptively blame the setlocal enabledelayedexpansion at the top of :PURGE for creating a local scope inside of the function.
Hi Shadow -

I'm just checking with echo %errorlevel% - I added it above.

Also, the SETLOCAL ENABLEDELAYEDEXPANSION is actually at the top of my script, however I place it here int he purge function for simplicity.

Even If I remove the SETLOCAL ENABLEDELAYEDEXPANSION it still doesn't work.

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

Re: ROBOCOPY walways returning code of "0"

#4 Post by SIMMS7400 » 11 Nov 2017 07:23

Here is my full scripts:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Script Name: Load_EBS_Actuals.bat                                       --::
::                                                                           --::
::-- Description: Load EBS Actuals                                           --::
::--																		 --::
::  																		 --::
::--  Calls:      env.cmd                                                    --::
::--  Called By:  Not Applicable                                             --::
::               														   	 --::
::-- Parameters:  Call _env.cmd to get variable to determine:                --::
::               														   	 --::
::--              1. Path variables                                          --::
::--              2. User ID(s)                                              --::
::--              3. Logon(s)                                                --::
::--              4. Password(s)									         --::
::--              5. Server(s)								         	     --::
::--              6. Application(s)	    						             --::
::--			  7. Database(s)   		    							     --::
::--			  8. etc													 --::
::  																		 --::
::-- Author:	  							     --::
::-- Date:	                                 	         		     --::
::  																		 --::
::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::-- Set Working Directory as Script Path --::

CD /D "%~dp0"

::-- Call Environment File --::

CALL C:\Automation\Config\env.bat 

::-- Set Process Output Variables --::

SET "PLOGPATH=Load_EBS_Actuals\Load_EBS_Actuals_Logs\"
SET "PERRORPATH=Load_EBS_Actuals\Load_EBS_Actuals_Errors\"

::-- Establish INTRAPATH variables & directories --::

SET "INTRAPATH=%LogPath%\%PLOGPATH%"
SET "ERRORINTRAPATH=%LogPath%\%PERRORPATH%"

FOR %%F IN ( "%INTRAPATH%" "%ERRORINTRAPATH%" ) DO IF NOT EXIST "%%~F" MKDIR "%%~F"

::-- Parse DOS TIME variable in saveable format --::

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 "DATETIME=%date:~-4,4%%date:~-10,2%%date:~-7,2%_%TIMESTAMP%"

::-- Establish Log & Error files --::

SET "LOGFILE=%INTRAPATH%%DATETIME%_%~n0.log"
SET "ERRORFILE=%ERRORINTRAPATH%%DATETIME%_%~n0.err"

::-- Begin Script Processing --::

SET "DELAY=5"

>>%LOGFILE% (
ECHO ********************************************************
ECHO %~n0 Starting at %TIME%                		   	    
ECHO ********************************************************
ECHO.
ECHO ********************************************************
ECHO Login to PBCS                                          
ECHO ********************************************************
ECHO.
ECHO Action   : Login
ECHO Login    : %LOGINID%
ECHO Password : %PASSWORD%
ECHO PBCS URL : %URL%
ECHO Domain   : %DOMAIN%
ECHO --------------------------------------------------------
ECHO.
)

ECHO Login to PBCS...
PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate login %LOGINID% %PASSWORD% %URL% %DOMAIN% >nul 2>&1 && (

	ECHO PBCS Login^: Successful >>%LOGFILE% 
	) || (
	ECHO PBCS Login^: Failed >>%LOGFILE%
    GOTO :EMAIL
)

CLS
>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO Perform Variable Assignment for EBS Load Process                        
ECHO ********************************************************
ECHO.
)

:RUN_LOAD
CALL :FILEPROCESS

CLS
>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO Perform EBS Data Load for %YEARPOV%%MONTHPOV%                        
ECHO ********************************************************
ECHO.
ECHO Data File : %datafilename%
ECHO Month POV : %MONTHPOV%
ECHO Year POV  : %YEARPOV%
ECHO --------------------------------------------------------
ECHO.
)

::----------------------------START EBS DATA LOAD PROCESS-----------------------------::
::---------------------------------------STEP 1---------------------------------------::
ECHO Step %CNT% of %TCNT%^: Delete EBS File %datafilename% from Inbox >>%LOGFILE%
ECHO Step %CNT% of %TCNT%^: Delete EBS File %datafilename% from Inbox 

PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate deletefile inbox/EBS/%datafilename% >nul 2>&1 && (

	ECHO Step %CNT% of %TCNT%^: Successful >>%LOGFILE% 
	SET /A "CNT+=1"
	) || (
	ECHO Step %CNT% of %TCNT%^: Failed >>%LOGFILE%
    GOTO :EMAIL
)
CLS

::---------------------------------------STEP 2---------------------------------------::
ECHO Step %CNT% of %TCNT%^: Upload EBS File %datafilename% to Inbox >>%LOGFILE%
ECHO Step %CNT% of %TCNT%^: Upload EBS File %datafilename% to Inbox 

PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate uploadfile %InboxPath%/%datafilename% inbox/EBS >nul 2>&1 && (

	ECHO Step %CNT% of %TCNT%^: Successful >>%LOGFILE% 
	SET /A "CNT+=1"
	) || (
	ECHO Step %CNT% of %TCNT%^: Failed >>%LOGFILE%
    GOTO :EMAIL
)
CLS

::---------------------------------------STEP 3---------------------------------------::
ECHO Step %CNT% of %TCNT%^: Set Substituion Variables for Business Rules ^|^
FDMEEMonth=%MONTHPOV% FDMEEYr=FY%YEARPOV% >>%LOGFILE%

ECHO Step %CNT% of %TCNT%^: Set Substituion Variables for Business Rules:
ECHO.
ECHO FDMEEMonth=%MONTHPOV%
ECHO FDMEEYr=FY%YEARPOV%

PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate setsubstvars All FDMEEMonth=%MonthPOV% FDMEEYr=FY%YearPOV% >nul 2>&1 && (

	ECHO Step %CNT% of %TCNT%^: Successful >>%LOGFILE% 
	SET /A "CNT+=1"
	) || (
	ECHO Step %CNT% of %TCNT%^: Failed >>%LOGFILE%
    GOTO :EMAIL
)
CLS

::---------------------------------------STEP 4---------------------------------------::
ECHO Step %CNT% of %TCNT%^: Run Clear Business Rule - Act: Before EBS Clear Act - KiteFIN >>%LOGFILE%
ECHO Step %CNT% of %TCNT%^: Run Clear Business Rule - Act: Before EBS Clear Act - KiteFIN

PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate runbusinessrule "Act: Before EBS Clear Act - KiteFIN" >nul 2>&1 && (

	ECHO Step %CNT% of %TCNT%^: Successful >>%LOGFILE% 
	SET /A "CNT+=1"
	) || (
	ECHO Step %CNT% of %TCNT%^: Failed >>%LOGFILE%
    GOTO :EMAIL
)
CLS

::---------------------------------------STEP 5---------------------------------------::
ECHO Step %CNT% of %TCNT%^: Load EBS File %datafilename% into Planning :^
%MONTHPOV%-%YEARPOV% >>%LOGFILE%

ECHO Step %CNT% of %TCNT%^: Load EBS File %datafilename% into Planning
ECHO.
ECHO Load Combo : %MONTHPOV%-%YEARPOV%

PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate rundatarule LR_EBS_Actual %MonthPOV%-%YearPOV% %MonthPOV%-%YearPOV% REPLACE STORE_DATA inbox/EBS/%datafilename% >nul 2>&1 && (

	ECHO Step %CNT% of %TCNT%^: Successful >>%LOGFILE% 
	SET /A "CNT+=1"
	) || (
	ECHO Step %CNT% of %TCNT%^: Failed >>%LOGFILE%
    GOTO :EMAIL
)
CLS

::---------------------------------------STEP 6---------------------------------------::
ECHO Step %CNT% of %TCNT%^: Run Aggregation Business Rule - Act: After EBS Agg Final - KiteFIN >>%LOGFILE%

ECHO Step %CNT% of %TCNT%^: Run Aggregation Business Rule:
ECHO.
ECHO Business Rule ^- Act: After EBS Agg Final - KiteFIN

PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate runbusinessrule "Act: After EBS Agg Final - KiteFIN" >nul 2>&1 && (

	ECHO Step %CNT% of %TCNT%^: Successful >>%LOGFILE% 
	SET /A "CNT+=1"
	) || (
	ECHO Step %CNT% of %TCNT%^: Failed >>%LOGFILE%
    GOTO :EMAIL
)
CLS

::---------------------------------------STEP 7---------------------------------------::
ECHO Step %CNT% of %TCNT%^: Archive EBS File - %datafilename% >>%LOGFILE%
ECHO Step %CNT% of %TCNT%^: Archive EBS File - %datafilename%

PING -n %DELAY% 127.0.0.1>nul

IF EXIST "%InboxPath%\%datafilename%" MOVE /Y "%InboxPath%\%datafilename%" "%InboxPath%\Archive\%datafilename%" >nul 2>&1 && (

	ECHO Step %CNT% of %TCNT%^: Successful >>%LOGFILE% 
	SET /A "CNT+=1"
	) || (
	ECHO Step %CNT% of %TCNT%^: Failed >>%LOGFILE%
    GOTO :EMAIL
)
CLS

::-----------------------------END EBS DATA LOAD PROCESS------------------------------::

GOTO RUN_LOAD 

:END
CLS
>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO Logout of PBCS                                          
ECHO ********************************************************
ECHO.
ECHO Action   : Logout
ECHO --------------------------------------------------------
ECHO.
)

ECHO Logout of PBCS...
PING -n %DELAY% 127.0.0.1>nul

ECHO CALL epmautomate logout >nul 2>&1 && (

	ECHO PBCS Logout^: Successful >>%LOGFILE% 
	) || (
	ECHO PBCS Logout^: Failed >>%LOGFILE%
    GOTO :EMAIL
)

>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO Perform File Purge Maintenance                                       
ECHO ********************************************************
ECHO.
)

ECHO Perform File Purge Maintenance
PING -n %DELAY% 127.0.0.1>nul

::-- Paths are added to below function call - age is set in function --::

CALL :PURGE ""%INTRAPATH%","%ERRORINTRAPATH%","%InboxPath%\Archive\""

:NormalExit
>>%LOGFILE% (
ECHO.
ECHO ********************************************************
ECHO %~n0 - Completed Successfully                           
ECHO ********************************************************

ECHO ********************************************************
ECHO Normal Exit - %~nx0                                    
ECHO ********************************************************
DATE /t                                                     
TIME /t                                                     
)

CALL :ARCHIVE
EXIT /B 0

:AbnormalExit
FOR %%L IN ( "%LOGFILE%" "%ERRORFILE%" ) DO (
	>>%%L (
	ECHO.
	ECHO ********************************************************
	ECHO %~n0 results in Abnormal Exit                           
	ECHO ********************************************************
	ECHO Please check log file for errors
	ECHO.
	ECHO ********************************************************
	ECHO Abnormal Exit - %~nx0                                   
	ECHO ********************************************************
	DATE /t                                                      
	TIME /t                                                      
	)
)

CALL :ARCHIVE
EXIT /B 1

:::::::::::::::::::::::::::::::::::::::::::::::::
::-- F U N C T I O N S   B E L O W   H E R E --::
:::::::::::::::::::::::::::::::::::::::::::::::::

:ARCHIVE

::-- Archive Log Files in \YYYYDDMM Folder --::
        
FOR %%F IN ("%INTRAPATH%*") DO (
	IF NOT EXIST "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
		MKDIR "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
	)
IF EXIST "%%~F" MOVE "%%~F" "%INTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" >nul 2>&1
)

::-- Archive Error Files in \YYYYDDMM Folder --::
::-- RD then used to remove empty error subdirectories --::

FOR %%F IN ("%ERRORINTRAPATH%*") DO (
	IF NOT EXIST "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%" (
		MKDIR "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
	)
IF EXIST "%%~F" MOVE "%%~F" "%ERRORINTRAPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%">nul 2>&1
)
RD "%ERRORINTRAPATH:~0,-1%" >nul 2>&1
GOTO :EOF

:EMAIL
SET "SW_SUB=Warning : %~nx0 failed to complete successfully"
SET "SW_BODY=%~nx0 failed to complete - please check the attached log file for details."
SET "SW_TO_EMAIL=%SW_LOADEBSACT_DL%"
SET "SW_ATTACH=%LOGFILE%"

CALL "%SW_MAIL_EXE%" /s /from "%SW_FROM_EMAIL%" /pass "%SW_PW%" /server "%SW_SERVER%"^
	/p "%SW_PORT%" /SSL /to "%SW_TO_EMAIL%" /sub "%SW_SUB%" /b "%SW_BODY%" /a "%SW_ATTACH%

GOTO AbnormalExit

:FILEPROCESS
SET /A "CNT=0"
SET /A "CNT+=1"
SET "TCNT=7"

::-- Determine if Load Load Files exist for processing --::

SET "PATTERN=KITE_GL_Detail_PBCS_*"
SET "EXIST="
FOR %%A IN ( %InboxPath%\%PATTERN% ) DO SET "EXIST=1"

IF DEFINED EXIST GOTO HASFILES

::-- No Data Load Files Detected --::

IF "%CNT%"=="1" IF ["%EXIST%"] == [""] (
	>>%LOGFILE% (
	ECHO WARNING: NO EBS DATA FILE AVAILABLE FOR PROCESSING
	ECHO.
	ECHO Please reach out to client contact for assistance
	)
)
PING -n %DELAY% 127.0.0.1>nul
GOTO END

:HASFILES
::-- Set Data Load File as variable for processing --::
::-- Data Load File will be parsed to extract Month and Year POV --::

PUSHD "%InboxPath%"
FOR %%F IN ( "%PATTERN%" ) DO SET "datafilename=%%~F"
	FOR /F "tokens=6 delims=_." %%A IN ("!datafilename!") DO (
		SET "STRING=%%A"
		SET "MONTHPOV=!STRING:~3,4!"
		SET "YEARPOV=!STRING:~0,-4!"
)
POPD
GOTO :EOF


:PURGE

SET "SOURCE=%~1"
SET "TEMP=%TEMP%\~%random%"
IF NOT EXIST "%TEMP%" MD "%TEMP%"

FOR %%D IN ( %SOURCE:,= % ) DO (

	ECHO "%%~D" | FINDSTR /C:"Archive" >nul 2>&1 && ( SET "PROC=Archives"& SET "AGE=7" )
	ECHO "%%~D" | FINDSTR /C:"Errors"  >nul 2>&1 && ( SET "PROC=Errors"& SET "AGE=7" )
	ECHO "%%~D" | FINDSTR /C:"Logs"    >nul 2>&1 && ( SET "PROC=Logs"& SET "AGE=7" )
		
	SET "DIR=%%~D"
	ROBOCOPY "!DIR:~0,-1!" "%TEMP%" /E /MOVE /MINAGE:!AGE!

		IF "%ERRORLEVEL%"=="0" (
			ECHO All !PROC! ^< !AGE! days old ^- no purging required >>%LOGFILE%
		) ELSE IF "%ERRORLEVEL%"=="1" (
			ECHO Purge !PROC! : Successful >>%LOGFILE%
		) 
)
RD /S/Q "!TEMP!"
GOTO :EOF

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: ROBOCOPY walways returning code of "0"

#5 Post by ShadowThief » 11 Nov 2017 07:34

SIMMS7400 wrote:I'm just checking with echo %errorlevel% - I added it above.
Does using !errorlevel! instead work correctly?

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

Re: ROBOCOPY walways returning code of "0"

#6 Post by SIMMS7400 » 11 Nov 2017 07:40

LOL - wow!

It works like a charm - never even thought to try that since errorlevel is natively set.

Thank you, Shadow - sorry for the bother. Have a great weekend!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: ROBOCOPY walways returning code of "0"

#7 Post by Squashman » 12 Nov 2017 16:08

SIMMS7400 wrote:LOL - wow!

It works like a charm - never even thought to try that since errorlevel is natively set.

Thank you, Shadow - sorry for the bother. Have a great weekend!
You are confusing ERRORLEVEL with %ERRORLEVEL%.

Inside a code block without delayed expansion enabled, ERRORLEVEL will work but !ERRORLEVEL! and %ERRORLEVEL% will not.
With delayed expansion enabled, then ERRORLEVEL and !ERRORLEVEL! work.

Code: Select all

@echo off
REM setlocal enabledelayedexpansion

for /L %%g in (1,1,1) do (
	choice /C 123 /M "Chose 1 2 or 3"
	if errorlevel 3 echo you chose 3
	if "%errorlevel%"=="3" echo you chose 3
	if "!errorlevel!"=="3" echo you chose 3
)
pause

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

Re: ROBOCOPY walways returning code of "0"

#8 Post by SIMMS7400 » 14 Nov 2017 02:28

Hi Squash -

Thank you for the follow up on this - Noted!!

Post Reply