views:

1003

answers:

2

Hello,

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

I have a script which asks me the file name

C:\\>getname.bat     
enter file name: c:\test\pack\a.txt   

now the script should return just the parent name of the file.

pack           

and NOT the entire parent path to the file.

c:\test\pack   

Please help

Thank you

Nirjhar

+1  A: 

see this question

@echo OFF
set mydir="%~p1"
SET mydir=%mydir:\=;%

for /F "tokens=* delims=;" %%i IN (%mydir%) DO call :LAST_FOLDER %%i
goto :EOF

:LAST_FOLDER
if "%1"=="" (
    @echo %LAST%
    goto :EOF
)

set LAST=%1
SHIFT

goto :LAST_FOLDER
asdfg
Thank you .. U r genius :) .. works perfectly
iBoy
+1  A: 

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
ghostdog74