0

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.

Endoro
  • 34,892
  • 8
  • 45
  • 61
Dimitrov
  • 1
  • 1
  • 1
  • possible duplicate of [How to split string without for loop in batch file](http://stackoverflow.com/questions/14620709/how-to-split-string-without-for-loop-in-batch-file) – jeb Jul 04 '13 at 14:53

2 Answers2

3

try this:

set "word=C:\Dir1\Dir2\Dir3\File.c"
set "word=%word:*C:\Dir1\=%"
echo %word%
Endoro
  • 34,892
  • 8
  • 45
  • 61
2
@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.

Magoo
  • 68,705
  • 7
  • 55
  • 76