How to get windows batch's parent folder

Tobia picture Tobia · May 18, 2013 · Viewed 31.1k times · Source

I'm writing a batch file, I need to get the parent folder of this bat file. Is it possibile? NB I mean the parent folder of the batch file not of the current directory of the prompt calling that batch.

Thanks

Answer

Endoro picture Endoro · May 18, 2013

The parent folder of a Batch is in the Variable %~dp0 located. Example:

@echo off&setlocal
for %%i in ("%~dp0.") do set "folder=%%~fi"
echo %folder%