2015-04-30 113 views
1

我想從包含完整路徑\文件名(含空格)的變量中提取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:\用戶\標記這個目錄\一些filename.mkv \」

應該返回:「這目錄「

謝謝!

回答

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" 
+0

謝謝!但是,該代碼返回文件名(無擴展名)。我嘗試過的其他解決方案也提供了相同的意外結果。 –

+0

找到它 - 指向「b」,而不是「a」: for %% b in(「!myPath:〜0,-1!」)do set myParent = %%〜Nb「 –

+0

Ops!我有一個錯誤!最後一行必須有** b **而不是** a **:'set「myParent = %%〜Nb」'。我在上面的代碼中修復了它。 – Aacini

相關問題