拆分路径并在批处理脚本中取最后一个文件夹名称 [英] split path and take last folder name in batch script
本文介绍了拆分路径并在批处理脚本中取最后一个文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 拆分字符串(具有路径)并在变量中取最后一个文件夹名称.请帮忙.
I want to split the string (having a path) with and take last folder name in a variable. Please help.
例如
mypath=D:FOLDER1FOLDER2FOLDER3
e.g
mypath=D:FOLDER1FOLDER2FOLDER3
我想要一个变量中的 FOLDER3.
I want FOLDER3 in a variable.
我尝试使用下面的命令,如果最后一个字符不是 :
I tried with the command below which is working if the the last character is not :
for %f in (C:FOLDER1FOLDER2FOLDER3) do set myfolder=%~nxf
如果最后一个字符是
如果像这样使用变量,它也不起作用:对于 (%mypath%) 中的 %f 设置 myfolder=%~nxf
Also it is not working if variable is used like : for %f in (%mypath%) do set myfolder=%~nxf
推荐答案
@echo off
set MYDIR=C:FOLDER1FOLDER2FOLDER3
set MYDIR1=%MYDIR:~0,-1%
for %%f in (%MYDIR1%) do set myfolder=%%~nxf
echo %myfolder%
输出
FOLDER3
这篇关于拆分路径并在批处理脚本中取最后一个文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文