批处理脚本:如何获取没有完整路径的父目录名称?
我正在编写一个处理文件夹的脚本,并且其中始终有一个文件需要重命名。新名称应该是父目录名称。如何在批处理文件中获取它?目录的完整路径是已知的。
I'm working on a script that processes a folder and there is always one file in it I need to rename. The new name should be the parent directory name. How do I get this in a batch file? The full path to the dir is known.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
目前尚不清楚脚本如何熟悉相关路径,但以下示例至少应该让您了解如何继续:
此脚本从
CD
获取路径变量并仅将名称提取到DirName
。It is not very clear how the script is supposed to become acquainted with the path in question, but the following example should at least give you an idea of how to proceed:
This script gets the path from the
CD
variable and extracts the name only from it toDirName
.您可以使用
basename
命令:You can use
basename
command:您可以使用内置的
bash
技巧:说明:
#
表示“从开头删除模式”,#
表示“删除较长的 模式”可能的模式'*/
是使用内置bash
避免调用外部命令(即basename
)的 ,因此这可以优化您的脚本。然而该脚本的可移植性较差。You can use built-in
bash
tricks:Explanations:
#
means 'remove the pattern from the begining'#
means 'remove the longer possible pattern'*/
is the patternUsing built-in
bash
avoid to call an external command (i.e.basename
) therefore this optimises you script. However the script is less portable.