What could be the reason for batch file with just REG ADD command looping endless?

好久不见. 提交于 2020-05-15 07:42:42

问题


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

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?


回答1:


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

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.

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?

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.

One more note:

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.



来源:https://stackoverflow.com/questions/34307134/what-could-be-the-reason-for-batch-file-with-just-reg-add-command-looping-endles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!