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!

2
  • 1
    How do you specify which part of the string supplied you require? Commented Sep 26, 2016 at 9:25
  • This is a possible duplicate stackoverflow.com/questions/1707058/… check it out! Commented Sep 26, 2016 at 9:26

2 Answers 2

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%
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.