Suppose i have a .bat file saved in a folder . When my .bat file runs i need to get the path in which this .bat file is saved . I tried Google it but could not find the answer.
Thanks.
%~dp0 will give you teh path to the bat file.
If you also need the whole path including the name of the bat file itself use %~0.
Related
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""
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%
As the tile says. I need a .bat file to automatically delete 2 files on my C on a specific directory everytime I execute it.
I'm noob and don't know the commands :(
Try this :
del "C:\TEST\*.TXT"
For this example it will delete all files under TEST Folder with the .TXT extension so it depends on the extensions of the files , you can merely mention their names with their right extensions and it will work !
(Save the command in a .bat file and execute it as Administrator)
I want to be able to zip up a couple WLAN.xml files and a batch file.
Batch file will contain the commands to import those WLAN.xml's.
Can this be done directly from the zip file? Or is there a command to copy them from the zip file to a specific location (ie. c:)?
I currently have the WLAN.xml files in C:\ and my batch files specifies the location of it.
NOTE: To do this you would have to download the tool 72a.exe from 7zip, Although most computers nowadays already have it pre-installed
You can just use the following syntax:
7z a -tzip archive.zip -r src*.cpp src*.h
Apply your respective file path to the syntax above, and it should work.
Can anyone tell me how to change an elevated batch files directory to the location of the batch file? I am trying to make a program and it won’t work unless it is elevated and it has files it needs in the save folder. Can anyone help?
I think what you are referring to is when you 'Run as Administrator' the current directory changes and you can't reference folders/files unless you use the full path. To set the curent directory to the directory of the batch file add this line near the beginning of your bat file (before your reference any folders/files).
pushd "%~dp0"
We need to run a .msi file from batch file which is working fine if the path of .msi file is hard coded in batch file. Is there any way to get the path of .msi file dynamically as the batch file and .msi file exist at same folder location? It will really solve the purpose as thses needs to be copied to multiple servers...
%~dp0install.msi
%~dp0 gives you the path of your bat-file.
(note, that the last backslash is already included.
try #echo %~dp0 in your batch-file)
If the batch file and msi file are in the same folder then no path at all is needed. The batch file defaults to the current directory - and will write the log file to the current directory.
This is only an issue if the batch file is launched from a network drive.