Post-Build Event Fails to Run Batch File - batch-file

I recently started adding Post-Build events to my projects and today I came across an annoying issue.
Post-build event command line:
"$(ProjectDir)PostBuildRelease.bat"
PostBuildRelease.bat - 1
CMD
SET parameter=%1
CD %1
The above errors with message:
'´╗┐CMD' is not recognized as an internal or external command,
operable program or batch file.
"Saved parameter "
Which is strange, because I definitely have another project where the first line reads CMD and it works perfectly.
Setting the first line to SET parameter=%1 also complains that SET is not a recognised command.
PostBuildRelease.bat - 2
Trying the same but with an empty file gives this error (build fails):
The command "C:\Users\name\ITSelfHelp\ITHelp\PostBuildRelease.bat" exited with code 1.
PostBuildRelease - 3
Entering the CMD code directly into the Post-Build Event command line field results in a successful post-build event, but this is not ideal for me and seems strange as I can run from an external .bat in other projects.
Can anyone explain this behaviour, or let me know how I can debug this?

It turns out the text editor in Visual Studio was encoding my batch script.
To recreate this:
Right-click the solution
Add file
Text file
Rename file.bat
Write the script in Visual Studio
Build
Check the output window for the error
To resolve:
Delete the file created in Visual Studio
Created a new .bat file in the same location using Windows Explorer
Open the file with Notepad++ and paste in the script
Save and build
I also noticed that syntax highlighting was not being applying in the file created by Visual Studio, which is a bit of a giveaway along with the ´╗┐ in front of the errors.

Related

The term 'behave' is not recognized as the name of a cmdlet, function

I have Python 3.10.7 with the follwing packgaes installed using pip install in command line:
behave 1.2.6
selenium 4.4.3
These have also been added to the packagelist of the project using the project config in pycharm
Also behave is in the systme path as well along with python.
I am trying to use the behave command but I am getting the following error:
behave : The term 'behave' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ behave features\myfeaturefile.feature
Following is my project directory:
Following is my project in pycharm:
I need help in fixing this problem as I am new to this tool. Thanks
First check if you have behave and python in the path variables.
Check if you can execute the below command
C:\demo>behave -h
If this does not return a list of help commands associated with behave, this means that behave is not in the path variables
Alternatively, you can check it via cmd using the "PATH" command. This will show you all the entries in the system path
C:\demo>PATH
PATH=C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Program Files\AdoptOpenJDK\jdk-11.0.11.9-hotspot\bin...
If behave or python are not in the path variables, you can set them using
set PATH=%PATH%;c:\PATH_TO_BEHAVE
NOTE: Setting path using cmd is only valid till the time the cmd window is open. Once you close it, the path variable will be rest to original values.
Behave, along with a few other .exe files is sitting in the:
C:\Users\chauhany\AppData\Roaming\Python\Python310\Scripts
As per your instructions #Manish Agarwal, I added the C:\Users\chauhany\AppData\Roaming\Python to the PATH from the command line and re-started the machine. But it didn't help. I then moved the behave.exe from the above location to my python 3.10 directory which is sitting in C:\Program Files\Python310 and python is in the system path, and restarted my machine again.
I then deleted and recreated my project with the new feature file (same directory structure).
If I now run the same command, that is, features\myfeaturefile.feature from
C:\Users\chauhany\PycharmProjects\martechBehaveProject>
I get a FileNotFound error
So I went to the folder where the file is actually sitting that is:
C:\Users\chauhany\PycharmProjects\martechBehaveProject\features\steps> and then re-ran the command and it worked.
I was under the impression that if you have just one feature file you don't have to specify the actual feature file and it can be run from anywhere in your project i.e., from any location which certainly is not correct.

MSDeploy Auto Generated deploy.cmd path issue?

I've been working on trying to figure out why our auto-generated deploy.cmdscripts will not work when installed to C:\Program Files (x86)\OurProgram. I finally narrowed down the issue to the closing parenthesis in (x86) as the script would terminate with "Files was unexpected at this time". One possible solution was to manually create another batch file to set _DeploySetParametersFile to a Windows path ( C:\Program^ Files^ (x86^)\OurProgram\Program.Parameters.xml). This isn't a valid solution for me, so I dug into the auto generated file and found the issue to be 2 variables:
RootPath
_DeploySetParametersFile
Both of which were referenced using %'s(i.e. %RootPath%), however as soon as they were changed to !'s(i.e. !RootPath!), no 2nd script was needed and I can run the cmd script from Program Files (x86) just fine. The issue of course with this is, the deploy.cmd file is auto-generated on each build, thus I have to change it for each build I do. My question is this: Is there a way to edit the template Visual Studio uses to generate the deploy.cmd file? If so, where? It seems so ridiculous that Microsoft would have this be an issue in their web deployment.

Unable to execute pre-build event stored in batch file at the time of publishing WPF application

I am executing a T4 template using a pre-build event with the following script added in the Properties > Build Events > Pre-build event command line:
set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
%textTemplatingPath% "$(ProjectDir)AssemblyFileVersion.tt"
While building the solution the above script is working fine and I am able to get output from T4 template file. But I am looking to execute this script while publishing the WPF application instead of building the solution.
I have moved the script in the batch file and put a reference of that batch file inside .csproj file.
<Target Name="BeforePublish" BeforeTargets="MSDeployPublish">
<Exec Command="$(SolutionDir)v1.bat" />
</Target>
While I am publishing the project, I am getting following error.
The command "xx\v1.bat" exited with code 9009
The file exists in the location but I am not able to figure out if there is an issue with the script or the way I am trying to achieve the result.
Edit:
When I checked the Output window, it was due to the space in one of the directories (Visual Studio 2015 in my case) included in the path. For now, I have placed v1.bat inside D: folder.
<Target Name="BeforePublish" BeforeTargets="MSDeployPublish">
<Exec Command="D\v1.bat" />
</Target>
Now when I am trying to publish the app, it shows me the following error.
The command "D:\v1.bat" exited with code 1
and the Output window is showing "The system cannot find the path specified".
It seems like the problem is with the variables used in the script and not in the path of the v1.bat file defined in the Target above because when I am simply keeping echo StackOverflow in the batch file it is showing the text and successfully publishing the app. To me, it seems like the way I used the script in the pre-build command line in Visual Studio IDE would be quite different the way if I need to run the same script from batch file.
Thanks
The following pre-build event script added in the Properties > Build Events > Pre-build event command line in Visual Studio IDE:
set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe"
%textTemplatingPath% "$(ProjectDir)AssemblyFileVersion.tt"
needs some changes when it is moved to a batch file which is intended to be executed while publishing the application instead of building it.
I need to change all variables from $(Variable1) to %Variable1%, so my script became
set textTemplatingPath="%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\%VisualStudioVersion%\texttransform.exe"
if %textTemplatingPath%=="\Microsoft Shared\TextTemplating\%VisualStudioVersion%\texttransform.exe" set textTemplatingPath="%CommonProgramFiles%\Microsoft Shared\TextTemplating\%VisualStudioVersion%\texttransform.exe"
%textTemplatingPath% "%ProjectDir%AssemblyFileVersion.tt"

Unable to do a simple rename file in ReleaseManagement during deployment

I have a problem that has been bugging me.
I have a Visual studio 2013 solution that goes through Release management.
One of the release steps in Release management is running a simple batch file using the Actions -> SxSBatchCommand tool. This is a custom action that simply executes a batch file and the batch file executes a simple rename that goes like this:
rename "$(currentDir)\project.ev1.app.config" "project.exe.config"
Which basically takes a specific app.config and applies it in the deployed directory as the main config.
All this works well but because I don't like all the batch files in my project, I decided to delete them and to use the WindowsOS -> Rename File or folder tool
So I deleted the SxSBatchCommand and added the "Rename File or Folder" with parameters:
FileFolderName: project.ev1.app.config
DestinationName: project.exe.config
I also tried:
FileFolderName: $(currentDir)\project.ev1.app.config
DestinationName: project.exe.config
This failed during deployment with the message:
The following command failed with the exit code '1' : powershell -command ./ManageWindowsIO.ps1 -Action Rename -FileFolderName 'project.ev1.app.config' -DestinationName 'project.exe.config'
Any ideas why such a simple command wouldn't be working?
Thanks
Can you run this exact command in powershell console ? You can also click on the button next to the command in the release log to grab a better detaild log whith explanation what went wrong .
The file could be in use , or without proper permissions for modification , you can look for the agent's logs in the target machine .
By the way if you are trying to make a file changed to the name of a configuration file for a win service or want to use transformation in a non web application you can use slow cheetah to accomplish it .

output to command line from windows gui program

I have a win32 gui based exe which is GDI based -
The program accepts command line arguments when it starts, and
I need a way to print out a message if the command line is incorrect.
At the moment, I am displaying an error message using MessageBox,
and would like to know how to print out this error message if the
user started the program from the dos command line. -
:)
ok this does what I want, but it requires me to press enter otherwise I dont see the current path displayed..
void RedirectIOToConsole()
{
int hConHandle;
FILE *fp;
AttachConsole(-1);
hConHandle=_open_osfhandle(GetStdHandle(STD_OUTPUT_HANDLE),_O_TEXT);
fp=_fdopen(hConHandle,"w");
fprintf(fp,"\ntesting\n");
fflush(fp);
_close(hConHandle);
FreeConsole();
}
Most applications that need to accomplish this will create a *.com version of their app (which will sit alongside the .exe). All shortcuts will point to the .exe, but if the user happens to type the name on a command line, then the .com version will end up executing (as CMD will search for .com files before .exe files) The .com version is often a thin wrapper which will invoke the .exe and wait for it to finish or utilize shared .dll files, but it exists purely so that output can be funneled to the proper console window and won't "release" it until the application finishes.
As an example, Microsoft's Visual Studio uses this approach:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE>dir devenv.*
Volume in drive C has no label.
Volume Serial Number is A482-59BA
Directory of C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE
07/26/2012 05:05 PM 7,680 devenv.com
07/26/2012 07:08 PM 406,976 devenv.exe

Resources