仅使用REG ADD命令循环无休的批处理文件的原因可能是什么? [英] What could be the reason for batch file with just REG ADD command looping endless?

查看:135
本文介绍了仅使用REG ADD命令循环无休的批处理文件的原因可能是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的批处理文件test.bat中有以下一行,该行向Windows注册表添加了一个条目:

I have below line in my batch file test.bat which adds an entry to Windows registry:

@echo off
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Server" /v DisplayName /t REG_SZ /d Server /f

当我从命令提示符窗口中运行同一行时,该命令正在工作.但是,当我将此行放入批处理文件test.bat并在Windows 7上运行该批处理文件时,会导致无限循环.

The command is working when I run the same line from within a command prompt window. But it results in an infinite loop when I put this line into batch file test.bat and run the batch file on Windows 7.

这种意外的批处理文件的原因可能是什么?

What could be the reason for this unexpected batch file processing?

推荐答案

建议在批处理文件中始终使用完整路径和文件扩展名来调用像reg这样的应用程序,而不是Windows命令处理器cmd.exe的内部命令而不只是文件名.

It is advisable to call applications like reg not being an internal command of Windows command processor cmd.exe in batch files always with full path and with file extension instead of just file name.

@echo off
%SystemRoot%\System32\reg.exe ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Server" /v DisplayName /t REG_SZ /d Server /f

仅使用REG取决于在当前命令进程的环境变量 PATHEXT 中定义了哪些可执行文件/脚本扩展名,以及在环境变量 PATH 中定义了哪些目录当前命令过程的强".在这种情况下,目录的顺序也很重要.

With using just REG it depends on which file extensions for executables/scripts are defined in environment variable PATHEXT of current command process and which directories are defined in environment variable PATH of current command process. The order of directories is in this case also important.

请参见在哪里开始"搜索可执行文件?,以获取有关搜索可执行文件的更多详细信息.但是请注意,尽管 start 是所有基于Windows NT的Windows的命令处理器cmd.exe的内部命令,但命令处理器本身仅使用 PATHEXT PATH ,并忽略Windows注册表中的应用路径键. Windows命令解释器的文件搜索行为在答案中得到了更详细的解释,这是因为无法识别"..."的原因是什么?内部或外部命令,可操作程序还是批处理文件?

See answer on Where is "START" searching for executables? for more details about search for executable. But please note that although start is for all Windows NT based Windows an internal command of command processor cmd.exe, command processor itself uses only PATHEXT and PATH and ignores App Paths keys in Windows registry. The file searching behavior of Windows command interpreter is explained even more detailed in answer on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?

无限循环很可能是由命名批处理文件reg.bat引起的,正如 Dennis van Gils>/a>.因此,命令处理器在搜索名称为REG的可执行文件时会在当前目录中找到已处理的文件reg.bat,并通过将其他字符串作为参数传递给reg.bat来继续对该批处理文件进行批处理.换句话说,批处理文件一次又一次地无休止地启动.

The infinite loop is most likely caused by naming the batch file reg.bat as supposed already by Dennis van Gils. So command processor finds in current directory on searching for an executable with name REG the file reg.bat which is already processed and continues batch processing on this batch file with passing the other strings as parameter to reg.bat. In other words the batch file starts itself again and again in an endless loop.

另外一个注意事项:

此处应使用控制台应用程序reg.exe向注册表配置单元 HKEY_LOCALE_MACHINE 中添加添加字符串值.整个计算机的写操作需要管理员权限.因此,分别使用该批处理文件为当前用户禁用用户帐户控制(UAC)或分别

The console application reg.exe should be used here to add a string value to registry hive HKEY_LOCALE_MACHINE. This write operation for entire machine requires administrator privileges. Therefore either user account control (UAC) is disabled for current user using this batch file or Run as Administrator respectively Runas is used on executing this batch file. Otherwise reg.exe fails to add the string value to registry because of missing permissions.

这篇关于仅使用REG ADD命令循环无休的批处理文件的原因可能是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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