Unable to execute a .bat file in post build event command line - batch-file

My post build command is
call "$(ProjectDir)MyFile.bat"
And getting build error:
Error 1 The command "call
C:\MyProject\MyFile.bat"
exited with code 1. C:\Program Files
(x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets 4548 5
What am I missing here? Working on TFS source code, is that the reason getting this error?
For testing in MyFile.bat the only code is mkdir MYTestFolder, but then I am also getting the same error.

It seems like the syntax of the call is correct. Therefore I believe it's failing inside the batch file. I suggest putting the first few lines of the batch to write the time into a log file, so you can confirm it's being called. Do this before any of the actual work, so it can be sure that the batch is being executed.
call "$(ProjectDir)MyFile.bat"
MyFile.bat
#echo off
echo time /t > MyTempFolderPath\logfile.txt

It is the code inside the batch file that is failing - what is it doing?
You have a few options:
a - I would use process monitor and watch process exits and see the status codes of whatever the batch file launches.
b - You could also try running the batch file from the same folder as msbuild and see if you get any errors
Ed

Related

Running Saxon from a batch file: how do I capture errors?

I've got a batch file that calls Saxon to do an XSLT transformation. For some files, Saxon gives me an error. This appears in a new command line window which is open for about 1 second, and then closes. I'm trying to capture that error message in a file.
This is the relevant part of the batch file:
start /wait "" "C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"file.xml" -xsl:fixerrors.xslt -o:"output.xml" 1>>fixerrors.log 2>&1
The fixerrors.log file is created, but remains empty even if Saxon encounters an error and creates the new command line window.
I was able to capture error messages from another program like this, so the idea of using 1>>fixerrors.log is not wrong in itself. This seems to be specific to Saxon.
Tried two approaches suggested by #Gerhard Barnard:
start /wait "" "C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"%~n1 - original.xml" -xsl:fixerrors.xslt -o:"%~n1.xml" 1>>fixerrors.log 2>&1 & type fixerrors.log
does not work, the Saxon errors are not placed in the log file.
"C:\Program Files\Saxonica\SaxonHE9.6N\bin\Transform.exe" -s:"%~n1 - original.xml" -xsl:fixerrors.xslt -o:"%~n1.xml" 1>>fixerrors.log 2>&1
does work, the error messages are placed in the log file (in fact, they're placed twice because I'm redirecting both standard error and standard output to the file).

Using call <file.bat> results in "sleep is not recognized as an internal or external command.."

I have a script that calls other commands in a for loop:
for %%x in (%CMDS::= %) do (
call C:\%%x %1%
echo "%%x complete"
)
However, running this results the console spitting out :
'sleep' is not recognized as an internal or external command,
operable program or batch file.
This is because the files i loop through and run have these commands in them. Why is it that if i run these files one by one they work, but when chained using call they don't? I can sleep in my terminal outside of this script..
Regards
Thanks to another answer, I solved this error by replacing sleep 5 in my .bat file with:
powershell -Command "& {sleep 5}"
Works fine now. Better still, also tested Stephan's suggestion:
timeout 5
Simpler, and shows a nice message like
Waiting for 0 seconds, press a key to continue ...
Note that some Windows versions require the /t option to define the time.
timeout /t 5
There is no sleep command in batch. That's why you are getting this error.
EDIT:
There is no sleep command in Windows CMD or Batch. BUT: as you can use the command in your console, I suppose there might be a script or a program called sleep. This script or program might be situated in your working directory or in some other directory included in your %PATH% variable. If this is the case, it's possible that your script gives you this error because of a path issue.
Say, you are in C:\SomeFolder and there is a sleep.exe in there. You are calling another script or command which changes the current directory to D:\AnotherFolder. Now another script or command tries to execute your mysterious sleep command assuming the working dir to be C:\SomeFolder but as you are in a different folder (D:\SnotherFolder) now, sleep can't be found. Further, when using call the variable scope of the calling script becomes also the scope for the called script. So it's also possible that variables are being overwritten by different scripts. Such a variable might contain the path to your sleep command. This could also cause an error.

Batch script is not executing another batch if I run from jenkins

I have installed Jenkins 1.55 exe in windows environment. I have a build script*.bat) which internally calls another (.bat). While executing manually it is working fine but while trying in jenkins only first batch script is executing without triggering the second one and it is giving as success.
Is there any solution for this?
D:\Jenkins\workspace\9.0_TP_Build>cd D:\PortalScripts\9000 portal\
D:\PortalScripts\9xxx portal>WL9.0-TPRefresh-doPortalNightlyBuild2.bat ---> THis is the file i am executing
D:\PortalScripts\9xxx portal>cd d:\PortalScripts\axxx portal
D:\PortalScripts\9xxx portal>call WL-9000-TPRefresh-BuildPortal.bat a.x.x.x axxx axxx --> THis is the second file needs to be executed...but the below echo are from the first batc files...
D:\PortalScripts\9xxx portal>REM #echo off
D:\PortalScripts\9xxx portal>SET HR= 5
If you call a batch file from another, the first one will exit after executing the 2nd one, unless it is called with "call". For example:
call WL9.0-TPRefresh-doPortalNightlyBuild2.bat
instead of just:
WL9.0-TPRefresh-doPortalNightlyBuild2.bat
There is a similar question/answer here, with an example: Batch Closing before end of file?

System cannot find the path specified

When I execute my batch file and write the output to a text file, I am getting error message "system cannot find the path specified" in the command prompt. I have to find which line in my batch script, throws this error or the path which is not found.
Note: As I have so many lines in my script, I cannot go through each line and check for the path.
I have to get the path or the line which causes this error when this error comes.
The quickest way to debug an error like this would be to
Remove #echo off if you have that at the top of your script
Remove any >nul's or similar you have for any commands
Add a pause at the very end of the script
Now run the script and look at the output for each of the commands until you find the error, and then you can see which command it did it for.
Look the first place in your batch that requires the path, i.e., a call like "copy" or "del", create a new line after it and put exit. Repeat further down the batch file until the problem appears.

how to stop calling another batch script from main batch script if first calling batch script gives any error..?

I have one windows batch script where I am calling two other batch script. What I want - if my first batch gives any error it should not call second batch; rather it should immediately exit the main batch script. Here is my main batch file(main.bat) code:
echo.[INFO] Calling Joomla setup batch file
call joomla_setup.bat
echo.[INFO] Finished Joomla setup batch file execution
echo.[INFO] Calling Build and deploy setup batch file
call build_deploy.bat
echo.[INFO] Finished Build and deploy setup batch file execution
ECHO.&ECHO.Press any key to exit COSOMO installer.&PAUSE>NUL&GOTO:EOF
In this code, if joomla_setup.bat gives any error in execution I want main.bat not to call build_deploy.bat and exit immediately. Any quick and useful response would be appreciable.
call joomla_setup.bat || exit /b 255
Of course, that relies on joomla_setup.bat using exit codes to signal failure. If that's not the case it would be helpful to know how you would determine failure in your example.

Resources