Win10批处理脚本:\ Notepad ++这时出乎意料 [英] Win10 Batch script: \Notepad++ was unexpected at this time

查看:56
本文介绍了Win10批处理脚本:\ Notepad ++这时出乎意料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此批处理文件在标题中给出了错误:

This batch file gives the error in the title:

if "%PROCESSOR_ARCHITECTURE%"=="x86" (
    echo FOOBAR
) else (
    set HOME_EDIT=%SystemDrive%\Program Files (x86)\Notepad++
)

这是在Win10 Pro x64系统上(因此测试为假).

This is on a Win10 Pro x64 system (so the test is false).

奇怪的是,"set HOME_EDIT ..."行如果单独执行,则不会产生错误.这个批处理文件似乎在几周前就可以正常工作了(我知道这本书中最古老的投诉,但也许最近的Win10 Creator的更新引起了某些问题?).

Strangely the "set HOME_EDIT..." line, if executed by itself, does NOT generate the error. And this batch file seemed to work OK a few weeks ago (oldest complaint in the book, I know, but maybe the recent Win10 Creator's Update chagned something?).

这是整个输出:

U:\Users\Dave\data\PC setup\2017-03 PC Setup for Win10>test.bat
\Notepad++ was unexpected at this time.

U:\Users\Dave\data\PC setup\2017-03 PC Setup for Win10>    set HOME_EDIT=C:\Program Files (x86)\Notepad++

U:\Users\Dave\data\PC setup\2017-03 PC Setup for Win10>

推荐答案

在作业中加上引号当然可以解决问题,但与空格无关.问题是路径中的)会过早关闭ELSE块,除非该路径被引号(或转义).

Putting quotes around the assignment certainly solves the problem, but it has nothing to do with spaces. The problem is the ) in the path is closing the ELSE block prematurely unless the path is quoted (or escaped).

不带引号的ELSE块变为

Without quotes, the ELSE block becomes

) else (
    set HOME_EDIT=%SystemDrive%\Program Files (x86
)

然后 \ Notepad ++ 确实是意外的,从而导致语法错误.

And then the \Notepad++ is indeed unexpected, causing a syntax error.

我看到了三种消除语法错误的方法:

I see 3 ways to eliminate the syntax error:

1)消除括号,然后将SET命令与ELSE放在同一行

1) Eliminate the parentheses and put the SET command on the same line as ELSE

else set HOME_EDIT=%SystemDrive%\Program Files (x86)\Notepad++

2)在作业周围加上引号

2) Put quotes around the assignment

) else (
    set "HOME_EDIT=%SystemDrive%\Program Files (x86)\Notepad++"
)

3)转义右括号

) else (
    set HOME_EDIT=%SystemDrive%\Program Files (x86^)\Notepad++
)

如果要执行分配,我将为文件夹使用预定义的环境变量.

If I were to do the assignment, I would use the predefined environment variable for the folder.

) else (
    set "HOME_EDIT=%ProgramFiles(x86)%\Notepad++"
)

这篇关于Win10批处理脚本:\ Notepad ++这时出乎意料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆