批处理文件-完整路径的父文件夹 [英] Batch File - Parent Folder from Full Path

查看:84
本文介绍了批处理文件-完整路径的父文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从包含完整路径\文件名(带空格)的变量中提取Parent_Folder名称.我见过一些类似的主题,但似乎无法成功适应我的方法.

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%

任何建议如何从"myPath"中获取"myParent"

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

示例:"c:\ users \ mark \此目录\ some filename.mkv"

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

应返回:此目录"

谢谢!

推荐答案

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

这篇关于批处理文件-完整路径的父文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆