2

Background:

I like to have my computer clean. Especially I don't like programs and services starting with my windows, but I still want to use all my programs without having to manually start services and dependencies. Reasonable, right? Also I want to be able to start most of my programs via command line.

Environment

In my user directory I have a bin-folder containing symbolic links (.lnk files) pointing to the respective program (program.lnk points to a program called program). This bin-folder is included in my environment variable PATH.

If a program has a dependency e.g. a service, the link doesn't point the the program directly, but to a little BATch script located in a subfolder of the bin-folder. This script starts the nessesary services and then starts the program. Simplified a script looks a lot like this:

:restart
net start "Nessesary Service"
net start | find "Nessesary Service" > nul 2>&1
if %errorlevel%==0 goto start
goto restart
:start
start program.lnk
exit

The program.lnk is located in the same subfolder the scripts are in and now points to the actual program that is to be started. But it does have the same name as the initial link in the bin-folder.

The actual problem:

If I now open up Run (by hitting Windows + R) and type program (or program.lnk in cmd), it locates the program.lnk through my PATH variable in the bin-folder, follows the link into the subfolder and executes the script. The scripts starts the service and then as it comes to the "start program.lnk" line, the weird things start.

It seems to follow the PATH yet again as the same script is being executed again. But instead of opening up another instance of cmd with start script when it reaches the "program.lnk" line it now opens the link in the sub folder and thus, starting the program.

So all I really see is two cmd windows for a short time.

My question

My question is WHY, of course. Why does it open up the link by finding the link through my PATH variable instead of looking into the directory first and finding the link program.lnk? And why does it then not do the same thing for the second instance of the script? I checked via taskmanager the command line arguments of the CMD processes via the taskmanager and they're exacly the same. I'm unable to make head or tail of that, any help is appreciated :)

Edit

I did some more testing and as it turns out, the current directory of the first shell is not the bin-folder but system32. The second cmd starts with the right directory and therefore, starts the program. The weird thing is:

This only is the case when the initial shortcut is set to start with admin rights (properties -> shortcut -> Advanced), which it needs to start the service. I don't know why that is, but at least that solves it for me. I used this to make the script request admin right by itself rather than through the shortcut call.

1

Okay, it is really just starting a batch script with administrator privileges. And after identifying the real problem, you can easily find an answer. For example here. A simple

cd %~dp0

at the beginning does the trick.

Information about batch parametersInformation about batch parameters

| improve this answer | |

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.