windowsbatch-filecmdxcopy

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.).


Solution

  • 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.