I need to find the name of the parent directory for a file in DOS
for ex.
Suppose this is the directory
C:\\test\\pack\\a.txt
The idea of DaDummy worked out in more practical reuseable functions. Also the first character of the _full_path is now included.
set map=D:\test1\test2\test3\test4.txt
call:get_parent_path "%map%"
echo full_path is %_full_path%
call:get_parent_path %_full_path%
echo full_path is %_full_path%
call:get_last_path %_full_path%
echo last_path is %_last_path%
goto :eof
:get_parent_path
set "_full_path=%~dp1"
:_strip
if not "%_full_path:~-1%"=="\" if not "%_full_path:~-1%"=="/" goto:_strip_end
set "_full_path=%_full_path:~0,-1%"
goto:_strip
:_strip_end
exit /b
:get_last_path
set "_last_path=%~nx1"
exit /b
::result:
::full_path is D:\test1\test2\test3
::full_path is D:\test1\test2
::last_path is test2
you can use a vbscript, eg save the below as getpath.vbs
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
WScript.Echo objFS.GetParentFolderName(strFile)
then on command line or in your batch, do this
C:\test>cscript //nologo getpath.vbs c:\test\pack\a.txt
c:\test\pack
If you want a batch method, you can look at for /?
.
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
call :set_dirname "%cd%"
echo %DIRNAME%
:set_dirname
set DIRNAME=%~n1
goto :eof