31

I have written a batch file from a VB.NET program I'm creating.

When I double click on the file in Windows XP it brings up a Command Prompt and appears to be running over and over again.

My batch file is as follows

REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ  /d "Open With Rename" /f
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename\Command" /ve /t REG_SZ  /d "P:\Misc\Rename v2.0\Rename v2.0\bin\Debug\Rename v2.0.exe ""%1""" /f
EXIT

I can't understan what I've done wrong, but if I open a command prompt and run it from there, it runs once.

Any help would be gratly appreciated!

Thanks

9
  • Do you see any output? Sep 27, 2013 at 9:36
  • Yes it just keeps repeating the following until I press Ctrl+C P:\Misc\Rename v2.0\Rename v2.0\bin\Debug>REG ADD "HKCU\Software\Classes*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f it doesn't add the registry key either
    – Ste Moore
    Sep 27, 2013 at 9:49
  • Remove EXIT command. Check results again. Sep 27, 2013 at 9:51
  • 19
    I guess: The name of your file is REG.bat?
    – jeb
    Sep 27, 2013 at 10:33
  • 8
    jeb answered your question. You are attempting to execute REG.EXE via your PATH variable, but instead your batch is executing itself from the current directory. So, yes, change the name of your batch script.
    – dbenham
    Sep 27, 2013 at 11:32

4 Answers 4

82

In windows, if you have a command line executable with the same name of your bat filename, and the batch file contains this command, the batch file keeps looping.

Example:

  • Create the file net.bat on your desktop.
  • In your file write this text: net

Double click the file and it will keep looping.

The cause of this behaviour is the order of execution of the commands. The command you want to execute is in one of the folders in your path. But the batch file is in your current folder so it gets executed first, causing the loop.

4
  • 2
    In Windows 7, this doesn't seem to be the case. Using the same script in Windows 10, I had the looping issue. This may be a Windows 10 feature. Apr 28, 2017 at 0:23
  • 1
    Just tried this in Windows 7 and Windows 8 and the looping issue is there. Jul 21, 2017 at 14:12
  • 2
    On Linux, this is considered a security risk. To run a file from the current directory, you have to specifically add ./ before the filename, or add the current directory . to the PATH. So why does Windows enable it by default?
    – wjandrea
    Jul 23, 2018 at 23:32
  • Who would guess that! :-D Dec 2, 2021 at 9:08
8

make sure:

  1. your script is not named like a build-in command or programm

  2. make sure the scripts your script calls are not named like a build-in command or programm

e.g. if your script is called: reeeeeboooot.bat that calls shutdown -t 10 -r, but in the SAME FOLDER resides a shutdown.cmd

reeeeeboooot.bat will actually call shutdown.cmd INSTEAD of the build-in command.

sometimes the easiest things are the hardest. (pretty often actually :-D)

4

In windows command line if you want to execute a command or a set of commands then we usually put those commands into a .bat file to execute them at any point of time but we must follow some guidelines before doing so.

  1. The file Name and the command name should not be same else it would loop forever.
  2. There should be no file in that directory having same name as the command name.
0

In Windows Terminal and DOS, to start a program, you only have to specify the filename without extension (such as .bat, .exe, .cmd, .com). Also, it is case-insensitive.

So if you create a batch file and execute REG, the system will first look in the current directory for something like reg.exe or reg.bat (or another executable with that name). Casing is ignored, so it will include REG.exe. If it doesn't find it, then it will look in the directories specified in the %PATH% environment variable.

In your case, you (assumingly) have an exetable named reg.bat in which you specify that it should call REG. So it will try to call itself, because it will first look in the current directory, in which it will find itself with that name.

The easiest fix is to use the full filename+extension instead. So you can simply change

REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f

to

REG.exe ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.