xcopy regex for excluding names - cmd

I have a folder named Account
I want to xcopy all its subfolders, but it two phases (i.e. two xcopy commands):
I want to splint this cmd:
xcopy /I /E /Y %env.working_directory%\Deployment\Account\Release*.*
\file-srv\Archive\Products\Web\AccountsServices\Account.Toolbar\Nightly\Accounts_3.10_Merged\%system.build.number%
Account\folder1 in first xcopy
Account\ --> all other in second xcopy
How can I write the second xcopy? (regex for excluding "folder1")

(They are simple wildcards rather than regular expressions)
Robocopy ships with recent versions of windows & can do everything that xcopy can, it supports /xd to exclude directories.

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 /?

xcopy returns error "Invalid number of parameters" when exclude parameter is set

Issuing:
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y
works as expected. However:
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:"Y:\...\exclude.txt"
returns error:
Invalid number of parameters
Which also occurs when path names (containing spaces) are not enclosed by quotation marks. This however, is not the case. Paths (edited for readability) all correspond correctly. Syntax (as per Product Documentation - Xcopy) is also correct. Concerning OS is Windows XP Professional x32 SP3.
Why is second cmd returning error and how is it to be solved? I am not looking for alternatives to xcopy (robocopy etc.).
XCOPY is an old command harking back to the days of DOS. It looks like the /EXCLUDE option was never updated to support long file names. Ugh :-(
If you remove the quotes, then the text after the space is interpreted as an additional parameter, and you get the "Invalid number of parameters" error. If you keep the quotes, then it treats the quotes as part of the path, and reports it cannot find the file.
I believe you have three possible solutions:
1) Use the short 8.3 folder names in your path.
Of course this cannot work if your volume has short names disabled.
2) Use the SUBST command to create a drive alias for your troublesome path.
subst Q: "Y:\path with spaces"
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:Q:exclude.txt
subst Q: /d
This could be a problem if you don't know a drive letter that is free.
3) (my favorite) Simply PUSHD do the troublesome path and run the command from there :-)
pushd "Y:\path with spaces"
xcopy X:\ "Y:\...\bin\9876543210\" /c /g /d /i /e /r /h /y /exclude:exclude.txt
popd
See https://sevenx7x.wordpress.com/2009/01/02/xcopy-with-exclude-option-shows-cant-read-file/ and http://forums.majorgeeks.com/showthread.php?t=54300 for more information.
/EXCLUDE:file switch will not exclude the file specified. As per xcopy command reference:
/exclude:FileName1[+[FileName2][+[FileName3](…)] Specifies a list of
files. At least one file must be specified.
Each file will contain search strings with each string on a separate line in the file. 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.
It took me some time to get this right as well (I had the same errors), but ultimately, this format worked for me. As with all things DOS, absolute precision is critical, so feel free to copy and paste the below.
xcopy /t /e "C:\Users\username\Your Folder" "C:\Users\user\Your Folder"

How to robocopy subfolders with content and files with a specific prefix

It is hard to believe but I seem to be not able to copy a folder with all its files (that begin with a certain character) and subfolders (beginning with the same character) to another folder in Windows 7. I used copy, xcopy and robocopy but all I do achieve is, that all files in the top level directory and all the subdirectories but without their content get copied. What am I doing wrong? I tried several ways, my last try was:
robocopy path\path\here x* path\path\there /E
I also tried
/COPYALL
/MIR
but with the same result.
Your robocopy syntax is incorrect. Is should be:
robocopy path\path\here path\path\there x* /E
ROBOCOPY path\path\here path\path\there \*.* /E

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

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?

Resources