Windows 10

Batch File To Get Current Directory

In this tutorial, we are going to see how to get the current directory. The batch file contains a series of DOS (Disk Operating System) instructions. It allows triggering the execution of commands found in this file.
 

Example 1: Batch File To Get Current Directory

The following example gets the path of the current directory where the batch file exists:

@echo off

echo %~dp0

Output:

C:\Users\StackHowTo\
  • echo %~dp0 will return the path to the batch location.
  • echo %~f0 will return the path to the batch with filename.
 

Example 2: Batch File To Get Current Directory using Variable

The following example store the path of the current directory where the batch file exists in a variable using the system read-only variable %CD%:

@echo off

set path=%cd%

echo %path%

Output:

C:\Users\StackHowTo\

Leave a Reply

Your email address will not be published. Required fields are marked *