Reputation: 13
SourceFilePath="D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp" How to strip folder names which is just before the file name
From the above example string, i want to get dll_feature,dllfunction,GV into different variables.
Upvotes: 0
Views: 174
Reputation: 4142
The one limitation that the MC ND's solution is that you must know the directory depth to get the parts at the end.
Here's an alternate strategy that employs recursion and subroutines. The "subroutine" :SPLIT
gets the nth item from the specified path (0 is the right-most) and saves it into the variable specified. The way it's written, if you specify a level that is greater than the number of directory parts, it saves an empty string to the variable. This could be changed easily if that was desirable.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "SourceFilePath=D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp"
FOR /L %%i IN (0,1,10) DO (
CALL :SPLIT "%SourceFilePath%" %%i TGT
@ECHO [%%i] !TGT!
)
EXIT /B
REM Syntax: CALL :SPLIT _path_ _level_
REM Get the _nth_ element of the path (0 is the right-most)
:SPLIT
SETLOCAL ENABLEDELAYEDEXPANSION
SET "PATH=%~1"
SET "LEVEL=%~2"
SET "TARGET_VAR=%~3"
REM Strip off a trailing dir sep (for the %%~ substitution).
IF "%PATH:~-1,1%"=="\" SET "PATH=!PATH:~0,-1!"
FOR %%p IN (%PATH%) DO (
IF /I "%PATH%"=="%%~dp" (
REM BASE CASE
IF "%LEVEL%"=="0" (
SET "_TARGET=%PATH%"
) ELSE (
SET "_TARGET="
)
) ELSE IF "%LEVEL%"=="0" (
REM Return the leaf
SET "_TARGET=%%~nxp"
) ELSE (
REM Recurse on the drive+directory
SET /A SUBLEVEL=LEVEL-1
CALL :SPLIT "%%~dpp" !SUBLEVEL! _TARGET
)
)
ENDLOCAL && SET "%TARGET_VAR%=%_TARGET%"
EXIT /B
The output of this script is:
[0] filename.cpp
[1] dll_feature
[2] dllfunction
[3] GV
[4] Beanch
[5] Site
[6] Code
[7] Projects
[8] D:
[9]
[10]
For the description you give, this invocation would give you something like you want:
CALL :SPLIT "%SourceFilePath%" 0 PART0
@ECHO %PART0%
CALL :SPLIT "%SourceFilePath%" 1 PART1
@ECHO %PART1%
CALL :SPLIT "%SourceFilePath%" 2 PART2
@ECHO %PART2%
Upvotes: 0
Reputation: 70943
@echo off
setlocal enableextensions disabledelayedexpansion
set "sourceFilePath=D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp"
set "count=10000"
for %%a in ("%sourceFilePath:\=" "%") do (
setlocal enabledelayedexpansion
for %%b in (!count:~-4!) do (
endlocal
set "e_%%b=%%a"
)
set /a "count+=1"
)
rem Show the variables
set e_
Separates each element in the variable replacing the backslash with a space (everything quoted to avoid problems) and for each element, a variable is defined
Numbers in variable names are padded. If not needed just change the initial count
value to 0
Upvotes: 1
Reputation: 80138
@ECHO Off
SETLOCAL
SET "SourceFilePath=D:\Projects\Code\Site\Beanch\GV\dllfunction\dll_feature\filename.cpp"
SET /a count=0
:loop
FOR %%a IN ("%sourcefilepath%") DO SET "part%count%=%%~nxa"&SET "sourcefilepath=%%~dpa"
IF DEFINED part%count% SET /a count +=1&SET "sourcefilepath=%sourcefilepath:~,-1%"&GOTO loop
SET part
SET count
GOTO :EOF
Provided you don'y have weirdo characters in the starting name...
Upvotes: 1