In Jenkins, I have few Commands that I am trying to implement in the BAT File. So I have 2 BAT Files (RUN.BAT & CHANGE.BAT). In RUN.BAT, Commands are as follows:
CD\
E:
CD E:\RESULTS\1
mkdir 1
xcopy E:\I1\0 E:\RESULTS\1
Above Given Command would Go to the particular folder and Make a folder named as "1" & then the XCOPY line would copy the contents from folder "O" to folder "1".
Now lets come to CHANGE.BAT here the commands are:
for /f "tokens=1-7 delims=-:. " %%a in ("%date% %time%") do ren E:\RESULTS\1 %%a%%b%%c_%%d%%e%%f%%g
This command would change the folder name to the Date & Time of the current System.
Here is the issue, the above-given bat files runs completely fine when I call them on my CMD. But the same doesn't work on the JENKINS & throws me "The syntax of the command is incorrect".
What I am not able to understand is, .bat file works in CMD but the same doesn't work in Jenkins.
Kindly guide me.
The two batch files RUN.BAT
and CHANGE.BAT
can be replaced by a single batch file with just one command line:
@for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do @%SystemRoot%\System32\robocopy.exe "E:\I1\0" "E:\RESULTS\%%I-%%J-%%K_%%L%%M%%N" /E /R:3 /W:2 /NDL /NFL /NJH /NJS & exit /B
ROBOCOPY is the replacement for XCOPY and is available by default since Windows Vista and Windows Server 2003.
There is first executed ROBOCOPY with an invalid source directory path to get output an error message by ROBOCOPY which contains current date and time in a region independent format. Region independent format means that the format of date and time does not depend on which country is configured for the used account which is for Jenkins running as service the built-in local system account. That is most likely the reason for the error message output by command REN. The new folder name is different on execution of the batch file with settings for system account in comparison to execution of the batch file with your user account.
Then ROBOCOPY is run once again with correct source directory path and the destination directory path with yyyy-MM-dd_hhmmss
as directory name in E:\RESULTS
.
ROBOCOPY creates automatically like XCOPY the entire directory tree to destination directory if the destination directory does not already exist.
The command exit /B
is required to end the batch file execution after copying the files because of ROBOCOPY outputs a second line on error which would be otherwise processed also by FOR. It is possible to replace exit /B
by goto Label
and the next line is :Label
if there are more lines to execute like:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1-6 delims=/: " %%I in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do %SystemRoot%\System32\robocopy.exe "E:\I1\0" "E:\RESULTS\%%I-%%J-%%K_%%L%%M%%N" /E /R:3 /W:2 /NDL /NFL /NJH /NJS & goto FilesCopied
:FilesCopied
rem More command lines to execute.
endlocal
For completeness a solution working also on Windows XP and all later Windows versions:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "CurrentDateTime=%%I"
set "CurrentDateTime=%CurrentDateTime:~0,4%-%CurrentDateTime:~4,2%-%CurrentDateTime:~6,2%_%CurrentDateTime:~8,6%"
%SystemRoot%\System32\xcopy.exe "E:\I1\0" "E:\RESULTS\%CurrentDateTime%\" /I /R /Q /Y >nul
endlocal
For a full explanation what is output by ROBOCOPY on running it first with an invalid source directory path and how FOR processes this output see Time is set incorrectly after midnight. The Windows XP solution with WMIC is explained in detail in the same answer.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
robocopy /?
set /?
setlocal /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?
xcopy /?