Post build event in VS2012..Running a Batch file - visual-studio-2012

Am trying to run a Batch file in Post build event in Visual studio.
Referred Can we execute a .bat file in post build event command line in visual studio? for reference.
When i post the line
xcopy "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin" /S in postbuild
am getting the expected result
Same line i put in bat and tried calling
call "$(SolutionDir)\Deploy.bat"
or
call "Physical path\deploy.bat"
Am getting excited with code 1. What am i doing wrong here ?
Can i specify macros inside batch file ?
Thanks

You are getting a VS error because it returned an exit code that is not 0. This does not necessarily mean there was an error.
The error code returned means that no files were copied.
These are the return codes for Xcopy:
Exit Code
0 Files were copied without error.
1 No files were found to copy.
2 The user pressed Ctrl+C to terminate xcopy.
4 Various errors including insufficient memory or disk space, an invalid drive name, or invalid syntax.
5 Disk write error occurred.
Try this code in your batch file. Use the /Y so that you will not have to deal with any prompts. You can handle the return code of 1 with another action or just return 0.
VS Post Build command line code:
CALL "$(SolutionDir)"Deploy.bat "$(ProjectDir)bin" "$(SolutionDir)Deploy\bin"
Deploy.bat file
Xcopy %1 %2 /S /Y
If errorlevel 1 #exit 0

Related

Use python, to open cmd window with the directory supplied as an argument as the current directory

I want to use Python to open a directory in a Win10 cmd window, and keep the window open.
I made a batch file named: open_dir_in_cmd_window.CMD:
CD /D %1
I tested that batch file successfully, by creating another batch file named, Test.cmd:
Rem "open_dir_in_cmd_window.CMD" "f:\backup"
"open_dir_in_cmd_window.CMD" "f:\backup"
A very helpful webpage provides the following example, which I seem unable to follow correctly:
Spaces in Program Path + parameters with spaces:
CMD /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
I made a python script, which contains the following lines, which alas, triggers an error message:
import subprocess
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
When I open a Command Prompt window and run:
"C:\ProgramData\Anaconda3\python.exe" E:\open_dir.py
I get an error message, SyntaxError: invalid syntax, with this:
subprocess.run(cmd /k "E:\open_dir_in_cmd_window.CMD f:\backup")
^
I've tried many different permutations of double quoting and can't figure out the right way to do it.
I have spent many hours hunting on the web and trying to figure this out and I do not know what to do.
Any suggestions would be appreciated.

How WinSCP determined Errors? [duplicate]

I'm new to WinSCP. I'm trying to set an Errorlevel for the batch IM running. If the Errorlevel= 0 PRINT SUCCESS and transfers those files with Errorlevel 0 to folder call success.
if the error not equal to 0 moves the file with the error level to different folder call errors. Any suggestions.
HERE IS MY.bat
WinSCP.exe /console /script="c:\users\PDP\script.txt" /log="c:\users\PDP\lastrun.txt"
if %ERRORLEVEL% equ 0
echo Success
sendmail.exe -t < success_mail.txt
move OPTTXM* c:\users\PDP\sent ( the files in the batch start with OPTTXM*)
exit /b 0
if %ERRORLEVEL% neq 0 goto error
echo Error!
sendmail.exe -t < error_mail.txt
move ????????????????????????????????? ( how you can get inside each file and check the status
)
exit /b 1
Thanks in advance
You can make WinSCP create XML log with files that were and were not transferred. But it would be difficult (if possible at all) to parse/process those XML files in a batch file.
You better use a more powerful scripting language, like PowerShell.
See for example Move files after upload using PowerShell/WinSCP Script.
(there are many other similar questions here)
There's also WinSCP article on this topic:
Moving local files to different location after successful upload

How to return information from Excel to the caller

I start Excel from the command line and my add-on does some work. when it's done, I want to return some info to the caller. At least 0/1 for success failure or better also an optional error-message.
By caller I mean the command or process that started Excel. e.g. in a Windows command script I could call excel like this:
Excel.exe SomeWorkbook.xlsx /p C:\Somedir /e
when you call an executable in Windows, it can return an numeric code or set an error.
in a script you could check the result like this:
if %errorlevel% neq 0 (
echo some error occurred...
)
MessageBoxes, etc. are no option, because this whole task should be triggered by another application automatically without any user-interaction.
How can we do that?
You could use the status bar:
Application.StatusBar = “your message here”
As far as I know, the message box requires a button to be clicked: macro will wait...
I ended up using text files: i.e when the add-on finished correctly, it will create an empty file OK.txt and when an error occurred it will create a file named ERR.txt that contains the error-message.
Now it's easy for the calling script to check the result:
OK.txt exists: everything is fine - delete OK.txt
no file exists: a fatal error has happened: show a general error message
ERR.txt exists: an error occured: maybe display the error text (contents of the text-file) to the user, delete ERR.txt

Batch file fails when solution is built by Jenkis, but works when solution is built via command line

The problem
I have a batch file the is being called from a Pre-Build event.
When the solution is being built by Jenkins it fails.
When the solution is being built from MSBuild or visual studio in command line It works fine.
My question is, what could cause such a behaviour?
Details
In my solution (.Net 4.5 and C++ projects), I have a batch file that is called from one of the project's Pre-Build events.
It works fine, except when I am trying to build the solution via Jenkins (CI Server).
The error is :
The command ""C:\Jenkins\apps\jenkins\jenkins_home\jobs\Builld_SMW2\workspace\Autosync\CopySymAlignFiles.bat" "C:\Jenkins\apps\jenkins\jenkins_home\jobs\Builld_SMW2\workspace\" "C:\Jenkins\apps\jenkins\jenkins_home\jobs\Builld_SMW2\workspace\Autosync\" "Debug"" exited with code 1.
from the error report, I copied the command that was used for attempting to build the solution:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /p:Platform=x86 /p:configuration=Debug /t:Rebuild /verbosity:quiet /p:WarningLevel=0 /p:Platform=x86 /p:configuration=Debug /t:Rebuild /verbosity:quiet /p:WarningLevel=0 ..\SMW2_Analysis.sln
(I am aware that the arguments are being passed twice, I have no Idea why jenkins does that, but this is not the issue...)
If I run this command from command line, the batch file works fine once the build event is triggered.
The content of CopySymAlignFiles.bat is:
Echo off
set solutionDir=%1
set projectDir=%2
set configuration=%3
REM Removing quotes
set solutionDir=%solutionDir:"=%
set projectDir=%projectDir:"=%
set configuration=%configuration:"=%
REM Does strings have a trailing slash? if so remove it
IF %solutionDir:~-1%==\ SET solutionDir=%solutionDir:~0,-1%
IF %projectDir:~-1%==\ SET projectDir=%projectDir:~0,-1%
IF %configuration:~-1%==\ SET configuration=%configuration:~0,-1%
REM This is just for easier debugging
Echo Solution Directory is: "%solutionDir%"
Echo Project Directory is: "%projectDir%"
Echo Configuration directory is: "%configuration%"
Echo Current Directory is: "%CD%"
Echo The batch file is executed at "%~dp0"
REM Copying sym_align files
copy /Y "%solutionDir%\SymAlignGlue\sym_align.dll" "%projectDir%"
copy /Y "%solutionDir%\SymAlignGlue\sym_align.ctf" "%projectDir%"
set isPV=%configuration%=="PV Release"
IF %isPV% copy /Y "%solutionDir%\SymAlignGlue\Release\SymAlignGlue.dll" "%projectDir%"
IF NOT %isPV% copy /Y "%solutionDir%\SymAlignGlue\%configuration%\SymAlignGlue.dll" "%projectDir%"
REM The result...
IF %ERRORLEVEL% EQU 0 (
REM Success
Echo Copied SymAlign files.
exit /b 0
)
IF NOT %ERRORLEVEL% EQU 0 (
REM Error
Echo An error was found while Copied SymAlign files. Error level was %ERRORLEVEL%
exit /b %ERRORLEVEL%
)
Some additional Information:
I am running Jenkins as a service and I have the service logon as An Administrator.
Does Anyone have an Idea how can this happen?
At the end, it seems that the issue was that there was a duplicate workspace folder.
1 was updated, and the other 1 was built, resulting the tested workspace did not get latest updates.
according to this link this was due to the way jenkins behaves with multiple configurations.

cscript - Invalid procedure call or argument when running a vbs file

I've been trying to use check_time.vbs to check the Windows time.
Here's the script: http://pastebin.com/NfUrCAqU
The help message could be display:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs /?
check_time.vbs V1.01
Usage: cscript /NoLogo check_time.vbs serverlist warn crit [biggest]
Options:
serverlist (required): one or more server names, coma-separated
warn (required): warning offset in seconds, can be partial
crit (required): critical offset in seconds, can be partial
biggest (optional): if multiple servers, else use default least offset
Example:
cscript /NoLogo check_time.vbs myserver1,myserver2 0.4 5 biggest
But I get the following error when running:
C:\Program Files\NSClient++\scripts>cscript //NoLogo check_time.vbs 0.asia.pool.ntp.org 20 50
C:\Program Files\NSClient++\scripts\check_time.vbs(53, 1) Microsoft VBScript run
time error: Invalid procedure call or argument
The screenshot:
Manually execute w32tm still works fine:
What might be the cause of this?
IIRC the (53,1) indicates that the error is on line 53. At this point it is expecting an array of regexp matches with at least one item (index 0) and one sub-match (i.e. the object in position 0 in the array has an array property called SubMatches with at least one item in it.
It is not checking to make sure this structure is present and correct before trying to use it.
My assumption is that the regexp call is failing to find anything to match, presumably because the input string is not in the expected format. You could output the content of strOutput before that line to see what it contains - it could be a date/time representation in a different localized form than the regexp is designed for. You could also output the content of input after each call to objProc.StdOut.ReadLine - this would show you if the call to w32tm.exe returned a useful error message that is being skipped over by the script which is just looking for the value returned when all is well and ignoring the possibility of different output.
The culprit is the /nowarn argument:
w32tm /monitor /nowarn /computers:0.asia.pool.ntp.org
The following arguments were unexpected: /nowarn
Exiting with error 0x80070057
Remove it from the script, and now it works:
cscript //NoLogo check_time.vbs 0.uk.pool.ntp.org 20 50
NTP OK: Offset -2.4262131 secs|'offset'=-2.4262131s;20;50;

Resources