windows 使用 DOS 批处理脚本获取特定文件的父目录名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2396003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get parent directory name for a particular file using DOS Batch scripting
提问by
I need to find the name of the parent directory for a file in DOS
我需要在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
采纳答案by asdfg
回答by duncanm
First answer above does not work if parent directory name contains a space. The following works:
如果父目录名称包含空格,则上述第一个答案不起作用。以下工作:
@echo off
setlocal
set ParentDir=%~p1
set ParentDir=%ParentDir: =:%
set ParentDir=%ParentDir:\= %
call :getparentdir %ParentDir%
set ParentDir=%ParentDir::= %
echo ParentDir is %ParentDir%
goto :EOF
:getparentdir
if "%~1" EQU "" goto :EOF
Set ParentDir=%~1
shift
goto :getparentdir
Calling the above with parameter of "C:\Temp\Parent Dir With Space\myfile.txt" gives following:
使用 "C:\Temp\Parent Dir With Space\myfile.txt" 的参数调用上面的内容如下:
>GetParentDir "C:\Temp\Parent Dir With Space\myfile.txt"
ParentDir is Parent Dir With Space
The above works by replacing spaces with colons (these should not exist in Windows paths), then replacing directory delimiters with spaces to so individual directories are passed to getparentdir as separate arguments. Function getparentdir loops until it finds its last argument. Finally any colons in result are replaced by spaces.
上面的工作是用冒号替换空格(这些不应该存在于 Windows 路径中),然后用空格替换目录分隔符,以便将各个目录作为单独的参数传递给 getparentdir。函数 getparentdir 循环直到找到最后一个参数。最后,结果中的任何冒号都被空格替换。
回答by foxidrive
It can be very simple to get the parent folder of the batch file:
获取批处理文件的父文件夹可以非常简单:
@echo off
for %%a in ("%~dp0\.") do set "parent=%%~nxa"
echo %parent%
And for a parent of a file path as per the question:
根据问题,对于文件路径的父级:
@echo off
for %%a in ("c:\test\pack\a.txt") do for %%b in ("%%~dpa\.") do set "parent=%%~nxb"
echo %parent%
回答by GerH
The idea of DaDummy worked out in more practical reuseable functions. Also the first character of the _full_path is now included.
DaDummy 的想法在更实用的可重用功能中得到了体现。现在还包括 _full_path 的第一个字符。
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
回答by DaDummy
I found this combination of approaches from the answers of djangofan and paranoid to be both, simple and perfectly sufficient, when looking up my script's parent directory:
在查找我的脚本的父目录时,我发现 djangofan 和 paranoid 的答案中的这种方法组合既简单又完全足够:
set FULL_PATH=%~dp0
set FULL_PATH=%FULL_PATH:~1,-1%
for %%i in ("%FULL_PATH%") do set "PARENT_FOLDER=%%~ni"
echo %PARENT_FOLDER%
Since you want to work on user input instead, you have to do some minimal additional work, to handle legal variations like C:\foo\bar\a.txt vs. C:\foo\bar\a.txt or c:/foo/bar/a.txt. This might then work for you:
由于您想处理用户输入,因此您必须做一些最少的额外工作,以处理诸如 C:\foo\bar\a.txt 与 C:\foo\bar\a.txt 或 c:/ foo/bar/a.txt。这可能对你有用:
@setlocal
@echo off
call:GET_PARENT_FOLDER C:\foo\bar\a.txt
echo %PARENT_FOLDER%
call:GET_PARENT_FOLDER C:\foo\bar\a.txt
echo %PARENT_FOLDER%
call:GET_PARENT_FOLDER c:/foo/bar/a.txt
echo %PARENT_FOLDER%
pause
goto:EOF
:GET_PARENT_FOLDER
:: Strip the filename, so we get something like this: 'C:\foor\bar\'
set "_FULL_PATH=%~dp1"
:: Strips all dangling '\' and '/' in a loop, so the last folder name becomes accessible
:_STRIP
if not "%_FULL_PATH:~-1%"=="\" if not "%_FULL_PATH:~-1%"=="/" goto:_STRIP_END
set "_FULL_PATH=%_FULL_PATH:~1,-1%"
goto:_STRIP
:_STRIP_END
:: We need the context of a for-loop for the special path operators to be available
for %%i in ("%_FULL_PATH%") do set "PARENT_FOLDER=%%~ni"
goto:EOF
回答by ghostdog74
you can use a vbscript, eg save the below as getpath.vbs
您可以使用 vbscript,例如将以下内容另存为 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 /?
.
如果您想要批处理方法,可以查看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
回答by npocmaka
Here's a way that does not use CALL
and I believe is faster.Based on jeb's splitting function(might fail if the directory name contains !
) :
这是一种不使用的方法,CALL
我相信它更快。基于 jeb 的拆分功能(如果目录名称包含 ,则可能会失败!
):
@echo off
set "mydir=%~p1"
SET mydir=%mydir:~0,-1%
setlocal EnableDelayedExpansion
set LF=^
rem ** Two empty lines are required
for %%L in ("!LF!") DO (
set "dir_name=!mydir:\=%%L!"
)
for /f "delims=" %%P in (""!dir_name!"") do set "dn=%%~P"
echo %dn%
exit /b 0
回答by paranoid
Here is another solution:
这是另一个解决方案:
SET LAST=%CD%
SET PARENTNAME=NONE
cd /D C:\test\pack
FOR %%I in (%CD%) do SET PARENTNAME=%%~nI
cd /D %LAST%
ECHO %PARENTNAME%
%%~nI: '~n' extracts name from path stored in %%I variable
cd: '/D' parameter added to switch between disks also
%%~nI: '~n' 从存储在 %%I 变量中的路径中提取名称
cd: '/D' 参数也用于在磁盘之间切换
回答by I-Chun Lai
call :set_dirname "%cd%"
echo %DIRNAME%
:set_dirname
set DIRNAME=%~n1
goto :eof