0

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.

rohit.dagur
  • 111
  • 2
  • 13

1 Answers1

3

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.

Mofi
  • 42,677
  • 15
  • 72
  • 128
  • But there is a issue with using ROBOCOPY as it gives exit code 1 when One or more files were copied successfully.Now what if it copies some files but skips one or more file accidently or due to any fault.Then also it will throw exit code 1. Is there a solution to fix it?....@Mofi – rohit.dagur Dec 22 '17 at 06:26
  • @rohit.dagur The link to SS64's [Robocopy](https://ss64.com/nt/robocopy.html) page in my answer contains a link to [Robocopy Exit Codes](https://ss64.com/nt/robocopy-exit.html) explaining in detail with which values __ROBOCOPY__ exits. If you want to test on any failure during copying in your batch file use as next command `if errorlevel 8 goto FileCopyError`. Please study also Microsoft support article [Testing for a Specific Error Level in Batch Files](https://support.microsoft.com/en-us/kb/69576) explaining why it is enough to use `if errorlevel 8` to jump to the label on any copy error. – Mofi Dec 22 '17 at 10:44