Capture return code of command executed by FORFILES
Posted: 24 Aug 2012 06:36
Hi,
I'm using FORFILES to loop over a directory and move the containing files to a subdirectory based on a part of the filename.
The FORFILES line is as follows:
The command that is executed by FORFILES is a subroutine in the same script so I restart the script with parameters. At the beginning of the script I check the parameters and CALL the label (:DoTheWork in this case).
The :DoTheWork routine checks whether it is a directory and if it's not, processes the file. It extracts the date part of the filename using the iFrom and iLen parameter, creates the subdirectory if needed and then moves the file. It returns with ENDLOCAL & (EXIT /B %ExitCode%).
After returning I check %ERRORLEVEL% in the line following FORFILES, but the returned errorlevel is from FORFILES and not from :DoTheWork.
When I use
it nicely echoes OK or NOK depending on the ExitCode I set in :DoTheWork, but when I use
and check for %ExitCode% in the next line, ExitCode is empty i.e. the variable does not exist.
So is there a way to retrieve the return code of the command that is executed by FORFILES?
Thanks.
-=Wim=-
I'm using FORFILES to loop over a directory and move the containing files to a subdirectory based on a part of the filename.
The FORFILES line is as follows:
Code: Select all
FORFILES /M %strFileFilter% /D %strDateFilter% /c "cmd /c call \"%~f0\" :DoTheWork @ISDIR \"@PATH\" %iFrom% %iLen%"
The command that is executed by FORFILES is a subroutine in the same script so I restart the script with parameters. At the beginning of the script I check the parameters and CALL the label (:DoTheWork in this case).
The :DoTheWork routine checks whether it is a directory and if it's not, processes the file. It extracts the date part of the filename using the iFrom and iLen parameter, creates the subdirectory if needed and then moves the file. It returns with ENDLOCAL & (EXIT /B %ExitCode%).
After returning I check %ERRORLEVEL% in the line following FORFILES, but the returned errorlevel is from FORFILES and not from :DoTheWork.
When I use
Code: Select all
FORFILES /M %strFileFilter% /D %strDateFilter% /c "cmd /c call \"%~f0\" :DoTheWork @ISDIR \"@PATH\" %iFrom% %iLen% && ECHO.OK || ECHO.NOK "
it nicely echoes OK or NOK depending on the ExitCode I set in :DoTheWork, but when I use
Code: Select all
FORFILES /M %strFileFilter% /D %strDateFilter% /c "cmd /c call \"%~f0\" :DoTheWork @ISDIR \"@PATH\" %iFrom% %iLen% && SET ExitCode=0 || SET ExitCode=1 "
and check for %ExitCode% in the next line, ExitCode is empty i.e. the variable does not exist.
So is there a way to retrieve the return code of the command that is executed by FORFILES?
Thanks.
-=Wim=-