Get current folder name instead of current path in windows batch file



When writing a Windows batch file, it can be useful to know the current folder name of the batch process instead of the full path. This is important when trying to determine the current working directory, or when using the “%cd%” parameter to return the current directory.

A common way to get the current folder name from a batch file is by using the “%~nx0” parameter. This parameter returns the name and the extension of the batch file itself, which is the same as the folder name. For example, if a batch file is located at “C:\\MyFolder\\MyBatch.bat”, then “%~nx0” will return “MyBatch.bat”.

Another way to get the current folder name is to use the “%CD%” parameter. This parameter returns the full current working directory, which includes the folder name. To extract the folder name, the “for” command can be used. For example, the following command will extract the folder name from the full directory:

for /F \tokens=*\ %%a in (\%CD%\) do echo %%~nxa

The “%%~nxa” parameter will return only the folder name from the directory.

Finally, the “dir” command can also be used to get the current folder name. The “dir” command will return a list of the files and folders in the current directory. To extract the folder name, the “for” command can be used. For example, the following command will extract the folder name from the directory list:

for /F \tokens=*\ %%a in ('dir /B /AD') do echo %%a

The “%%a” parameter will return only the folder name from the directory list.

In summary, there are several ways to get the current folder name from a batch file. The “%~nx0” parameter can be used to get the name and extension of the batch file itself. The “%CD%” parameter can be used to get the full current working directory, and the “for” command can be used to extract the folder name. Finally, the “dir” command can also be used to get the current folder name, and the “for” command can be used to extract the folder name from the directory list.

Inquire Now

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Find your next full or part-time role here

ScaleDesk can either help you find your next full time gig or a part time side gig where you can create extra income!

Onboard with us once
Skip HR screening and go to the final interview with with only your resume and a video interview you never have to redo
Get paid electronically every month for the hours you work
We will be your reference even if you work for us once

IT Teams: Use ScaleDesk to augment your team

Schedule Demo