Parse path and return 2nd subdirectory off the root

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Parse path and return 2nd subdirectory off the root

#1 Post by SIMMS7400 » 16 Sep 2016 01:52

Good Morning -

What is the simplest way to get a path and return the 2nd subdirectory name?

For instance, my path would be as follows:

Code: Select all

C:\Automation_Batch\APP_1\Hyperion_Batch\Scripts\NTScripts


I essentially need to grab APP_1

I never know what the name of that directory will be, therefore the need to make it dynamic. It will ALWAYS be 2 down from the ROOT. The script performing this action will always be in the NTScripts folder in the same directory.

Thanks!

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Parse path and return 2nd subdirectory off the root

#2 Post by penpen » 16 Sep 2016 04:29

This may help you:

Code: Select all

@echo off
for %%a in (%~f0\..\..\..\..\.) do echo %%~nxa


penpen

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Parse path and return 2nd subdirectory off the root

#3 Post by Squashman » 16 Sep 2016 06:27

Or put the path into a FOR /F command use the backslash at the delimiter in which case you would want the third token.

SIMMS7400
Posts: 539
Joined: 07 Jan 2016 07:47

Re: Parse path and return 2nd subdirectory off the root

#4 Post by SIMMS7400 » 16 Sep 2016 08:23

Works like a charm! Thank you both!

Post Reply