Visual studio post build event command line "for" syntax - c#

Solution1 : { Project1 (windows form), Project2 (class library) }
Trying to copy all .dll(s) I get from after compiling Project1, from the default directory (same as the .exe) to a /lib sub-folder.
if not exist Lib mkdir Lib
for %i in (*.dll) move /Y "$(TargetDir)%i" "$(TargetDir)Lib\%i"
I have problem with the for %i in (*.dll) syntax. What is the correct way of doing it?
Note: This would give no errors (but would copy only 1 .dll, not all):
if not exist Lib mkdir Lib
move /Y "$(TargetDir)first.dll" "$(TargetDir)Lib\first.dll"

You were almost there. You should use a double percentage %% and do:
for %%i in (*.dll) do move /Y "$(TargetDir)%%i" "$(TargetDir)Lib\%%i"

Related

XCopy failing during build but not from command line?

I have a console program that outputs its exe & dlls to a specified directory.
As a post build event I am trying to copy everything in that directory to another directory.
My xcopy command works from command prompt but fails in VS2010? How can this be?
I am testing it by going to the project folder and executing the following in command prompt. (it is the output from VS2010)
In my post-build event:
xcopy "$(OutDir)*.*" "$(TargetDir)..\..\Foo\Bar\" /s /y /i
From command prompt I am executing the following which works.
xcopy "..\..\..\..\MyDir\baz\zip\*.*" "c:\1\2\3\MyDir\baz\zip\..\..\Foo\Bar\" /s /y /i
Sorry about the directory names.
End result should be two directories with the same files in them:
c:\1\2\3\MyDir\baz\zip
c:\1\2\3\MyDir\foo\bar
The target path is relative to the output directory.
When its executed as part of the build it gives an exit code 4
Initialization error occurred. There is not enough memory or disk
space, or you entered an invalid drive name or invalid syntax on the
command line.
Where am I going wrong?
Got it,
I changed the xcopy command in my post build event to:
xcopy "$(TargetDir)*.*" "$(TargetDir)..\..\Foo\Bar\" /s /y /i
The executed result being:
xcopy "c:\1\2\3\MyDir\baz\zip\*.*" "c:\1\2\3\MyDir\baz\zip\..\..\Foo\Bar\" /s /y /i
Which VS2010 much preferred, I guess you can't use a relative path without a base path.
Why you don'y call batch file which will run xcopy for required files source to destination?
call "$(SolutionDir)scripts\copyifnewer.bat"
With copyifnewer.bat looking like this:
IF NOT EXIST <destination> md <destination>
XCOPY /Y <file> <destination>

Change connectionstring in Visual studio whening publishing

I have an asp.net application. I have two databases set up, a test and production. Write now before I push any updates I manually change my conn string to point to production. Is there a way to do this when I publish my application, automatically change to production? Thanks for any help.
You can use web.config transformations for this.
Yeah web.config transformations or I think a better approach is to separate your configs from your web/app.config, so having a folder for each build configuration for example
Config/Debug/connectionStrings.config
Config/Stage/connectionStrings.config
Config/Production/connectionStrings.config
Then your app or web.config would look like
<connectionStrings configSource="bin\connectionStrings.config" />
To get the environment specific config to your bin directory create a post build event which copies the config based on the current build configuration or copy them manually to your bin. This way you don't have to relay on a build to get your desired configuration.
If you really want this automated your post build event could look something like
"$(SolutionDir)CopyConfigs.bat" "$(ProjectDir)" "$(ConfigurationName)" "$(OutDir).."
and the batch file so the copying can be reused between projects:
#echo CopyConfigs.bat :
#echo Coping Config Files...
set projectDir=%1
set configurationName=%2
set outDir=%3
REM Trim Quotes
for /f "useback tokens=*" %%a in ('%1') do set projectDir=%%~a
for /f "useback tokens=*" %%a in ('%2') do set configurationName=%%~a
for /f "useback tokens=*" %%a in ('%3') do set str3=%%~a
#echo Project Directory: %1
#echo ConfigurationName: %2
#echo OutDir: %3
if not exist %1 goto ProjectDirectoryNotFound
REM Copy the configuration files to the projects output directory
xcopy /Y "%projectDir%Configuration\%configurationName%\*.config" "%outDir%"
xcopy /Y "%projectDir%Configuration\*.config" "%outDir%"
#goto END
:ProjectDirectoryNotFound
#echo Project Directory %projectDir% was not found.
#goto END
:END
#echo Coping Config Done

Create Batch file for iexpress

I am trying to use iexpress to run my batch file which will execute the 2 exe & 1 msi files for me. when i try to do it manually, it works.
following is the code in my batch file.
Start /wait %CD%\1.exe /q
Start /wait %CD%\2.exe /q
msiexec.exe /i "%CD%\3.msi"
but this doesn't seem to be working when i create an exe file from iexpress.
Reference
Above mentioned article has some code(to copy files to temp folder) & but i am not able to understand the syntax.
MKDIR %Tmp%\<UNIQUE PRODUCT NAME>
XCOPY . %Tmp%\<UNIQUE PRODUCT NAME> /S /E /Y
%Tmp%\<UNIQUE PRODUCT NAME>\setup.exe
The problem is that, as you can see from your screenshot, the batch file is being executed by command.com, not cmd.exe. (If you don't specify an interpreter, IExpress uses command.com. Ouch.) So there are no variables like %cd% or %~dp0.
You likely don't need them anyhow. But you do need to execute your batch file explicitly in IExpress like:
cmd.exe /c file.bat
so that it uses a modern command interpreter.
The second bit of code in your question makes the files persistent (ie they won't be deleted after the IExpress archive terminates) by xcopying them to a different directory.
Here is what it means:
1) Creates a directory(MKDIR) with name of "UNIQUE PRODUCT NAME" in the path stored in %TMP% Environment Variable, which normally points to: C:\DOCUME~1\yourusername\LOCALS~1\Temp
MKDIR %Tmp%\<UNIQUE PRODUCT NAME>
2) Then copy recursively all installation files from current folder into the new folder created before.
XCOPY arguments:
/S Copies directories and subdirectories except empty ones.
/E Copies directories and subdirectories, including empty ones.
Same as /S /E. May be used to modify /T.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
XCOPY . %Tmp%\<UNIQUE PRODUCT NAME> /S /E /Y
3) Finally execute the application from the new location
%Tmp%\\setup.exe
Hope this helps
Try replacing %CD% with %~dp0
Assuming that 1.exe is in the same folder as your batch script.
Your %CD% is not working. Please be sure that CMD extensions are enabled (type CMD /x to enable and CMD /y to disable) then expand the %CD% with this code
SET CURDIR=%CD%
Start /wait "%CURDIR%\1.exe" /q
Start /wait "%CURDIR%\2.exe" /q
msiexec.exe /i "%CURDIR%\3.msi"
And I'm not sure that you can start an exe from that location (APPDATA) for security reasons.
Thanks a lot for this forum discussion.Finally i could able to compile all msi files and executables in an one .exe file.
Complete procedure as follows create a batch file
echo on
SET CURDIR=%CD%
msiexec.exe /i "%CURDIR%\1.msi"
"%CURDIR%\3.EXE"
"%CURDIR%\setup.exe"
echo off
You can arrange any number of exe files or msi files as you wish and save the batch file as yourfile.bat.
Now the tricky part is before you proceed to Iexpress, convert the batch file to exe with the software provided by http://www.f2ko.de/programs.php?pid=b2e
Now when you run the program keep the 'Invisible Application' checked to hide the command prompt.You can also encrypt your exe with the password.'Delete at Exit' is optional as the temporary folder will be automatically deleted when the execution of files completed.
Once you successfully compile the batch file,execute the .exe file created.
Bingo!! you'll not see the command prompt window and your applications start executing sequentially.
Begin your Iexpress tool and Add all your files present in the batch file(except batch file).On the ‘Install Program to Launch’ screen, leave the Post Install Command blank and find the following in the Install Program dropdown:'demo.exe'and proceed further to create your complete bunch of single package. Cheers!!

What does this funnelweb command mean in visual studio? and why did it exit with 4?

In visual studio on the post build event command line I have the following:
xcopy $(TargetPath) $(SolutionDir)FunnelWeb.Web\bin\Extensions\ /Y
and its creating the error:
Error 1 The command "xcopy C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Extensions.MetaWeblog\bin\Debug\FunnelWeb.Extensions.MetaWeblog.dll
C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Web\bin\Extensions\
/Y
xcopy C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Extensions.MetaWeblog\bin\Debug\CookComputing.XmlRpcV2.dll
C:\Users\Exitos\Desktop\FunnelWeb-2.0.2.572-source\src\FunnelWeb.Web\bin\Extensions\
/Y" exited with code 4. FunnelWeb.Extensions.MetaWeblog
Im confused as to where the $(TargetPath) and $(SolutionDir) are set, and why this error happened?
xcopy exit codes can be found here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true
I suspect the problem is that you need double quotes around your paths:
xcopy "$(TargetPath)" "$(SolutionDir)FunnelWeb.Web\bin\Extensions" /Y
Becouse of spaces inside your path.
When you open a .sln, $(SolutionDir) is the directory where it came from. If you
want to change it, move the root folder for your .sln file.
Hope it helps.

Command Line Compiling a Win Forms C# Application

I'm trying to create a script to compile an Windows Forms C# 2.0 project from the command line (I know, I know.. I'm reinventing the wheel.. again.. but if somebody knows the answer, I'd appreciate it).
The project is a standard Windows Forms project that has some resources and references a couple external assemblies. Here is a list of the files:
Program.cs // no need to expand on this on :)
frmMain.cs // this is a typical C# windows forms file
frmMain.designer.cs // .. and the designer code
frmMain.resx // .. and the resource file
MyClasses.cs // this contains a couple classes
Properties\AssemblyInfo.cs // the Properties folder -- again pretty standard
Properties\Resources.Designer.cs
Properties\Resources.resz
Properties\Settings.Designer.cs
Properties\Settings.settings
References\AnAssembly.dll // an assmebly that I reference from this application
So far I've identified the following programs/tools that I would need:
csc.exe // the C# compiler
al.exe // the assembly linker
resgen.exe // the resource compiler
And this is my script so far:
#echo off
set OUT=Out
set AL=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\al.exe
set RESGEN="C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\resgen.exe"
set COMPILER=C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe
echo.
echo Compiler: %COMPILER%
echo.
if "%1"=="/help" goto help
:start
echo Starting...
set REFERENCES=.\References\AReferencedll
set SRCFILES=Program.cs frmMain.cs frmMain.designer.cs MyClasses.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs Properties\Settings.Designer.cs
del /Q %OUT%\*
%RESGEN% /compile frmMain.resx,%OUT%\frmMain.resources
cd Properties
%RESGEN% /compile Resources.resx,..\%OUT%\Resources.resources
cd ..
%COMPILER% /target:module /out:%OUT%\app.module %SRCFILES% /reference:%REFERENCES%
%AL% %OUT%\app.module /embed:%OUT%\frmMain.resources /target:winexe /out:%OUT%\app.exe /main:App.Program.Main
goto done
:error
echo Ooooops!
:done
echo Done!!
In the end, no matter how I spin it I get different errors from the linker, or the final executable will just not run - it crashes.
Please help (MSDN didn't help too much..)!
I like to cheat for these kinds of things. Take a working project and run the following command on it
msbuild Project.csproj /t:rebuild /clp:ShowCommandLine
The output of that will show you the command msbuild uses to compile the project, which you can then take and modify as you like.
Is there a reason why you are not using msbuild? It can compile any visual studio project/solution in one command...
msbuild.exe <solution filename>
Just use MSBuild passing in the solution or project file.
The best way to use MSBuild is via the Visual Studio Command Prompt; it sets all the required environment variables for you. This command setup is also available in the SDK.

Categories