Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FOR /F and delims=\ string processing

912 views
Skip to first unread message

gwh...@ti.com

unread,
Aug 12, 2013, 8:56:51 PM8/12/13
to
Hi,

In a batch file, I want to assign a variable as a string with the current directory stripped of the parent directories.

Say I am "in" (batch file doesn't know where it is run from):

C:\a\b\c\d

I want to set a variable to d.

If I do

set x=%cd%
echo %x%

Then the command screen shows:

C:\a\b\c\d


I tried various ways of FOR /F with delims=\ to strip off all but "d", but I can't figure it out.

I'm a hack when it comes to batch files. Does anyone know how to do this?

(I am trying to use it on Win7 command line at the moment.)

Thanks!

foxidrive

unread,
Aug 12, 2013, 9:14:25 PM8/12/13
to
On 13/08/2013 10:56, gwh...@ti.com wrote:

> In a batch file, I want to assign a variable as a string with the current directory stripped of the parent directories.
>
> Say I am "in" (batch file doesn't know where it is run from):
>
> C:\a\b\c\d
>
> I want to set a variable to d.
>
> If I do
>
> set x=%cd%
> echo %x%
>
> Then the command screen shows:
>
> C:\a\b\c\d

Try this:

@echo off
for /f "delims=" %%a in ("%cd%\.") do set "folder=%%~nxa"


--
foxi

gwh...@ti.com

unread,
Aug 14, 2013, 2:39:00 PM8/14/13
to
Oh, thank you very much! Fantastic.

I kind of see what it does. With 20-20 hindsight, some of it almost appears obvious. Isn't that always the way?

To me, it seems that the parsing part of the FOR "engine" only sees it as a string. That means it does not know if "d" is a file or a folder, so one can simply extract a folder name by using file syntax. It would be the same if the folder or file name was "d.x". (It has an extension.)


The part I don't quite get in this is the "\." suffix. Once you got me on the right track, I tried all these "in (...)" combos and got the same results for all:

for /f "delims=" %%G in ("%cd%\.") do set "folder=%%~nxG"
echo.
echo %%cd%%\. %folder%
echo.
for /f "delims=" %%G in ("%cd%.") do set "folder=%%~nxG"
echo.
echo %%cd%%. %folder%
echo.
for /f "delims=" %%G in ("%cd%") do set "folder=%%~nxG"
echo.

The syntax for all of the above work whether the directory is "d" or "d.x"

For me trying to understand the command line, I ask what does the "\." suffix do?

I also noticed I can eliminate "delims=" and get the same results for that path. But it breaks if there are spaces (like C:\a\b\c c\d), so I am I right that "delims=" sets the delimiter to "nothing?" (Meaning, there are no delimiters, take the whole string.)

Thanks again.

Todd Vargo

unread,
Aug 14, 2013, 5:40:06 PM8/14/13
to
On 8/14/2013 2:39 PM, gwh...@ti.com wrote:

> To me, it seems that the parsing part of the FOR "engine" only sees it as a string. That means it does not know if "d" is a file or a folder, so one can simply extract a folder name by using file syntax. It would be the same if the folder or file name was "d.x". (It has an extension.)
>
>
> The part I don't quite get in this is the "\." suffix. Once you got me on the right track, I tried all these "in (...)" combos and got the same results for all:
>
> for /f "delims=" %%G in ("%cd%\.") do set "folder=%%~nxG"
> echo.
> echo %%cd%%\. %folder%
> echo.
> for /f "delims=" %%G in ("%cd%.") do set "folder=%%~nxG"
> echo.
> echo %%cd%%. %folder%
> echo.
> for /f "delims=" %%G in ("%cd%") do set "folder=%%~nxG"
> echo.
>
> The syntax for all of the above work whether the directory is "d" or "d.x"
>
> For me trying to understand the command line, I ask what does the "\." suffix do?
>
> I also noticed I can eliminate "delims=" and get the same results for that path. But it breaks if there are spaces (like C:\a\b\c c\d), so I am I right that "delims=" sets the delimiter to "nothing?" (Meaning, there are no delimiters, take the whole string.)

The \. does not do anything in this case and can be omitted. In a
general sense, it refers to the current folder which is already provided
by the %cd% variable. And you are correct about the purpose for "delims=".

Type FOR/? at the prompt for further information.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

foxidrive

unread,
Aug 14, 2013, 8:05:47 PM8/14/13
to
On 15/08/2013 07:40, Todd Vargo wrote:

>> For me trying to understand the command line, I ask what does the "\." suffix do?

Thanks Todd, for supplying the answers. The \. sets the current folder at the end of the path, for when
the path has a trailing \ so that it then also returns the correct folder.


>> I also noticed I can eliminate "delims=" and get the same results for that path. But it breaks if there are spaces (like C:\a\b\c c\d), so I am I right that "delims=" sets the delimiter to "nothing?" (Meaning, there are no delimiters, take the whole string.)
>
> The \. does not do anything in this case and can be omitted. In a
> general sense, it refers to the current folder which is already provided
> by the %cd% variable. And you are correct about the purpose for "delims=".
>
> Type FOR/? at the prompt for further information.
>


--
foxi

Todd Vargo

unread,
Aug 14, 2013, 11:46:26 PM8/14/13
to
On 8/14/2013 8:05 PM, foxidrive wrote:
> On 15/08/2013 07:40, Todd Vargo wrote:
>
>>> For me trying to understand the command line, I ask what does the "\." suffix do?
>
> Thanks Todd, for supplying the answers. The \. sets the current folder at the end of the path, for when
> the path has a trailing \ so that it then also returns the correct folder.

AFAIK, the only time %cd% returns a trailing \ is when the root folder
is current. Are there other instances?

>
>
>>> I also noticed I can eliminate "delims=" and get the same results for that path. But it breaks if there are spaces (like C:\a\b\c c\d), so I am I right that "delims=" sets the delimiter to "nothing?" (Meaning, there are no delimiters, take the whole string.)
>>
>> The \. does not do anything in this case and can be omitted. In a
>> general sense, it refers to the current folder which is already provided
>> by the %cd% variable. And you are correct about the purpose for "delims=".
>>
>> Type FOR/? at the prompt for further information.
>>
>
>


--

foxidrive

unread,
Aug 15, 2013, 12:46:53 AM8/15/13
to
On 15/08/2013 13:46, Todd Vargo wrote:
> On 8/14/2013 8:05 PM, foxidrive wrote:
>> On 15/08/2013 07:40, Todd Vargo wrote:
>>
>>>> For me trying to understand the command line, I ask what does the "\." suffix do?
>>
>> Thanks Todd, for supplying the answers. The \. sets the current folder at the end of the path, for when
>> the path has a trailing \ so that it then also returns the correct folder.
>
> AFAIK, the only time %cd% returns a trailing \ is when the root folder
> is current. Are there other instances?

No, you are quite right that it was not needed in this instance. I was clarifying that it was just there
because it always works, and fixes the times when it is needed.


--
foxi
0 new messages