Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

windows - "invalid path 0 files copied" Error while using xcopy command

Hi I have this little command to copy files in a batch, which will help because I do this specific copy multiple times a day. The problem occurs while using the xcopy command. Everything is in order, but I am receiving this error: "Invalid path 0 files copied". Here is the code:

C:WindowsSystem32xcopy  /Y "C:UsersRyanDesktopmmars_pub" "C:UsersRyanDesktopDropboxMMARSmmars_pub"

I'm using the full path to the xcopy executable because I was having trouble configuring the path environment variable to function properly. I would imagine that it shouldn't affect the result though. I read somewhere about the "Prevent MS-DOS-based programs from detecting Windows" checkbox that should fix the issue, but I just can't seem to find that. Any help appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Original answer

Remove the ending backslash from the source folder path

C:WindowsSystem32xcopy.exe  /Y "C:UsersRyanDesktopmmars_pub" "C:UsersRyanDesktopDropboxMMARSmmars_pub"

edited 2015/10/01

While the original question used a literal path, and the indicated solution will solve the problem, there is another option. For literal paths and in cases where the path is inside a variable and could (or not) end in a backslash, it is enough to ensure that the ending backslash (if present) is separated from the quote, including an ending dot.

xcopy /y "x:source." "x:arget"
xcopy /y "%myVariable%." "x:arget"

This ending dot will not interfere in files/folders names. If there is and ending backslash, the additional dot will simply refer to the same folder. If there is not ending backslash as in windows files and folders can not end their names with a dot, it will be discarded.

BUT if the output of the xcopy command will be processed, remember that this additional dot will be included in the paths shown.


note: The solutions are above the line. Keep reading if interested on why/where there is a problem.

Why xcopy "c:source" "d:arget" fails but xcopy "c:source" "d:arget" works?

Both commands seems to have valid path references, and ... YES! both are valid path references, but there are two elements that work together to make the command fail:

  • the folder reference is quoted (note: it should be quoted, it is a good habit to quote paths as you never know when they will contain spaces or special characters)
  • xcopy is not an internal command handled by cmd but an executable file

As xcopy is an external command, its arguments are not handled following the cmd parser command line logic. They are handled by the Microsoft C startup code.

This parser follows two sets of rules, official rules

  • Arguments are delimited by white space, which is either a space or a tab.

  • A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. Note that the caret (^) is not recognized as an escape character or delimiter.

  • A double quotation mark preceded by a backslash, ", is interpreted as a literal double quotation mark (").

  • Backslashes are interpreted literally, unless they immediately precede a double quotation mark.

  • If an even number of backslashes is followed by a double quotation mark, then one backslash () is placed in the argv array for every pair of backslashes (\), and the double quotation mark (") is interpreted as a string delimiter.

  • If an odd number of backslashes is followed by a double quotation mark, then one backslash () is placed in the argv array for every pair of backslashes (\) and the double quotation mark is interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (") to be placed in argv.

and undocumented/non official rules (How Command Line Parameters Are Parsed)

  • Outside a double quoted block a " starts a double quoted block.
  • Inside a double quoted block a " followed by a different character (not another ") ends the double quoted block.
  • Inside a double quoted block a " followed immediately by another " (i.e. "") causes a single " to be added to the output, and the double quoted block continues.

This parser sees the sequence " found at the end of the "first" argument as a escaped quote that does not end/closes the argument, it is seen as part or the argument. And the "starting" quote of the "second" argument is just ending the double quoted block BUT not ending the argument, remember that arguments are delimited by white space.

So while it seems that the command line arguments are

     v           v            v......argument delimiters
      v.........v v..........v ......quoted blocks
xcopy "x:souce" "x:arget"
       ^.......^   ^........^  ......argument data
       arg #1      arg #2

       arg #1 = x:source
       arg #2 = x:arget

the actual argument handled by xcopy is

     v                        v .....argument delimiters
      v......................v  .....quoted block
xcopy "x:souce" "x:arget"
       ^.....................^  .....argument data
      arg #1    

      arg #1 = x:source" x:arget"

When the ending backslash is removed or the additional dot included, the closing quote in the argument will not be escaped, it will close the quoted block and the space between arguments will be seen as a delimiter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...