I use this script to find out the current folder with its .bat file:
for /f %%i in ("%0") do set curpath=%%~dpi echo %curpath%
it doesn't work correctly, if the path contains spaces(D:\Scripts\All Scripts -> retrieves only D:\Scripts\, if I place in the folder, whose path doesn't have spaces it retrieves the full path). How can I fix it?
Go to the destination folder and click on the path (highlights in blue). type cmd. Command prompt opens with the path set to your current folder.
To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory.
In a Windows command prompt, chdir or cd will print the full path of the current working directory in the console.
The path command specifies the location where MS-DOS should look when it executes a command. For example, if you were to use the "format" command, the path must be specified, or you will receive the message "bad command or file name." See our path definition for a full explanation and examples of paths on computers.
2015-03-30: Edited - Missing information has been added
To retrieve the current directory you can use the dynamic %cd%
variable that holds the current active directory
set "curpath=%cd%"
This generates a value with a ending backslash for the root directory, and without a backslash for the rest of directories. You can force and ending backslash for any directory with
for %%a in ("%cd%\") do set "curpath=%%~fa"
Or you can use another dynamic variable: %__CD__%
that will return the current active directory with an ending backslash.
Also, remember the %cd%
variable can have a value directly assigned. In this case, the value returned will not be the current directory, but the assigned value. You can prevent this with a reference to the current directory
for %%a in (".\") do set "curpath=%%~fa"
Up to windows XP, the %__CD__%
variable has the same behaviour. It can be overwritten by the user, but at least from windows 7 (i can't test it on Vista), any change to the %__CD__%
is allowed but when the variable is read, the changed value is ignored and the correct current active directory is retrieved (note: the changed value is still visible using the set
command).
BUT all the previous codes will return the current active directory, not the directory where the batch file is stored.
set "curpath=%~dp0"
It will return the directory where the batch file is stored, with an ending backslash.
BUT this will fail if in the batch file the shift
command has been used
shift echo %~dp0
As the arguments to the batch file has been shifted, the %0
reference to the current batch file is lost.
To prevent this, you can retrieve the reference to the batch file before any shifting, or change the syntax to shift /1
to ensure the shift operation will start at the first argument, not affecting the reference to the batch file. If you can not use any of this options, you can retrieve the reference to the current batch file in a call to a subroutine
@echo off setlocal enableextensions rem Destroy batch file reference shift echo batch folder is "%~dp0" rem Call the subroutine to get the batch folder call :getBatchFolder batchFolder echo batch folder is "%batchFolder%" exit /b :getBatchFolder returnVar set "%~1=%~dp0" & exit /b
This approach can also be necessary if when invoked the batch file name is quoted and a full reference is not used (read here).
for /f "delims=" %%i in ("%0") do set "curpath=%%~dpi" echo "%curpath%"
or
echo "%cd%"
The double quotes are needed if the path contains any &
characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With