作为管理员批处理文件命令运行错误.exe'不被识别为内部或外部命令 [英] Run as admin batch file command error .exe' is not recognized as an internal or external command
问题描述
以管理员身份运行时出错:
Error when running as Administrator :
BackGroundJob.exe"未被识别为内部或外部命令,可运行程序或批处理文件.
BackGroundJob.exe' is not recognized as an internal or external command, operable program or batch file.
exe和bat文件都在同一文件夹中.
Both the exe and the bat file is in same folder.
它在我以管理员身份运行时运行.但是要安装此.exe,我需要以admin身份从同一文件夹安装它.
It run when I run it without run as administrator. But to install this .exe I need to install it as admin from same folder.
ECHO OFF
CLS
:MENU
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 to select your task, or 3 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Install BackGroundJob
ECHO 2 - UnInstall BackGroundJob
ECHO 3 - EXIT
ECHO.
SET /P M=Type 1, 2, or 3 then press ENTER:
IF %M%==1 GOTO INSTALL
IF %M%==2 GOTO UNINSTALL
IF %M%==3 GOTO EOF
:INSTALL
CD %cd%
BackGroundJob.exe install
GOTO MENU
:UNINSTALL
CD %cd%
BackGroundJob.exe uninstall
GOTO MENU
推荐答案
给出的信息很多,因此您可以尝试一下.
Not many information given, so here's a shot you may try.
backgroundJob.exe
似乎不在:
-
path
环境变量中的任何文件夹 - 批处理脚本所在的文件夹
- Any folders in the
path
environment variable - The folder that the batch script is in
使用外部工具时,始终理想的是检查该工具是否存在并且可以访问.
When using external tools, it is always ideal to check if the tool exists and is accessible.
-
使用
IF EXIST filename.ext
- 这可以检查文件是否存在,从而进一步防止此类错误.
或者,使用for
和where
知道文件在哪里
Alternatively, use for
and where
to know where the file is
- 浏览目录并检索文件路径.
CD %cd%
,因为它只是将目录更改为当前目录.仅在%cd%
之前被编辑时才使用此代码行,不建议这样做.
CD %cd%
is not required since it just changes directory to the current directory. This line of code only comes into use when %cd%
has be edited before, which is not recommended.
用户eryksun提到过,也许您当前的目录未设置为批处理文件的目录.考虑添加cd /d %~dp0
. cd
更改目录,/d
启用驱动器更改,并且%~dp0
代表驱动器和当前批处理脚本的路径.
User eryksun has mentioned, maybe your current directory is not set to the batch file's directory. Consider adding cd /d %~dp0
. cd
changes directory, /d
enables drive-changing, and %~dp0
stands for the drive and path to the current batch script.
IF %M%==1
是非常不安全的,因为输入内容几乎可以是任何东西,从字母数字字符到特殊字符.如果%M%
等于一个空格,则会发生这种情况:
is very insecure, as the input can be almost anything, from alphanumeric characters to special characters. If %M%
equals to a space, this happens:
IF ==1
命令处理器不理解这一点.
Command processor doesn't understand this.
如果输入是
1==1 format D:\ &&
这将格式化D盘.
GOTO EOF
应该为GOTO :EOF
,因为:EOF
是预定义的标签,并且只能由GOTO :EOF
访问.否则,cmd将跳到未定义的EOF标签,从而导致错误.
GOTO EOF
should be GOTO :EOF
, as :EOF
is a pre-defined label, and can only be accessed by GOTO :EOF
. Otherwise, cmd will jump to an undefined EOF label, causing an error.
虽然GOTO :EOF
可行,但我建议使用内部的exit
命令.
While GOTO :EOF
works, I'd recommend to use the internal exit
command.
EXIT
退出当前命令控制台.如果要退出子例程或其他脚本而不关闭控制台,则应尝试使用exit /b
.
EXIT
exits the current command console. If you want to exit a subroutine or an additional script without closing the console, you should try exit /b
instead.
您可以考虑使用@echo off
而不是echo off
,因为符号代表内联@echo off
.
You may consider to use @echo off
instead of echo off
, as the at-sign acts as the inline @echo off
.
这篇关于作为管理员批处理文件命令运行错误.exe'不被识别为内部或外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!