\dotnet\ 在此时是意外的。

huangapple go评论50阅读模式
英文:

Why this?: \dotnet\ was unexpected at this time

问题

在Windows 11上,这是我的批处理文件:

if "%orig_path%"=="" (
  set "orig_path=%path%"
) else (
  path %orig_path%
)

path %path%;D:\.jdks\corretto-1.8.0_322\bin
set "JAVA_HOME=D:\.jdks\corretto-1.8.0_322"

(我这样做是因为我经常需要在Java 8和11之间切换。)

在第一行,我收到这个错误消息:在此时间点上,\dotnet\ 是意外的。

这是我当前路径中的两个元素中的一个的引用:

...C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;...

我怀疑问题不是空格字符,因为在这些之前还有几个元素使用了空格字符,所以问题很可能是围绕 x86 的括号。

(我没有权限更改路径的这部分。)

我已经读到可以通过将 if 语句更改为以下内容来解决此问题:

if [%orig_path%]==[] ...

但这也不起作用。这会给我带来以下错误:

在此时间点上,C:\WINDOWS 是意外的。

我还读到应该在文件开头添加 SETLOCAL ENABLEDELAYEDEXPANSION,但那没有改变任何事情。

如何才能让这个工作?

英文:

On Windows 11, here's my batch file:

if "%orig_path%"=="" (
  set "orig_path=%path%"
) else (
  path %orig_path%
)

path %path%;D:\.jdks\corretto-1.8.0_322\bin
set "JAVA_HOME=D:\.jdks\corretto-1.8.0_322"

(I do this because I often need to switch between Java 8 and 11.)

On the very first line, I get this error message: \dotnet\ was unexpected at this time.

This is a reference to one of two elements in my current path:

...C:\Program Files\dotnet\;C:\Program Files (x86)\dotnet\;...

I doubt the problem is the space character, because several elements before these also use a space character, so the culprit is probably the parentheses around x86.

(I'm not authorized to change that part of my path.)

I have read that I can fix this by changing the if statement to this:

if [%orig_path%]==[] ...

but that doesn't work either. That gives me this error:

C:\WINDOWS was unexpected at this time.

And I've read that I should say SETLOCAL ENABLEDELAYEDEXPANSION at the start of the file, but that didn't change anything.

How can I get this to work?

答案1

得分: 1

以下是您要翻译的内容:

Easy-peasy.

Your code would be resolved to:

    if "Program Files (x86)\dotnet\"=="" (
      set "orig_path=%path%"
    ) else (
      path Program Files (x86)\dotnet\...
    )

and that ) closes the if; cmd doesnt know what to do with the \dotnet\... so complains.

To fix:

    if "%orig_path%"=="" (
      set "orig_path=%path%"
    ) else (
      path "%orig_path%"
    )

or my preference

    if defined orig_path (
      path "%orig_path%"
    ) else (
      set "orig_path=%path%"
    )
英文:

Easy-peasy.

Your code would be resolved to:

if "...Program Files (x86)\dotnet\..."=="" (
  set "orig_path=%path%"
) else (
  path ...Program Files (x86)\dotnet\...
)

and that ) closes the if; cmd doesnt know what to do with the \dotnet\... so complains.

To fix:

if "%orig_path%"=="" (
  set "orig_path=%path%"
) else (
  path "%orig_path%"
)

or my preference

if defined orig_path (
  path "%orig_path%"
) else (
  set "orig_path=%path%"
)

huangapple
  • 本文由 发表于 2023年3月7日 20:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定