0

I am trying to create a windows service of some .exe file by using .bat file. As far as I know I need to use the path of the .exe file i want to run as service. But in generally the path of .exe file can be different, so I cannot use static path in the .bat file. I can solve the part of my problem by placing .exe and .bat files in the same direction, but I need to somehow get the direction of the .bat file within itself and add the name of my .exe file. It is just an idea so is it possible to do it in .bat file ?

Thank you

P.S.

To run .exe as a windows service, I use .bat file with the following script

SC create MyService displayname= "MyService" binpath= "<path of exe>\NAME.exe" start= auto

SC failure MyService reset= 86400 actions= restart/1000/restart/1000/run/1000

sc failure MyService command= "\"<path of exe>\NAME.exe""
Henrik
  • 159
  • 1
  • 15
  • By starting an .exe that makes up a Windows Service, you're not starting it as a service (unless the executable, when invoked as an application, starts its associated service). Do you intend to invoke `net start $servicename`? – CodeCaster Dec 14 '17 at 08:28
  • 3
    Possible duplicate of [Get current batchfile directory](https://stackoverflow.com/questions/17063947/get-current-batchfile-directory) – CodeCaster Dec 14 '17 at 08:29
  • To run .exe as a windows service, I use .bat file with the following script [ SC create MyService displayname= "MyService" binpath= "\NAME.exe" start= auto SC failure MyService reset= 86400 actions= restart/1000/restart/1000/run/1000 sc failure MyService command= "\"\NAME.exe"" ] – Henrik Dec 14 '17 at 08:31
  • That's creating a service, not running it. Anyway see duplicate. – CodeCaster Dec 14 '17 at 08:32
  • Thanx for reply – Henrik Dec 14 '17 at 08:39

1 Answers1

2

Use %~dp0

This example will set the path of the batch file you are running the code from.

set filepath=%~dp0
echo %filepath:~0,-1%
Gerhard
  • 22,678
  • 7
  • 27
  • 43