批处理文件 - 完整路径中的父文件夹

时间:2015-04-30 15:03:06

标签: batch-file

我正在尝试从包含完整路径\ filename(带空格)的变量中提取Parent_Folder名称。我已经看过一些类似的线索,但我似乎无法成功地适应我的方法。

@echo off
setlocal enabledelayedexpansion

set File1=filelist.txt
set File2=filelist2.txt

cd\users\mark\downloads\media

::Remove existing metadata files
for /f "delims=" %%f in (%File2%) do del "%%f"

::List current \Media files
dir /s/b *.mp4 *.mkv > %File1%

::Write metadata
for /f "tokens=*" %%a in (%File1%) do ( 
   :: filename, no extension
   set myFile=%%~na  

   :: full path, no filename
   set myPath=%%~dpa

   set myParent=????

   echo title : !myParent! > %%a.txt
   echo seriesTitle :  !myParent! >> %%a.txt
   echo seriesId :  !myParent! >> %%a.txt
   echo episodeTitle :  !myFile! >> %%a.txt
   echo description :  !myFile! >> %%a.txt
   echo isEpisode : true >> %%a.txt
   echo isEpisodic : true >> %%a.txt

   echo myPath :  !myPath! >> %%a.txt
)

::Relist the metadata files
dir /s/b *.m*.txt > %File2%

任何建议如何抓住" myParent"来自" myPath"

示例:" c:\ users \ mark \ this directory \ some filename.mkv"

应该返回:"此目录"

谢谢!

1 个答案:

答案 0 :(得分:4)

@echo off

set "myFile=c:\users\mark\this directory\some filename.mkv"

for %%a in ("%myFile%") do set "myPath=%%~Pa"
for %%a in ("%myPath:~0,-1%") do set "myParent=%%~Na"

echo %myParent%

在您的具体情况下,请使用:

::Write metadata

for /f "tokens=*" %%a in (%File1%) do ( 
   :: filename, no extension
   set myFile=%%~na  

   :: full path, no filename
   set myPath=%%~dpa

   :: my parent
   for %%b in ("!myPath:~0,-1!") do set "myParent=%%~Nb"