Manuel Ramos
Manuel Ramos

Reputation: 15

Get part of path for the absolute path of a file in batch script

I'm struggling with this problem, i'm new with batch scripting.

I have the absolute path of a file,

pathStr = "C:/a_folder/another_folder/com/project/files/my_file.properties"

And i need to know how can i extract the following part of that path.

subStr = "com/project/files/my_file" (without the extension of the file)

If this can help: the comfolder is always present in the absolute path. The only thing that varies are the names of the folders that are behind and in front of it

i don't know if this is possible, please can anybody give me a hint?

Upvotes: 1

Views: 56

Answers (1)

dbenham
dbenham

Reputation: 130919

@echo off
setlocal

:: Define your starting full path
set "fullPath=C:/a_folder/another_folder/com/project/files/my_file.properties"

:: Remove the extension (also converts / into \)
for %%F in ("%fullPath%") do set "newPath=%%~pnF"

:: Remove the path before com\
set "newPath=%newPath:*\com\=com\%"

:: Display the result
echo newPath="%newPath%"

:: If you want to restore forward slashes for some reason
set "newPath=%newPath:\=/%"

:: Display final result
echo newPath="%newPath%"

--OUTPUT--

newPath="com\project\files\my_file"
newPath="com/project/files/my_file"

Upvotes: 1

Related Questions