简体   繁体   中英

Split a string with a word for delimiter in batch

I need to split a string in a batch file using a word for delimiter.

For example if I have an input like :

C:\Dir1\Dir2\Dir3\File.c

I'd like to split it considering the word "Dir2" so that I get in output :

Dir2\Dir3\File.c

Note that the number of parent/children directories of Dir2 cannot be known before processing.

I've tried with for /f but it does not work as it does not accept delimiters with several characters but only single characters.

try this:

set "word=C:\Dir1\Dir2\Dir3\File.c"
set "word=%word:*C:\Dir1\=%"
echo %word%
@ECHO OFF
SETLOCAL
SET string=C:\Dir1\Dir2\Dir3\File.c
SET divider=Dir2
CALL SET after=%%string:*%divider%=%%
CALL SET before=%%string:%divider%%after%=%%
ECHO before=+%before%+
ECHO divider=+%divider%+
ECHO after=+%after%+
GOTO :eof

Test output:

before=+C:\Dir1\+
divider=+Dir2+
after=+\Dir3\File.c+

+ was included simply to demonstrate that there are no stray spaces involved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM