“was unexpected at this time.”

2020-02-02 11:23发布

问题:

I'm running this command on a batch file:

for %I in (*.txt *.doc) do copy %I c:\test2

...and it keeps returning:

I was unexpected at this time.

What is the cause of this error?

回答1:

If you're running within a batch/cmd file, you need to double the % markers:

for %%i in (*.txt *.doc) do copy %%i c:\test2

The single % variant only works from the command line.



回答2:

If being run from a batch file, variables need to be denoted with two percent signs, like %%I, only from the command line you use one



回答3:

I ran across a case where I was getting this error from a file that was named *.cmd. The error arose when I tried to access the first argument to the batch command:

if %1 EQU ""

Once I put quotes around the symbol for the first argument, the warning message went away:

if "%1" EQU ""


回答4:

Not a direct answer to the question, but if you encounter this message in any program, batch command, etc. then it's most likely related to your PATH containing " characters.

For instance, in Atom editor I was getting the message in the settings view.

"\"GNU was unexpected at this time

This was due to a different program putting in my PATH the following entry

...;C:\"Program Files"\"GNU ARM Embedded;..."

Because of that, the antislash character is read as escaped by some programs, which causes issues because then it's not a pathname delimiter but a simple character.

The solution for me was to remove those " from the PATH, and everything worked fine.

...;C:\Program Files\GNU ARM Embedded;...

PS: I have a doubt whether or not this can impact the original program (GNU ARM Embedded in this case) that maybe does not support spaces in pathnames. If somebody with more insight can clarify in the comments, I will update my post.

Hope this helps