Kiwijunglist
Kiwijunglist

Reputation: 75

.BAT - Splitting %string% into filename + filepath + file's folder + file's parent folder

I'm creating a .bat file that is creating customised tv recording icons for mediaportal tvserver and need your help. I start with the following string

SET "path_and_filename=D:\TEMP FOLDER\TV RECORDINGS\TV3\Campbell Live\Campbell Live - 2012-10-25 - S38536.ts"

I'm trying to create 4 seperate variables from %path_and_filename% containing

  1. The filename (without extension) eg. Campbell Live - 2012-10-25 - S38536

  2. The filepath eg. D:\TEMP FOLDER\TV RECORDINGS\TV3\Campbell Live\

  3. The file's parent folder eg. Campbell Live

  4. The file's parent parent folder eg. TV3

Thank You

Upvotes: 1

Views: 1039

Answers (1)

dbenham
dbenham

Reputation: 130819

Easily done using FOR variable modifiers and . for current folder and .. for parent folder. (type HELP FOR from command prompt to get info on modifiers at bottom of help)

@echo off
setlocal
SET "path_and_filename=D:\TEMP FOLDER\TV RECORDINGS\TV3\Campbell Live\Campbell Live - 2012-10-25 - S38536.ts"
for %%F in ("%path_and_filename%") do (
  set "fileName=%%~nF"
  set "filePath=%%~dpF"
)
for %%F in ("%filePath%.") do set "fileParentFolder=%%~nF"
for %%F in ("%filePath%..") do set "fileGrandparentFolder=%%~nF"
set file

Sample output:

fileGrandparentFolder=TV3
fileName=Campbell Live - 2012-10-25 - S38536
fileParentFolder=Campbell Live
filePath=D:\TEMP FOLDER\TV RECORDINGS\TV3\Campbell Live\

Upvotes: 3

Related Questions