0

I have a batch script with input say /home/home1/home2/home3/

I need to extract part of the file path say /home2/home3. How can I achieve this?

Thanks for the help!

Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
  • 1
    How do you specify which part of the string supplied you require? – Magoo Sep 26 '16 at 09:25
  • This is a possible duplicate http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file check it out! –  Sep 26 '16 at 09:26

2 Answers2

1

Not tested:

@echo off

set "p=/home/home1/home2/home3/"
set "p=%p:/=";"%"

setlocal ENABLEDELAYEDEXPANSION
for %%a in ("%p%") do (
    if "%%~a" neq "" set "butlast=!last!"
    if "%%~a" neq "" set "last=%%~a"

)

echo %butlast%/%last%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

for /f "tokens=1-4 delims=/" %%a in ('echo /home/home1/home2/home3/') do @echo /%%b/%%c

tokens tells for how many variables to spit out & delims tells it what to split on. %%a is the first token, it will count out a through d because it is told to generate 4.

you will likely have your path as a variable, you can just put it in place of the path in the example, but you may need to use delayed expansion

Rob Biggs
  • 21
  • 1