copy entire folder on postbuild to a specific folder - visual-studio

Lets say ,
I have xyz.dll in the debug folder, along with resource files folders , de, da ,etc.
F be the project name .
Folder Structure as mentioned below
d:A\B\C\D\E\F\bin\debug
I have to copy the dll and resource file folders from debug folder to C1 folder .
Folder structure as shown below
d:A\B\C1.
available macros are
TargetDir : d:A\B\C\D\E\F\bin\debug
ProjectDir: d:A\B\C\D\E\F\
I tried this but its not working
xcopy $(TargetDir ) $(ProjectDir)........\c1 /R /Y
This copies only the dll not the resources folder .
Any suggestions on how I should do this ?

Use the /S switch to xcopy to include subdirectories recursively
/? is your friend..
C:\>xcopy /?
Copies files and directory trees.
XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
[/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
[/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B] [/J]
[/EXCLUDE:file1[+file2][+file3]...]
source Specifies the file(s) to copy.
destination Specifies the location and/or name of new files.
/A Copies only files with the archive attribute set,
doesn't change the attribute.
/M Copies only files with the archive attribute set,
turns off the archive attribute.
/D:m-d-y Copies files changed on or after the specified date.
If no date is given, copies only those files whose
source time is newer than the destination time.
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
/P Prompts you before creating each destination file.
/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.
/V Verifies the size of each new file.
/W Prompts you to press a key before copying.
/C Continues copying even if errors occur.
/I If destination does not exist and copying more than one file,
assumes that destination must be a directory.
/Q Does not display file names while copying.
/F Displays full source and destination file names while copying.
/L Displays files that would be copied.
/G Allows the copying of encrypted files to destination that does
not support encryption.
/H Copies hidden and system files also.
/R Overwrites read-only files.
/T Creates directory structure, but does not copy files. Does not
include empty directories or subdirectories. /T /E includes
empty directories and subdirectories.
/U Copies only files that already exist in destination.
/K Copies attributes. Normal Xcopy will reset read-only attributes.
/N Copies using the generated short names.
/O Copies file ownership and ACL information.
/X Copies file audit settings (implies /O).
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.
/B Copies the Symbolic Link itself versus the target of the link.
/J Copies using unbuffered I/O. Recommended for very large files.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

finally I found a way ,
xcopy $(TargetDir).$(ProjectDir)..\..\..\C1 /R /Y /E .
There is an * operators surrounding the dot(.) after the $(TargetDir) ,As its not visible when i post it , I'm mentioning it here .

Related

Batch program to copy file when there is a change in source folder

I need a batch program that will copy files from source folder to destination folder whenever there is a change(new files added or files modified) in source folder and restart a specific service.
Refer to the help of xcopy /? using the switch /D
/D : Copy files changed on or after the specified date.
If no date is given, copy only files whose
source date/time is newer than the destination time.
Syntax :
XCOPY source [destination] [options]
Remark :
So, in this batch file below, if you have any spaces in your directory names, you need to enclose them in double quotes.
#echo off
Set SourceDir="C\My Source\stuff"
Set TargetDir="D:\My Backup\stuff"
xcopy %SourceDir% %TargetDir% /i /d /y /e
/D : copy only files whose source date/time is newer than the destination time.
/E : To copy everything, including new directories, you should add the /e switch
/Y : Suppress prompt to confirm overwriting a file.
/I : If in doubt always assume the destination is a folder e.g. when the destination does not exist.

Windows copy command copies only 1 file

In a folder, I have 4 files, for example:
install.bat
program.exe
service.exe
uninstall.bat
I am interested to know why the following command copies all bat files to C:\Temp
copy *.bat C:\Temp
But this command only copies the first file, ignoring the rest?
copy *.bat+*.exe C:\Temp
It sees all the files because the output lists them, but only one is copied.
install.bat
program.exe
service.exe
uninstall.bat
1 file(s) copied.
Of course I can use two commands, one for each extensions, but why does it copy only one file, and how to specify multiple sources at once?
EDIT
The documentation of the command (i.e. copy /?) is the following:
Microsoft Windows [Version 10.0.19042.985]
(c) Microsoft Corporation. All rights reserved.
C:\Users\yanickrochon>copy /?
Copies one or more files to another location.
COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
[+ source [/A | /B] [+ ...]] [destination [/A | /B]]
source Specifies the file or files to be copied.
/A Indicates an ASCII text file.
/B Indicates a binary file.
/D Allow the destination file to be created decrypted
destination Specifies the directory and/or filename for the new file(s).
/V Verifies that new files are written correctly.
/N Uses short filename, if available, when copying a file with a
non-8dot3 name.
/Y Suppresses prompting to confirm you want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want to overwrite an
existing destination file.
/Z Copies networked files in restartable mode.
/L If the source is a symbolic link, copy the link to the target
instead of the actual file the source link points to.
The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line. Default is
to prompt on overwrites unless COPY command is being executed from
within a batch script.
To append files, specify a single file for destination, but multiple files
for source (using wildcards or file1+file2+file3 format).
This question is about the follow part:
COPY source + source destination
If the command accepts multiple sources, how does it work?
#for %e in (bat exe) do (#XCOPY "C:\source\*.%e" C:\tmp /C /S /I /F /H)
Just put this to cmd and you should be fine.

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

Windows cmd shell xcopy to network directory doesn't work

Im trying to make a batch file that will copy all new files and folders from a source folder to an network directory. All the new subdirectories and new files should be copied (backup).
My code:
xcopy "C:\Source" "T:\Backup" /d/i/s/q
(/d for only new files, /i because source is a dir, /s for all the subdirs and files, /q just to supress the copy text)
Source contains both subdirectories and files (.txt).
The first run it copies Everything as it should. When I add a new .txt file to one of the existing subdirectories and run it again I get the message:
"An error occured when the file The directory is not empty. was being created.
The folder "T:\Backup" could not be created.
0 files copied.
(Translated from Swedish so not 100% original)
The thing is when I try this command to a local source like e.g. "C:\test" and do the same procedure it works.
Anyone who can understand why this doesn't work for the network drive?
Should I try Another command such as robocopy?
Skip xcopy and use robocopy with the /E flag instead. It's built into all recent versions of Windows. Free download for XP.
Example:
robocopy c:\source T:\backup /E
That will copy all the files in the "source" folder to the "backup" folder that haven't been copied already.
And if you don't want to have the output shown on the console (equivalent to the /Q option in xcopy):
robocopy c:\source T:\backup /E /LOG:nul
Robocopy must be better because it should create directories with the \E switch. No overwrites for files, just adds a file with extra letters or extension <> command. Still must defrag.
XCOPY "DRIVE LETTER:\windows.old\USERS" "\computername\D\NAME\" /D /E /C /R /I /K /Y /f

converting xcopy to robocopy

Tried to use xcopy and excluding a folder and all its subfolders.
C:\Merged\org\a>xcopy /I /E /Y C:\Merged\org\*.* C:\Merged\dest /exclude:"C:\Mer
ged\org\a\*.*"
Can't read file: "C:\Merged\org\a\*.*"
I guess I cannot exclude folder, but only file with xcopy.
So I think of moving to robocopy, but I'm not sure which flags to use.
What is the equivalent robocopy to my above xcopy ?
Well, you can. Read up on XCOPY /?:
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
The problem is you specify the path you want to exclude to /EXCLUDE when you should specify the name of a file, which contains the "paths" to exclude (it is actually more flexible see above). The error message you get even hints at that.
Create a file, e.g. C:\ignore.txt, that contains the line "C:\Merged\orga\a\", then invoke XCOPY as follows:
xcopy.exe C:\Merged\org\*.* C:\Merged\dest /exclude:C:\ignore.txt /I /E /Y
If you want to use robocopy nevertheless, you can do so like:
robocopy.exe c:\merged\org c:\merged\dest /xd c:\merged\org\a /IS /E
I has been wrote seemless wrappers from xcopy command line to the robocopy. You can find it from here: https://github.com/andry81/contools.
The scripts:
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_dir.bat
https://github.com/andry81/contools/tree/HEAD/Scripts/Tools/std/xcopy_file.bat

Resources