使用DOS批处理脚本的特定文件获取父目录名 [英] Get parent directory name for a particular file using DOS Batch scripting

查看:953
本文介绍了使用DOS批处理脚本的特定文件获取父目录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到父目录的名称为DOS文件

为前

假设这是目录

  C:\\测试\\包\\ A.TXT

我有一个脚本,问我的文件名

  C:\\\\> getname.bat
输入文件名:C:\\测试\\包\\ A.TXT

现在脚本应该只返回文件的父名称。

 

和不是整个父文件路径

  C:\\测试\\包


解决方案

看<一个href=\"http://stackoverflow.com/questions/280969/windows-batch-loop-over-folder-string-and-parse-out-last-folder-name\">this问题

 @echo OFF
设置MYDIR =%〜P1
SET MYDIR =%MYDIR:\\ =;%FOR / F令牌= * delims =; %%我(%MYDIR%)请拨打:LAST_FOLDER %%我
GOTO:EOF:LAST_FOLDER
如果%1==(
    @echo%,去年%
    GOTO:EOF
)最后设置=%1
转移后藤:LAST_FOLDER

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   

解决方案

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

这篇关于使用DOS批处理脚本的特定文件获取父目录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆