Search code examples
cmdxcopy

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.


Solution

  • XCOPY is deprecated. It has been replaced by ROBOCOPY.

    Open a command prompt window an run robocopy /? for help on command ROBOCOPY.
    The help of XCOPY is output on running xcopy /? in the command prompt window.

    The Windows command ROBOCOPY has the option /XF to exclude one or more files specified after this switch and it is even possible to use wildcards.

    The command to 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 much sense to specify both on the command line.

    It is advisable to specify the target directory with backslash at the end on using XCOPY. That makes it clear for XCOPY that the target string specifies a directory and not a file. XCOPY supports also copying a single file to a new destination with a new file name. The backslash at the end of the destination directory path on using XCOPY is important in case of just a single file is copied as it can be read in the answer on batch file asks for file or folder.

    It is not advisable to let any directory path end with a backslash on using ROBOCOPY, especially if the directory path must be enclosed in " because of containing a space or one of these characters &()[]{}^=;!'+,`~. ROBOCOPY interprets a backslash left to " or one more \ as escape character for " respectively \. That special command line arguments parsing behavior of ROBOCOPY is counterproductive for source / destination path on being enclosed in " and the directory paths end with the directory separator \. Directory paths on ROBOCOPY command line end best never with \, except a directory path references the root directory of a drive. The backslash at the end is required in this special use case on which just C:\, N:\, ... without " should be used on the ROBOCOPY command line.

    Both commands create the entire directory tree to target directory if this is necessary.

    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.