How to use xcopy to only copy files if they are newer? - visual-studio

I have many web applications in a Visual Studio solution.
All have the same post build command:
xcopy "$(TargetDir)*.dll" "D:\Project\bin" /i /d /y
It would be useful to avoid replacing newer files with old ones (e.g. someone could accidentally add a reference to an old version of a DLL).
How can I use xcopy to replace old files only with newer DLL files generated by Visual Studio?

From typing "help xcopy" at the command line:
/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.
So you already are using xcopy to only replace old files with new ones. If that's not happening, you may have to swap the positions of the /i and /d switches.

User following command to copy all new and modified file from source to destination
xcopy c:\source d:\destination /D /I /E /Y
/D to check any modified file are there
/I If the destination does not exist and copying more than one file, assumes that destination must be a directory.
/E To copy directory and sub directories
/Y to suppress any over write prompt.

Not tested, but I use a similar command script for these sorts of tasks.
set DIR_DEST=D:\Project\bin
pushd "$(TargetDir)"
::
:: Move Files matching pattern and delete them
::
for %%g in (*.dll) do (xcopy "%%g" "%DIR_DEST%" /D /Y & del "%%g")
::
:: Move Files matching pattern
::
:: for %%g in (*.dll) do (xcopy "%%g" "%DIR_DEST%" /D /Y )
popd

Related

Windows script to backup specific files from directory to another on same machine

I need a help with mine school project scipt. I thought it would be easy, but apparently found myself a bit confused with it.
The task is to:
Write a script, which gets as parameters two directories. First directory must exist. From the first directory and its subfolders the backup will be done for files such as .c,.txt,.jpg,.csv... and these files will be backed up to the second directory, which is nonexistent or is empty.
I figured out just the copying part...
#echo
if %username%==administrator goto useradmin
rem # files with C
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.c" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with TXT
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.txt" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with JPG
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.jpg" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
rem # files with CSV
XCOPY "%USERPROFILE%\Documents\iT universe city\Source Folder\*.csv" "%USERPROFILE%\Desktop\jpg\" /D /I /S /Y
You aren't usually provided homework/tasks for which you have not previously been provided sufficient information. When you are the intention is usually that you actually put in some time and effort researching.
For that reason I will only provide this. You can put in your own time and effort to look up the commands and work out how it works:
#Echo Off
Set/P "SrcDir=Enter Source Folder: "
If Not Exist "%SrcDir%\" Exit/B
Set/P "DstDir=Enter Destination Folder: "
ROBOCOPY "%SrcDir%" "%DstDir%" *.c *.txt *.jpg *.csv /S

Copy all files in a folder (including all sub-folders recursively) into another folder

set /P source=C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Config
set /P destination=C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Hi
set xcopy=xcopy /S/E/V/Q/F/H/I/N
%xcopy% %source% %destination%
this does not work.
Can anyone tell me what's wrong ?
Update:
The following code works but it creates the entire directory structure inside the destination. I only want the files to be copied.
xcopy /s /e /y "C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Config" "C:\Users\akshjosh\Documents\PROJECTS\119657_119578\119657\Hi"
pause
You need to remove the /p following the set commands. Also for the xcopy command you included switches that contradict themselves, ex. /s and /e. Look up the documentation on them.
You also need to change the variable name of xcopy to something that isn't already an inbuilt command.
set source=//FileLocation//
set destination=//FileDestination//
xcopy %source% %destination% /E /V /F /H /N
This should copy the files over with the directory structure. If you are still looking for only the files then I would recommend using a for loop to loop over all the files and transfer them that way. See here How to copy only files(not directories) using batch file?

Copying a set of specific files from a directory to another directory while retaining folder structure

I have a directory which looks like this:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\scannedDocs\2004\2009\jan\file2.pdf
\isa\documents\2005\2008\jac\file1.txt
\isa\documents\2003\jan\file1.txt
\isa\scannedDocs\2002\2009\jan\file2.pdf
I have a list of files I need copied (exported from a database), but because I don't need EVERY file in the directory, I only want the ones copied from my list:
Files-needed.txt:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\documents\2004\jac\file3.txt
\isa\documents\2003\jan\file1.txt
Basically:
I'll need to copy out the specific files needed, and
Log when a file can't be found, and,
I need folder structure retained, as the files might have the same name, just in different directories.
It's on a windows 7 machine, and I can run PowerShell and batch files. I tried robocopy and xcopy and either got all of the directories and no files or all files and no directories...
Any assistance would be great.
EDITS:
Okay, so i have tried Robocopy, but it is either copying the directories and no files or files and no directories. I haven't tried anything in powershell yet, but that might be the way...
It is Windows, I might have just written the slashes incorrectly above, I work between both environments, and was just trying to explain the problem.
Things I tried:
#echo off
set src_folder="C:\batchScripting\TEST_DIR\"
set dst_folder="C:\batchScripting\COPY2_DIR\"
robocopy "C:\batchScripting\TEST_DIR" "C:\batchScripting\COPY2_DIR" FileList.txt /S /V /NP /LOG:"log.log" /R:10 /W:30
and
#echo off
set src_folder=C:\batchScripting\TEST_DIR\
set dst_folder=C:\batchScripting\COPY2_DIR\
for /f "tokens=*" %%i in (File-list.txt) DO xcopy /s /i "%src_folder%\%%i" "%dst_folder%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (q23221996.txt) DO (
IF EXIST "%sourcedir%%%a" (ECHO f|xcopy /y "%sourcedir%%%a" "%destdir%%%a" 2>NUL >nul
) ELSE (ECHO "%sourcedir%%%a" does NOT exist)
)
GOTO :EOF
I used a file named q23221996.txt containing your data for my testing. sourcedir and destdir are both set up to suit my system.
The /y on the xcopy command forces overwrite if the destination file already exists.

How do I write a batch script that copies one directory to another, replaces old files?

I'd like a batch script in Windows with which I can copy one directory to another. If this directory already exists, and then for each file that already exists in both with the same name and location, it should be overwritten, if it does not exists, it should just be added.
In the end it should be a batch script to which I can pass 2 arguments, source & destination.
In your batch file do this
set source=C:\Users\Habib\test
set destination=C:\Users\Habib\testdest\
xcopy %source% %destination% /y
If you want to copy the sub directories including empty directories then do:
xcopy %source% %destination% /E /y
If you only want to copy sub directories and not empty directories then use /s like:
xcopy %source% %destination% /s /y
It seems that the latest function for this in windows 7 is robocopy.
Usage example:
robocopy <source> <destination> /e /xf <file to exclude> <another file>
/e copies subdirectories including empty ones, /xf excludes certain files from being copied.
More options here: http://technet.microsoft.com/en-us/library/cc733145(v=ws.10).aspx
Have you considered using the "xcopy" command?
The xcopy command will do all that for you.
Try this:
xcopy %1 %2 /y /e
The %1 and %2 are the source and destination arguments you pass to the batch file. i.e. C:\MyBatchFile.bat C:\CopyMe D:\ToHere
Just use xcopy /y source destination

How can I recursively copy files of a specific pattern into a single flat folder on Windows?

I need to copy a set of DLL and PDB files from a set of folders recursively into another folder. I don't want to recreate the folder hierarchy in the target folder.
I want to use built in Windows tools, e.g. DOS commands.
mkdir targetDir
for /r %x in (*.dll, *pdb) do copy "%x" targetDir\
Use /Y at the end of the above command if you are copying multiple files and don't want to keep answering "Yes".
command XCOPY
example of copying folder recursively:
mkdir DestFolder
xcopy SrcFolder DestFolder /E
(as stated below in the comment following WIKI that command was made available since DOS 3.2)
Make sure that you have the quotes right if you have spaces in your path.
This is my postbuild event for my TFS build server (that is why there is "%%"). I needed all the test files to be copied over.
if not exist "$(TargetDir)..\SingleFolderOutput" mkdir -p "$(TargetDir)..\SingleFolderOutput"
for /r **%%x** in (*.dll, *.pdb, *.xml, *.xaml, *.exe, *.exe.config) do xcopy **"%%x"** "$(TargetDir)..\SingleFolderOutput" /Y
For gathering PBD files I end up with this:
cmd /q /c for /R "<your_source_folder>" %f in (*.pdb) do xcopy "%f" "<your_destination_folder>" /I /Y
Note: must be two percent chars, if you use for /r from cmd-file:
for /r %%x in (*.dll, *pdb) do copy "%%x" targetDir\
#echo off
if %1'==' goto usage
if %2'==' goto usage
if %3'==' goto usage
for /D %%F in (%1\*) do xcopy %%F\%2 %3 /D /Y
for /D %%F in (%1\*.) do call TreeCopy %%F %2 %3
goto end
:usage
#echo Usage: TreeCopy [Source folder] [Search pattern] [Destination folder]
#echo Example: TreeCopy C:\Project\UDI *.xsd C:\Project\UDI\SOA\Deploy\Metadata
:end
I'm not aware of any command line tools that do this directly, but you could create a batch script to loop through sub folders, and copy the files you need.
However you may end up with files with duplicate filenames if you place them all in the same folder.

Resources