Batch - Resolve relative path - windows

My batch scripts are located in the following directory:
\\test\public\windows\scripts\32\test.bat
I'm trying to get the path of this folder: \test\public\windows\logs
I've tried using %~dp0 in order to get the path of the script, and i've tried:
SET REL_PATH=..\..\
The thing is that it's not showing me this folder.

SET REL_PATH=..\..\
path points to the path according to you, to verify where it is taking from, please make a
echo %CD%
it will tell you the path the system is looking at.
then set the relative path from %CD% to the path of your script.
Please comment if you get stuck after doing this.
For example:
echo %CD%
gives C:\windows\system32
and you want to execute batch file at c:\test\public\windows\scripts\32\test.bat
you will need to do
SET REL_PATH=%CD%\..\..\test\public\windows\scripts\32\
To move to this path do a:
cd /d %REL_PATH%
To solve UNC path do PUSHD and POPD:
#echo off
pushd \\test\public\windows\scripts\32\
REM do your work
call test.bat
popd
Reference: How to run batch file from network share without "UNC path are not supported" message?

Use the PUSHD command when working with network drives. This will assign an unused drive letter to the UNC path so you can navigate it like normal. Then you can use POPD to release it.
For example:
#ECHO OFF
PUSHD "\\test\public\windows\scripts\32"
REM This will be \\test\public\windows
DIR ".\..\..\"
POPD

Related

Any way to get folder of batch script if it is run from a different location?

I have put my batch script's folder in my PATH environment variable, so I can open up any command prompt and can run it from anywhere. The problem is I have links to other files as relative paths in the script, so I need to get the path of the actual script.
Typing ~dp0 only gives me the path of the location I am in in the command prompt when running the script, and the same for %cd%. I would like the location of the actual script. Is this possible?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
:: build a simple batch file in "%sourcedir%" which is NOT on the path
(
ECHO #ECHO OFF
ECHO SETLOCAL
ECHO ECHO %%~dp0
)>"%sourcedir%\q24777129s.bat"
:: Force "%sourcedir%" into the path
SET path=%path%;%sourcedir%
:: execute the batch built
q24777129s
GOTO :EOF
This little script should demonstrate that %~dp0 indeed shows the script file's location. In my case, it showed U:\sourcedir\ which is not on the (original) path.
If it doesn't work for you, then we'd need more information, explicitly specifying the procedure you are using - what you are typing, where the script is located and the exact names of the batches/executables involved.

Batch file: for parameters

I am having trouble understanding what does following windows batch file do, can somebody explain:
for /f %%i in ("%0") do set curpath=%%~dpi
cd /d %curpath%
/*Some other code...*/
cd /d %curpath%
%0 is the full path to the .bat file itself (if run from another directory) and ~dpi is a modifier to extract the drive and directory from a path omitting the file name, so this snippet sets the current drive & directory to the one in which the batch file lives.
I can't see the reason for using a FOR, %~dp0 does the same thing in one go.

Windows CMD script to get directory in which file exists

I have a executable file VSTO.exe & a try.bat file in one folder. I want to get the folder path of the bat file & concat that path with VSTO.exe.
I have this script in my try.bat.
"%~fp0\VSTO.exe" /q
but it creates path: "G:\test\try.bat\VSTO.exe" . I want to get a path "G:\test\VSTO.exe"
can anyone tell me how do i do that?
"%~dp0\VSTO.exe" /q
is the exact answer.
How to get folder path from file path with CMD
Try with this
SET CURDIR=%CD%
"%CURDIR%\VSTO.EXE" /q
The %CD% pseudo-variable expands to the current working directory.
Type SET /? to see other pseudo-variable (last page)

batch script to set a variable with the current path location

How can I set a variable with the current location? For instance, if I get in c:\test and want to set the variable to test and if I get inside c:\test\test2 the variable will be set to test2.
I'm thinking about using a for to get inside a lot of folders and check if some file exists, if the correct file exist I want to set the current folder to a variable so I can copy the path and copy the folder.
The main problem deals with copying the rest of the files is the same folder as the .inf file.
The current directory is in the "shadow" variable cd.
You could try
set "var=%cd%"
%~dp0
This expands into the drive & path of the currently running batch file. I usually surround my batch files with something like:
#echo off
pushd %~dp0
...
popd
Edit: It seems I didn't understand the OP. My example gets the location of the currently running script, not the "Current Directory". +1 to jeb.
I think there is a little confussion here. %CD% always have the current directory, so you don't need to add anything to have it. However, by rereading your original question, I think that you need the LAST PART of the current directory, that is, the name of the current location excluding all previous locations. If so, then you may use this:
set i=0
:nextdir
set /a i+=1
for /f "tokens=%i% delims=\" %%a in ("%CD%") do if not "%%a" == "" set lastdir=%%a& goto nextdir
echo Current location: %lastdir%

How to execute programs in the same directory as the windows batch file?

I have in the same folder a .bat and a .exe file.
I couldn't call the .exe file from the .bat unless I put the full absolute path to it.
Is there a way to don't specify the path?
Try calling the .exe with %~dp0, like this: %~dp0MyProgram.exe.
%0 contains the full path to the called .bat file.
~dp says to get the drive and path, including trailing \.
I solved this by changing the working directory using pushd at the start of the script and restoring is at the end of the script using popd. This way you can always assume the working directory is the same as the location of the bat file.
pushd %~dp0
ProgramInSameFolderAsBat.exe
popd
As Stephen C said, to properly support paths with spaces we can use:
start "%~dp0" "myfile.exe"
or with arguments:
start "%~dp0" "myfile.exe" -my_arguments

Resources