获取批处理脚本中文件的绝对路径的路径的一部分

时间:2016-05-26 14:53:20

标签: batch-file

我正在努力解决这个问题,我是批处理脚本的新手。

我有文件的绝对路径,

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

我需要知道如何提取该路径的以下部分。

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

如果这有帮助:com文件夹始终存在于绝对路径中。唯一不同的是它后面和前面的文件夹的名称

我不知道这是否可行,请有人给我一个暗示吗?

1 个答案:

答案 0 :(得分:1)

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

- 输出 -

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