获取文件夹名称

塞尔吉奥

我有一些字符串,它是一些路径C:\ A \ B \ C \ D \如何获取文件夹名称,说“ A”或“ B”或其他所需名称。我仅对“ D”有解决方案:

@echo off
setlocal enabledelayedexpansion
FOR /f %%i IN ("C:\A\B\C\D\") DO (
set parent=%%~dpi
for /F "tokens=*" %%f in ("!parent:~0,-1!") do echo %%~nf
)
动作

您的问题不清楚(“其他需要的文件夹”不是一个规范),因此我假设您要将所有文件夹都分成数组元素,因此可以通过其索引获取任何所需的文件夹。

@echo off
setlocal EnableDelayedExpansion

set "string=C:\A\B\C\D\"

rem Separate all folders in the string into "folder" array
set i=-1
for %%a in ("%string:\=" "%") do (
   if %%a neq "" (
      set /A i+=1
      set "folder[!i!]=%%~a"
   )
)

rem Show some folders
echo The first folder:  %folder[1]%
echo The second folder: %folder[2]%
echo The last folder:   !folder[%i%]!

有关批处理文件中数组的更多详细信息,请参阅此文章

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章