Post-Build script throws error in VIsual Studio 2010 - c++

I've seen several threads with the same issue, but none of the solutions seems to work for me so I'm trying it here.
I need a post-build script in VS2010 that moves a .lib file to a directroy (which possibly dosn't exist yet; if so create it).
I am using this, which returns error code 2:
xcopy /y "$(TargetDir)$(ProjectName).lib" "$(SolutionDir)lib\$(ProjectName).lib"
Also tried, which returns error code 1: (what is the difference?)
copy /y "$(TargetDir)$(ProjectName).lib" "$(SolutionDir)lib\$(ProjectName).lib"
The most common issues people seem to hav is the lack of quotes on paths, but I have that.
Why dosn't it work?

This ought to be closer:
if not exist "$(SolutionDir)lib" md "$(SolutionDir)lib"
xcopy /y /d "$(TargetPath)" "$(SolutionDir)lib"

After a quick test on the command line what's happening with copy is it is failing because the directory does not exist. What is happening with xcopy is it is failing when it prompts for whether the target is a file or directory when it finds the directory doesn't exist. /-Y may be set in your COPYCMD environment variable or your target path may be misleading causing a prompt for whether the target is a directory or file which is not supressed by the /Y flag for overwrite.
Example: xcopy /Y "C:\test.txt" "missingdirectory\test5.txt"
Obviously the easiest solution is to check if the directory exists and create it if it's missing before doing the copy in your post-build script.

Related

Postbuild Event Copy build output to parent sibling undertermined folder

I have two solutions, WildCougarFarm and WildLionFarm both depending on a shared library
\Folder 1
\WildCougarFarm
\WildSharedLib (Separate Solution)
\Folder 2
\WildLionFarm
\WildSharedLib (Separate Solution)
When \WildSharedLib is built I want to run a post build script that automatically copies the output directories contents to the sibling \Wild[Lion|Cougar]Farm solution folder. Depending on whether WildLionFarm exists, I want it to copy with say wildHorseFarm in future.
I need an xcopy expression with a regex but xcopy doesn't support this.
Any ideas how I can accomplish this?
I figured it out.
This is pretty cool, because it uses a foreach type loop... so we ask if the parent folder of the solution contains any folders that end in farm. For any returned it will execute the xcopy statement to copy the files to that folder.
for /d %%a in ($(SolutionDir)..\*farm) do xcopy $(TargetDir)*.* "%%a\dependancies" /s /e /f /h /k /y /i

Windows XP Batch IF & XCOPY

I've been struggling with trying to get the below (example) batch file to work on Windows XP SP3. The IF NOT EXIST part seems to work, but I keep receiving the "Does <path\filename.ext> specify a file name or directory name on the target" message despite using the /I flag on XCOPY:
#IF NOT EXIST "\\SERVER\PATH\TO\FILE DIR" (
MKDIR "\\SERVER\PATH\TO\FILE DIR"
XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
) ELSE (
XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM" /I
)
My understanding is that with the /I switch, XCOPY should create the directory structure if it doesn't exist - at least it does when I don't specify a file name. Unfortunately for the requirements of this project, I must specify a file name and cannot keep the original as it's a template file that gets manipulated with an automated process every day.
So, I tried to get around the issue with XCOPY and the directory path not existing by checking for the existence of the path, and if it's not there, creating it with the MKDIR command and then copying the file - but XCOPY still prompts as to whether the destination is a file or directory, which doesn't make sense but maybe I'm missing something.
Just to be clear, this is on Windows XP SP3.
Any ideas?
You might find it easier to do something like this:
md "\\SERVER\PATH\TO\FILE DIR" 2>NUL
copy "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"
The initial 'md' will attempt to create the directory. If it already exists, it will output an error message to STDERR. The 2>NUL redirects that to Windows' built-in "null device", which is to say, it just swallows the error message. Assuming you have the appropriate permissions, you can be sure that this directory exists now.
The copy command just copies your file. No need to use xcopy to copy a single file - that's both overkill and fraught with little gotchas like being prompted whether it's a file or directory.
Since the destination file doesn't exist before you copy, xcopy isn't sure if it needs to create a new directory called "FILE FINAL.XLSM", and put the file in there. By the way, since you already create the destination dir, you don't need the /I on your xcopy. Here are a couple ways to do what you want:
echo F | xcopy .... (feed the "F" answer to xcopy)
copy .... (you don't need to use xcopy for a single file)
echo f|XCOPY "\\SERVER\PATH\TO\ORIG FILE\FILE TEMP.XLSM" "\\SERVER\PATH\TO\FILE DIR\FILE FINAL.XLSM"
should copy the file AND create the directory. No idea why the option to specify "this is a file" wasn't made available, but RTFM - the /i switch is only effective if you are copying MORE than one file, and specifying \ as the last character of the destination name tells XCOPY that the target is a directoryname in any case, so /i sems redundant.
However, be careful if you follow the copy route. It's better in general to use copy /b because plain copy may fail to properly copy some filetypes (like .MPGs) - it may stop on the first ^Z. copy /b appears safe however.

TFS - xcopy not working with my TFS builds

I created a custom template for a TFS build that copies files from a Source folder to a Destination folder.
ForEach<String>
InvokeProcess
xcopy "C:SourceFolder\File1.doc" "C:DestinationFolder"
ForEach<String>
InvokeProcess
xcopy "C:SourceFolder\File2.doc" "C:DestinationFolder"
I can type the xcopy line into a command prompt and it works just fine, but for some reason not in TFS. I get no error. I have been stuck on this for a few days and now I'm just baffled. Anyone have any experience with this?
You have to fully qualify the path to xcopy. So the filename for invoke process should be "C:\Windows\System32\xcopy.exe"

Copy Directory - Post Build Event

How do I copy some directory from one place to another (not file by file)
in post build event (whats the comman line??). im using vs 2005 (c++ project)
For more clarification, here is an example that copies a folder called "ApplicationFiles" from the root of your project to the destination (binary) folder:
xcopy "$(ProjectDir)ApplicationFiles" "$(TargetDir)ApplicationFiles" /e /y /i /r
Thanks, just what I needed. Options documented here for future reference:
/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.
/I If destination does not exist and copying more than one file, assumes that destination must be a directory.
/R Overwrites read-only files.
The commandline is simply a batch script that is executed upon completion of the build. Therefore, you can just use regular Windows shell commands, such as mkdir, copy, ... To copy whole directories recursively, use xcopy <src> <dest> /E.

Batch/CMD: Adding files to Startup list

How can a batch file lists itself in the startup list of Windows???
It doesn't matter if it goes from the registry or not.
IF with the registry, please give also the command to DELETE the registry entry.
This should work under all versions from ME to 7 please.
Otherwise just XP/Vista/7.
Thanks.
Not sure i understand you, but if what you want is an easy way to execute a command/batch on startup, why not just put it in the All Users\Startup folder?
To do so programatically would just mean copying a file to that directory.
For example, in Windows Vista, the full path of that directory is:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
(you can use replace the beginning of the line with %ProgramData% or %AllUsers%\ProgramData to make it more global - such as when Windows is installed on D:).
I do not use windows7 (might get a check at the beta shortly), but I think the correct place will always be better taken from the registry, because of the Windows versions being localized. My own version of C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup here looks more like "C:\Documents and Settings\All Users\Menu Démarrer\Programmes\Démarrage" (from XP, of course)
-10 for programmers using hard-coded directory names (yes, some installers will create english/different language directories at installation).
-1 for Microsoft localising directory names...
Anyhow here is a snipet for this, valid for XP at least:
commonstartup.cmd
#echo off
for /F "tokens=3 delims= " %%k in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Startup"^| findstr /i /c:"Common Startup"') do set StartUp=%%k
echo StartUp="%StartUp%"
___Notes_____
1: Because reg.exe from Windows2000 and XP have different command arguments, maybe the W7 one has changed too so test it before set and forget.
2: To get a list of all the system directories, issue the command: reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" and read the lines. You might want to change the "Common Startup" for something else, if things are so different with W7.
3: There is also a personal/user list within HKEY_CURRENT_USER if you want this to be usable by some users only.
xcopy C:\Users\NAME\Desktop\Batch.bat C:\ProgramData\Microsoft\Windows\"Start Menu"\Programs\StartUp /O /X /E /H /K
is the correct command for windows 10. simply change the the second path to your version, and remember whenever there is a space, place a " before the word before the space, and after the word after it.
however, it MUST be opened in administrator, so after some research, i found that a batch file could be used to start a different batch file and run it in administrative mode:
runas /user:administrator C:\data\mybatchfile.bat
that should work!