3

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?

Mofi
  • 46,139
  • 17
  • 80
  • 143
anonymous
  • 151
  • 1
  • 16
  • 3
    What is the batch-file that is running this called? Make sure you don't call it REG.bat or something like that because than it will see reg not as a command but as a call to itself – Dennis van Gils Dec 16 '15 at 10:47
  • 1
    Which Windows version you are using? Do you can query the key or does it fails too? `REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Server" /v DisplayName` – jeb Dec 16 '15 at 10:49
  • when I run the same line on command prompt its working but when I put this line in batch file its going infinite loop. I am using windows 7. The name of batch file is test.bat – anonymous Dec 16 '15 at 10:53

1 Answers1

4

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143