Get parent directory name for a particular file using DOS Batch scripting

后端 未结 9 1300
無奈伤痛
無奈伤痛 2020-12-08 21:33

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
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 22:30

    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
    

提交回复
热议问题