Last directory name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
maxcaribbean
Posts: 3
Joined: 23 Mar 2018 16:38

Last directory name

#1 Post by maxcaribbean » 23 Mar 2018 16:55

Hello,
i would like to get the name of the last repository. (Sorry for my language, i try to improve that too:) ).

My current directory is:
c:\users\ssdmax\Google Drive\Exercices\temp

i try to get "temp" as a string in a variable.

For that purpoose, a make a batch file:

Code: Select all

set cd1=%cd%
cd ..
set cd2=%cd%%
cd %cd1%

::thats command works and do the job, but depend of the example:
set "repert=%cd1:*Exercices\=%"
echo %repert%
:: %repert%=temp  wonderful!!!

::what i would like, but doesnt work:
set"reperto=%cd1:cd2=%

The idea is to substitute the begining of the path with nothing to extract the last name:
cd1=c:\users\ssdmax\Google Drive\Exercices\temp
cd2=c:\users\ssdmax\Google Drive\Exercices
replace cd2 into cd1 with nothing should give me temp.

I hope i've been clear. Sorry again for my language. Thanks.

Max.

ps: my computer is w10 64 bits, i use DOS command to call my batch file.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Last directory name

#2 Post by Ed Dyreen » 23 Mar 2018 18:51

My current directory is:
c:\users\ssdmax\Google Drive\Exercices\temp

i try to get "temp" as a string in a variable.

Code: Select all

@echo off

for /?

for %%? in ( "c:\users\ssdmax\Google Drive\Exercices\temp" ) do echo. ?=%%~n?_
according to the help provided by for /? command temp is not a directory name but a filename, a directory ends with a backslash.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Last directory name

#3 Post by Compo » 24 Mar 2018 12:23

Code: Select all

Set "repert=%CD:\="&Set "repert=%"

maxcaribbean
Posts: 3
Joined: 23 Mar 2018 16:38

Re: Last directory name

#4 Post by maxcaribbean » 24 Mar 2018 12:41

Hello,
thanks to Compo and Ed Dyreen, both solutions are working fine!!! I put them both on the same message.


Code: Select all

for %%? in ( "c:\users\ssdmax\Google Drive\Exercices\temp" ) do set repert=%%~n?
echo %repert%

Set "repertt=%CD:\="&Set "repertt=%"
echo %repertt%

I've try to understand them, in order to modify them, but my level in batch is too low.
If someone wants to comment them, it will be helpfull, and i thanks him.

Against thanks for your quick help.

Max

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Last directory name

#5 Post by Ed Dyreen » 24 Mar 2018 13:54

If someone wants to comment them, it will be helpfull, and i thanks him.

Code: Select all

for %%? in ( "c:\users\ssdmax\Google Drive\Exercices\temp" ) do set repert=%%~n?
echo %repert%
FOR %variable IN ("symbolsequence") DO command [commandparameters]
%~nI - expands %I only to a filename

Code: Select all

Set "repertt=%CD:\="&Set "repertt=%"
echo %repertt%
CD=c:\users\ssdmax\Google Drive\Exercices\temp
Set "repertt=%CD:\="&Set "repertt=%"
Set "repertt=c:"&Set "repertt=users"&Set "repertt=ssdmax"&Set "repertt=Google Drive"&Set "repertt=Exercices"&Set "repertt=temp

both solutions give incorrect results if expression contains escape characters or unicode characters outside the used codepage. ( this can be overcome, but increases complexity and the time it takes for the command to complete. )

maxcaribbean
Posts: 3
Joined: 23 Mar 2018 16:38

Re: Last directory name

#6 Post by maxcaribbean » 28 Mar 2018 03:55

Great thanks for your answers, it's really helpfull!

Post Reply