1

Okay, I want a batch file to detect whether if it can find the file it is inhabiting and not anything else.

For Example: I receive the path, (Down there) from the variable %~dp1.

C:\Users\%USERNAME%\Desktop\File\file1.bat

But what I want to do is receive this section of its directory, "\File\", and check whether the batch file can find out if that directory really exists.

12
  • I am not sure what you mean by the file is is inhabiting. If you want to know where the batch file is then use %0 Commented Jan 6, 2016 at 3:58
  • for %%I in ("%~0\..") do @echo %%~nxI Commented Jan 6, 2016 at 3:59
  • +Squashman Sorry, I know the directory of the batch file, I want the batch file to recognize the name of the file it is in. If the batch file were in File\random.bat, I would like the batch program to find File. Commented Jan 6, 2016 at 4:00
  • @DavidRios, you want to know the name of the batch file or the parent folder? Commented Jan 6, 2016 at 4:03
  • @Squashman I want to know the relative directory it is in, sorry. Thanks :) Commented Jan 6, 2016 at 4:05

4 Answers 4

2
for %%a in ("%~dp0\..") do SET "parent=%%~nxa" ECHO(%parent% 

since we are aware that we're running from a directory that probably has a parent, this sets parent to nothing if the parent is the root or the batch is at the root.

Sign up to request clarification or add additional context in comments.

Comments

0

Here is a solution, did not code solution for file being in root.

Variable f is what I believe you are asking for.

foo.cmd

@echo off setlocal ::: p parent ::: g grandparent ::: a grandparent absolute ::: f folder of this batch file set p=%~dp0 set g=%~dp0\.. FOR /F "delims=" %%t IN ("%g%") DO SET "a=%%~ft" call set f=%%p:%a%=%% call set f=%%f:\=%% echo p is: %p% echo g is: %g% echo a is: %a% echo f is: %f% endlocal 

Test Cases

L:\test>foo.cmd p is: L:\test\ g is: L:\test\\.. a is: L:\ f is: test L:\test>md subdir L:\test>cd subdir L:\test\subdir>..\foo.cmd p is: L:\test\ g is: L:\test\\.. a is: L:\ f is: test L:\test\subdir>copy ..\foo.cmd 1 file(s) copied. L:\test\subdir>foo.cmd p is: L:\test\subdir\ g is: L:\test\subdir\\.. a is: L:\test f is: subdir L:\test\subdir>cd.. L:\test>cd.. L:\>copy test\foo.cmd 1 file(s) copied. L:\>foo.cmd p is: L:\ g is: L:\\.. a is: L:\ f is: \= 

1 Comment

Thank you, that is exactly what I wanted. :)
0

Getting the parent directory of the batch file. I know we covered this several times on SO but I can't find the question.

for %%a in ("%~dp0\.") do for %%b in ("%%~dpa\.") do echo %%~nxb 

Comments

0

This will return the dir name (without path) of the running batch file:

@echo off for %%a in ("%~dp0\.") do echo %%~nxa 

Magoo has it slightly wrong: . not ..

An alternative if you don't use blanks in filenames/dirs is:

@echo off set p=%~p0 for %%x in (%p:\= %) do set dir=%%x echo %dir% 

3 Comments

We have a terminology problem. If the parent directory is required, then .. is correct. If the directory in which the batch is resident is required, the . is correct.
You can see from the question what he wants.
Thank you, Henrik and Magoo. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.