Solved cut the later part of a string of a path

April 26, 2012 at 20:34:51
Specs: Windows 7
I have my file abc.txt located at path D:\user\desktop\source\system\etc\abc.txt and I want to cut/extract the part of the string after "source" in this path. i.e I want to extract the \system\etc\abc.txt from this and save into a variable say "leftover". is it possible using dos batch command?

See More: cut the later part of a string of a path

Report •

✔ Best Answer
May 2, 2012 at 15:02:42
Hmm. I see. How about:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET current=D:\user\desktop\source\system\etc\some\other\dirs\abc.txt
SET ascend_dir=..

:Find_Source
FOR /F "tokens=* delims=" %%A IN ("%current%\!ascend_dir!") DO (
    IF /I "%%~nxA" NEQ "Source" (
        SET ascend_dir=!ascend_dir!\..
        GOTO Find_Source
    )
    SET cutoff=%%~nxA
)

SET leftover=!current:*%cutoff%=!
ECHO Leftover = !leftover!

EXIT /B

When your only tool is a hammer, every problem looks like a nail.



#1
April 28, 2012 at 20:10:40
Are these strings based off of your directory structure?

How To Ask Questions The Smart Way


Report •

#2
April 29, 2012 at 04:28:43
Try this:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET current=D:\user\desktop\source\system\etc\abc.txt
SET greatgrandparent=..\..\..

FOR /F "tokens=* delims=" %%A IN ("%current%\%greatgrandparent%") DO (
    SET cutoff=%%~nxA
)

SET leftover=!current:*%cutoff%=!
ECHO Leftover = !leftover!

EXIT /B

When your only tool is a hammer, every problem looks like a nail.


Report •

#3
May 2, 2012 at 08:07:57
@orangeboy
Thanks a lot. But the tree under the folder "source" is not always \System\etc\abc.txt. it can be \system\bin\egl\xyz.txt also (4steps). this means that the %greatgrandparents% has variable children. had it been fixed then the script as above shall work fine.
need help...

Report •

Related Solutions

#4
May 2, 2012 at 15:02:42
✔ Best Answer
Hmm. I see. How about:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET current=D:\user\desktop\source\system\etc\some\other\dirs\abc.txt
SET ascend_dir=..

:Find_Source
FOR /F "tokens=* delims=" %%A IN ("%current%\!ascend_dir!") DO (
    IF /I "%%~nxA" NEQ "Source" (
        SET ascend_dir=!ascend_dir!\..
        GOTO Find_Source
    )
    SET cutoff=%%~nxA
)

SET leftover=!current:*%cutoff%=!
ECHO Leftover = !leftover!

EXIT /B

When your only tool is a hammer, every problem looks like a nail.


Report •

#5
May 3, 2012 at 12:35:48
You know, you never told me if this is based off of your directory structure or not.

Hey orangeboy, I'm just going to leave this here. If the OP ever responds to my question, you might find a use for it.

C:\>subst /?
Associates a path with a drive letter.

SUBST [drive1: [drive2:]path]
SUBST drive1: /D

  drive1:        Specifies a virtual drive to which you want to assign a path.
  [drive2:]path  Specifies a physical drive and path you want to assign to
                 a virtual drive.
  /D             Deletes a substituted (virtual) drive.

Type SUBST with no parameters to display a list of current virtual drives.

How To Ask Questions The Smart Way


Report •

#6
May 3, 2012 at 22:53:18
@orangeboy
Thanks a lot. It worked just great. May I ask you why are you using !ascend_dir!, !leftover! and not %ascend_dir%, %leftover%

@razor2.3
Pardon me. Since I am not a programmer I could not understand your query properly. Pardon me..


Report •

#7
May 4, 2012 at 04:47:36
The use of ! instead of % is just a habit of mine whenever I use ENABLEDELAYEDEXPANSION. I just tested and both leftover and ascend_dir work fine with %.

When your only tool is a hammer, every problem looks like a nail.


Report •

Ask Question