32

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

Ste Moore
  • 423
  • 2
  • 5
  • 7
  • Do you see any output? – LS_ᴅᴇᴠ Sep 27 '13 at 09: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 '13 at 09:49
  • Remove `EXIT` command. Check results again. – LS_ᴅᴇᴠ Sep 27 '13 at 09:51
  • I initially created it without the `EXIT` command and it did the same thing – Ste Moore Sep 27 '13 at 09:54
  • Makes no sense... Try putting some `ECHO Line 1` and `PAUSE` to check flow... – LS_ᴅᴇᴠ Sep 27 '13 at 09:55
  • 19
    I guess: The name of your file is `REG.bat`? – jeb Sep 27 '13 at 10:33
  • Yes, it's called REG.bat, but I can change that to whatever I want if that'll help. I tried putting in `ECHO Line 1` and `PAUSE` and I get `P:\Misc\Rename v2.0\Rename v2.0\bin\Debug>echo Line 1 Line 1 P:\Misc\Rename v2.0\Rename v2.0\bin\Debug>pause Press any key to continue . . .` Then I press return and get `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` and it loops around and starts again – Ste Moore Sep 27 '13 at 11:28
  • 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 '13 at 11:32
  • Also you shouldn't use `exit` in a batch file unless your intention is to exit the command processor. Exiting batch files is done with `exit /b` or `goto :eof`. – Joey Sep 27 '13 at 12:15

4 Answers4

86

In Windows, if you have a command line executable with the same name as your bat filename, and the batch file contains this command, the batch file keeps looping since Windows will execute that .bat file instead of the Windows command.

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.

Huseyin Yagli
  • 9,896
  • 6
  • 41
  • 50
  • 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. – jewbix.cube Apr 28 '17 at 00:23
  • 1
    Just tried this in Windows 7 and Windows 8 and the looping issue is there. – Huseyin Yagli Jul 21 '17 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 '18 at 23:32
  • Who would guess that! :-D – Oak_3260548 Dec 02 '21 at 09: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)

canoodle
  • 506
  • 5
  • 10
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.
Ashish Mishra
  • 704
  • 1
  • 6
  • 20
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

Devabc
  • 4,782
  • 5
  • 27
  • 40