converting xcopy to robocopy - windows

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

Related

XCOPY - How to exclude a certain file name?

This is my current xcopy command:
xcopy C:\SourceCodeOld\Release\"Source Code"\*.vb C:\SourceCodeNew\"Source Code"\ /S /Y /R
In several subfolders, I have a file named "AssemblyInfo.vb". How can I exclude it from being copied?
There is an /EXCLUDE option of xcopy, which allows to specify (the path to) a text file that contains partial file paths/names one per line (note also the fixed quotation):
xcopy /S /Y /R /I /EXCLUDE:exclude.txt "C:\SourceCodeOld\Release\Source Code\*.vb" "C:\SourceCodeNew\Source Code"
with exclude.txt being in the current working directory and containing:
AssemblyInfo.vb
However, the implementation of the /EXCLUDE option is terrible, because actually all files are excluded whose absolute source paths contain any of the given strings at any position (in a case-insensitive manner). Moreover, you cannot even use wildcards in these strings. Furthermore, you cannot provide a quoted path to the exclusion text file to protect potential spaces or special characters. (Refer also to this related answer of mine.)
I strongly recommend to use the robocopy command, whose exclusion options are more mature:
robocopy "C:\SourceCodeOld\Release\Source Code" "C:\SourceCodeNew\Source Code" "*.vb" /S /XF "AssemblyInfo.vb"
This truly excludes only files whose names are AssemblyInfo.vb.
Create a file excludes.txt and add the files to exclude to it (each in a new line)
AssemblyInfo.vb
anotherfile.vb
Then run your xcopy command using the /EXCLUDE parameter pointing to the file that contains the files to exclude:
xcopy "C:\SourceCodeOld\Release\Source Code\*.vb" "C:\SourceCodeNew\Source Code\" /EXCLUDE:excludes.txt /S /Y /R
To see the various options available for the /EXCLUDE parameter, run xcopy /?

I want to copy a directory excluding one file

I have used the xcopy command with /EXCLUDE switch but for that i have to make another file that contains the list of files to be excluded.
Below is the xcopy command i am using...
xcopy /EXCLUDE:C:\AA\excludedfiles.txt C:\AA d:\Models\Broker\NB\MOTNB0056
/S /E
where excludedfiles.txt contains the name of file that i want to exclude.
C:\AA is source and d:\Models\Broker\NB\MOTNB0056 is destination.
However i don't want to make extra file(excludedfiles.txt) for it. Suggest a command that exclude a file by giving just its path.
XCOPY is deprecated. It has been replaced by ROBOCOPY.
Open a command prompt window an run robocopy /? for help on command ROBOCOPY. In comparison to help of XCOPY output on running xcopy /? it has the option /XF to exclude one or more files specified after this switch and it is even possible to use wildcards.
So the command you might use is:
%SystemRoot%\System32\robocopy.exe C:\AA D:\Models\Broker\NB\MOTNB0056 /E /XF C:\AA\FileToExclude.ext
Some additional notes:
/S means copying with subdirectories, but without empty directories.
/E means copying with subdirectories with including empty directories.
It does not make sense to specify both on the command line.
It is advisable to specify target directory with backslash at end. This makes it clear for ROBOCOPY as well as for XCOPY that the target string specifies a directory and not a file. This is important in case of just a single file is copied as you can read in answer on batch file asks for file or folder. Both commands create the directory tree to target directory if this is necessary on having target directory specified with \ at end.
See Microsoft's documentation on Windows Commands and SS64.com - A-Z index of the Windows CMD command line on searching for a command for a specific file operation from command line or batch file.

Xcopy certain file names only

I've never used the Xcopy feature before and I was wondering if it would be possible to use xcopy to copy only certain files within a directory tree.
For example, suppose I have the following documents:
\servername\generateddocuments\2014\20141231\GUID1.doc
\servername\generateddocuments\2014\20141231\GUID2.doc
\servername\generateddocuments\2015\20150101\GUID3.doc
\servername\generateddocuments\2015\20150101\GUID4.doc
Now, suppose I have a spreadsheet that tells me which .doc files I need to copy:
GUID1.doc
GUID3.doc
Is there a way to base the xcopy on the spreadsheet(or txt document) so I don't copy the files I don't need?
I don't think xcopy can read files to include from a file. But you can create a batch file that does this:
for /F "tokens=*" %%A in (documents.txt) do (
copy %%A x:\targetfolder\
)
Try typing HELP XCOPY at a command prompt and look at the /EXCLUDE parameter. The documentation isn't quite correct, you can put a list of files in a single file, one file name per line, and they will be excluded from the xcopy.

How to use a batch command to copy all folders to a destination location

I have the following line in a bat file:
xcopy script_temp\* \\CHU-Computer-Science\CHU\scripts\ /S /E /H
however, this will only copy files, how can i also have it copy the folder?
The options you used copied empty folders for me on Vista. I don't know if there are differences in versions of Windows.
The /S and /E are mutually exclusive. /E copies subfolders, including empty ones. /S copies subfolders but ignores empty ones.
I thought that the last option specified wins, so the command you used should work. You could try eliminating the /S option and see if that helps on your system.
Or possibly I don't understand your question?

Copy a file to all folders batch file?

I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
Give this a try:
for /r "D:\Software\destinationfolder" %i in (.) do #copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"
Of course, if it's to go into a batch file, double the '%'.
This was the first search I found on google for batch file copy file to all subfolders.
Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on xcopy.
This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.
cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
the for loop - See help here. Or type for /? in a command prompt.
/r - Loop through files (recurse subfolders)
/d - Loop through several folders
%%I - %%parameter: A replaceable parameter
xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
C:\temp\file.ext - The file you want to copy
"%%~fsI" - Expands %%I to a full pathname with short names only
/H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
/K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.
for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"
#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir
I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).

Resources