I want to split the string (having a path) with \
and take last folder name in a variable. Please help.
我想要将字符串(有路径)与\进行分割,并将最后的文件夹名放在一个变量中。请帮助。
e.g
mypath=D:\FOLDER1\FOLDER2\FOLDER3\
e。g mypath = D:\ FOLDER1 \ FOLDER2 \ FOLDER3 \
I want FOLDER3 in a variable.
我想要一个变量中的FOLDER3。
I tried with the command below which is working if the the last character is not \
:
我尝试了下面的命令,如果最后一个字符不是\:
for %f in (C:\FOLDER1\FOLDER2\FOLDER3) do set myfolder=%~nxf
It is not working if the last character is \
如果最后一个字符是\,它就不起作用。
Also it is not working if variable is used like : for %f in (%mypath%) do set myfolder=%~nxf
如果变量被使用,它也不会起作用:for %f in (%mypath%) do set myfolder=%~nxf。
2 个解决方案
#1
20
@echo off
set MYDIR=C:\FOLDER1\FOLDER2\FOLDER3\
set MYDIR1=%MYDIR:~0,-1%
for %%f in (%MYDIR1%) do set myfolder=%%~nxf
echo %myfolder%
outputs
输出
FOLDER3
#2
11
try:
试一试:
for %f in (C:\FOLDER1\FOLDER2\FOLDER3\.) do set myfolder=%~nxf
works also:
作品也:
for %f in (C:\FOLDER1\FOLDER2\FOLDER3.) do set myfolder=%~nxf
#1
20
@echo off
set MYDIR=C:\FOLDER1\FOLDER2\FOLDER3\
set MYDIR1=%MYDIR:~0,-1%
for %%f in (%MYDIR1%) do set myfolder=%%~nxf
echo %myfolder%
outputs
输出
FOLDER3
#2
11
try:
试一试:
for %f in (C:\FOLDER1\FOLDER2\FOLDER3\.) do set myfolder=%~nxf
works also:
作品也:
for %f in (C:\FOLDER1\FOLDER2\FOLDER3.) do set myfolder=%~nxf