My Batch File keeps looping, but why? - batch-file

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

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.

make sure:
your script is not named like a build-in command or programm
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)

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.
The file Name and the command name should not be same else it would loop forever.
There should be no file in that directory having same name as the command name.

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

Related

Running all exe files with arguments in a specific path using a batch file

First time posting something here
So I have several exe files in a folder and I need to run them all with an argument (the argument "5050" is the same for all). I can't have the batch file in the same folder and also the names of the exe files may change.
That's why I'm looking for something like "start C:\path\*.exe 5050" (I wish it would be that simple)
Here's my current code
forfiles /p "C:\Users\Já\Desktop\mpfiles" /m "*.exe" /c "cmd /c #file 5050"
The problem is that it works in cmd but not as a batch file.
Any ideas?

How to run .EXE from .BAT inside the folder where .EXE is locating

To recreate my problem you need to understand that I have next files in 2 folders.
K:\Script.bat
K:\Project\PortChanger.exe
K:\Project\settings.xml
I want to launch PortChanger.exe using Script.bat that contains next line:
start "K:\Project\PortChanger.exe"
This script is actually executing Program.exe, but my program throws me exception since PortChanger.exe can't find Settings.xml.
How can I launch PortChanger.exe from "K:\Project\", not from "K:\"? Now it seems like .BAT taking .EXE code and just running it where .BAT is locating.
To make it even more clear:
You could use Start with its /D option:
Start "" /D "K:\Project" "K:\Project\PortChanger.exe"
Open a Command Prompt window and enter start /? to read its usage information.
I would suggest you rather use pushd and popd
#echo off
pushd "K:\Project"
start "" PortChanger.exe
popd
pushd will change to the directory, launch the executable from it, then popd will return to the previous directory stored.

Open file in program from batch file windows

I have a batch file to open a bunch of programs on demand (not start-up)
E.g.
cd "C:\Program Files (x86)\Microsoft Office\root\Office16"
start lync.exe
I want to open files inside another program automatically specifically AutoHotKey scripts but with the option to open general files e.g. something like
d:
cd "D:\.PortableApps\AutoHotkey_1.1.28.02\"
start notifier.ahk AutoHotkeyU64.exe
I have tried
START "" /D "D:\.PortableApps\AutoHotkey_1.1.28.02\notifier.ahk" "D:\.PortableApps\AutoHotkey_1.1.28.02\AutoHotkeyU64.exe"
I would recommend, first and foremost, that you open a Command Prompt window and enter start /? to read its usage information.
For your first example, you should just Start your executable linc.exe directly, you do not need to change your working directory to do so:
#Start "" "C:\Program Files (x86)\Microsoft Office\root\Office16\lync"
In the above example you'll note that as the first doublequoted string is expected to be a title, I have used an empty one to prevent your command being read as one. Also if you cannot be sure that .EXE is a value entry under %PATHEXT%, you'd include that .exe extension, i.e. #Start "" "C:\Program Files (x86)\Microsoft Office\root\Office16\lync.exe".
As a final note, as lync.exe is a Microsoft Office product, I'd fully expect that its path is entered in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\lync.exe. In that case, you should not need to enter its path at all and the following would be all that is needed:
#lync
For the second example, the first thing you should note from the output of start /? is that you should be starting the executable with its script as an argument, not starting the script with its executing file as the argument.
If your AHk command does not require that your current directory is its own, the following should suffice:
#Start D:\.PortableApps\AutoHotkey_1.1.28.02\AutoHotkeyU64 D:\.PortableApps\AutoHotkey_1.1.28.02\notifier.ahk
If there were spaces or other poison characters in any of those names or paths, then the safer doublequoted option, complete with executable extension, would be better:
#Start "" "D:\.PortableApps\AutoHotkey_1.1.28.02\AutoHotkeyU64.exe" "D:\.PortableApps\AutoHotkey_1.1.28.02\notifier.ahk"
If you need to change directory, for the AHk command to work properly, then you have two options along the lines of your provided tests:
#PushD D:\.PortableApps\AutoHotkey_1.1.28.02
#Start AutoHotkeyU64 notifier.ahk
#PopD
If you cannot be sure that .EXE is a value entry under %PATHEXT%, you'd include that .exe extension, i.e. #Start AutoHotkeyU64.exe notifier.ahk. Also if there were spaces in the first line directory path, then you would doublequote it, if that path was not on an NTFS file system.
To use the Start option directly, similar to the first example:
#Start /D D:\.PortableApps\AutoHotkey_1.1.28.02 D:\.PortableApps\AutoHotkey_1.1.28.02\AutoHotkeyU64 notifier.ahk
If there were spaces or other poison characters in any of those names or paths, then the safer doublequoted option, complete with executable extension, would be better:
#Start "" /D "D:\.PortableApps\AutoHotkey_1.1.28.02" "D:\.PortableApps\AutoHotkey_1.1.28.02\AutoHotkeyU64.exe" "notifier.ahk"
In all of the examples above I have prepended the commands with #. If your script has already turned echoing off, then you can omit all of those prepending characters.
When you run cd /? from cmd you will find this section in the help topic:
Use the /D switch to change current drive in addition to changing current
directory for a drive.
You are therefore required to use the /d switch when changing directories which are on a different drive letters.
#echo off
cd /d "D:\.PortableApps\AutoHotkey_1.1.28.02\"
start notifier.ahk AutoHotkeyU64.exe
Alternatively by using pushd
#echo off
pushd "D:\.PortableApps\AutoHotkey_1.1.28.02\"
start notifier.ahk AutoHotkeyU64.exe
popd
Or simply start (without the incorrectly implemented /D as in your example).
start "" "D:\.PortableApps\AutoHotkey_1.1.28.02\notifier.ahk" "D:\.PortableApps\AutoHotkey_1.1.28.02\AutoHotkeyU64.exe"
Alternatively you can use pushd
or start /b without creating a new window:
start /b "" "D:\.PortableApps\AutoHotkey_1.1.28.02\notifier.ahk" "D:\.PortableApps\AutoHotkey_1.1.28.02\AutoHotkeyU64.exe"
Just as a reminder, all of the relevant help for the above mentioned commands can be found by running the following from cmd.exe
cd /?
pushd /?
popd /?
start /?

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

Multiple commands from cmd.exe /k with & does not work

Apologies if this is answered somewhere, been searching for 30+ mins to no avail.
So I have a batch file, from within it I call:
%comspec% /K "CD ..\..\test\java_6_86 & "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java"
However that does not work.
What I am trying to do is:
(Within a batch file)
Open a new command prompt
Change the Current Directory to the directory where Test1.java lives
Then call the java compiler and have it compile Test1.java
Also, is it possible to tell command where to start, instead of having to do a CD as the fist command?
Thanks,
DoW
I do not know what your %comspec% is but suppose it is 'cmd' this should do the trick (the cmd /K has to be in front of the javac call)
cd ..\..\test\java_6_86 & cmd /K "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java
By the way, a two liner would be much more readable
cd ..\..\test\java_6_86
cmd /K "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java
You can of course use absolute pathes to specify your classpath and java Source file and skip the directory change.
To answer your second question, you could use pushd/popd. As name suggests, those allow to store current directory, then change to given path (pushd path) and then popd pops/restores the original current dir. It's useful if you need to temporarily change current directory to do some processing or you want to shield your code from unwanted directory changes (eg. if you call another batch). It's also handy with network paths as it auto-creates a drive letter for it. Help pushd will give you full info.

Resources