1

I am trying to extract the Parent_Folder name from a variable containing a full path\filename (with spaces). I've seen a few similar threads, but I cannot seem to successfully adapt to my approach.

@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%

Any suggestions how to grab "myParent" from "myPath"

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

Should return: "this directory"

Thanks!

Mark Pelletier
  • 1,329
  • 2
  • 24
  • 42

1 Answers1

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%

In your specific case, use this:

::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"
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks! But, that code returns the Filename (no extension). Other solutions I have tried have provide the same unexpected result. – Mark Pelletier Apr 30 '15 at 16:25
  • Found it - point to "b", not "a": for %%b in ("!myPath:~0,-1!") do set "myParent=%%~Nb" – Mark Pelletier Apr 30 '15 at 16:29
  • Ops! I had a bug! The last line must have a **b** instead of an **a**: `set "myParent=%%~Nb"`. I fixed it in the code above. – Aacini Apr 30 '15 at 16:29