Robocopy /if (Include Files) syntax does not follow /xf syntax - cmd

I am trying to use the /if (Include Files) flag for robocopy to provide an array list of all the files I would like to copy.
I successfully did this when setting the source directory just 1 level up (e.g. C:\Documents\Folder), and providing just the filename in the array after /if (e.g /if file1).
However, when I try to set the source directory 2 levels up (e.g. C:\Documents) and pass the entire filepath into /if (e.g if/ C:\Documents\Folder\file1 [no extension on these files]), the robocopy will not recognise the entire filepath as a valid parameter. I tried with just one filepath instead of an array, and multiple ways of inputting this such as with double quotes around the filepath, but still errored with invalid parameter.
To test, I tried to do the opposite with /xf, and the robocopy then excluded this file successfully.
Is this a limitation of the /if flag for robocopy?
robocopy C:\Documents D:\Documents /e /tee etc... /if "C:\Documents\Folder\file1"
Thanks.

The /if option is for creating robocopy jobs. It supports only filters, not paths. So you could do:
robocopy C:\Documents D:\Documents /e /if *.pdf /save:myjob
The robocopy syntax allows specifying file names to copy, but not paths:
robocopy C:\Documents\Folder D:\Documents\Folder file1 file2
(Note: The filenames specified here and with the /if option are combined.)
If your files all reside in the same folder, you can just specify all their names. If they are in different folders, you have to run the command for each folder.

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.

copy entire folder on postbuild to a specific folder

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 .

Alternative to multiple robocopy commands iterating the same directory

I have a list of file extensions I need to collect all the files of from a particular directory while maintaining attributes, timestamps etc. which has resulted in my usage of Robocopy. I'm hoping someone can suggest a more efficient method than my current solution?
At present I copy these files using the following command into an "unprocessed" directory:
robocopy %Directory1% "%Directory2%\unprocessed" /Z /E /copy:dat *.pst *.ost *.doc *.docx *.pdf *.docm *.xls *.xlsx *.ppt /log+:%Directory%.txt
The individual collected files from inside the "unprocessed" directory are then sorted to individual folders named after each file extension and created at the same level as the "unprocessed" directory, again using individual Robocopy commands:
robocopy "%directory2%\unprocessed" %directory2%\pst *.pst /Z /E
...
...
robocopy "%directory2%\unprocessed" %directory2%\ppt *ppt /Z /E
As you can appreciate, this results in unnecessary iterations of the "unprocessed" directory multiple times. I'm unable to copy files straight to the final resting places due to the nature of robocopy so I'm hoping someone can suggest a more suitable solution that will still allow file attributes to remain intact.
(I apologise in advance that this could turn into a discussion as opposed to someone being directly able to answer this)
Given the % signs in your examples, it looks like you're probably running this as part of a batch file. Either way, if you have the environment variables defined, you could use the following:
Command line:
for %e in (pst ost doc docx pdf dcom xls xlsx ppt) do robocopy "%Directory1%" "%Directory2%\%e" /Z /E /copy:dat *.%e /log+:%Directory%.txt
Bat file:
for %%e in (pst ost doc docx pdf dcom xls xlsx ppt) do robocopy "%Directory1%" "%Directory2%\%%e" /Z /E /copy:dat *.%%e /log+:%Directory%.txt
One other thing to note: You'll probably end up with all docx files in both doc and docx, likewise xlsx would end up in both xls and xlsx. Windows always picks up longer extensions with 3 character extensions.

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