get current folder name (dot problem)

December 21, 2009 at 21:30:48
Specs: Windows 7
(I posted the same post into the WIN7 forum, but I recognized later, that there is a forum specially for Programming, so I repost it. Hope you'll forgive me =p )

Hi,
I need to get the folder name, where the batch is run from, as a var to work with later.
I nearly solved this Problem with one of the scripts from this side.
Now I use

for /F "delims=\" %%I in ("%CD%") do set e=%%~nI

to get the folder name. It does work... but not, if the Folder contains any [dot].

My folder structure is like

...\temp1\temp 2\temp 3.1.2\this.bat

but when I use the code mentioned before, I only get
temp 3.1
Does anyone know, how to get the chars behind the last dot in the var as well?
Thanks and greetings
neXor

See More: get current folder name (dot problem)

Report •


#1
December 21, 2009 at 22:11:14
Try this:

for %%I in ("%cd%") do set e=%%~nxI

The trick is to add the "x" for extension, even though it's a folder(see "for /?").

Edit: see next post.

If %cd% is always going to be the same folder the batch is run from(no cd's or pushd's) then this may also work:

set e=%~nx0



Batch Variable how to


Report •

#2
December 21, 2009 at 23:24:56
Hi Judago,

I'm glad it's not just me who saw no point in delims.

"set e=%~nx0"

Will get the bat name, not the folder.


=====================================
Helping others achieve escape felicity

M2


Report •

#3
December 22, 2009 at 00:00:55
Hi M2,

Will get the bat name, not the folder.

Your 100% correct as usual.

I'm glad it's not just me who saw no point in delims.

Not only did I see no point, I'm surprised the op said it worked at all...

Edit: He he, it can be used to get the current directory of any given drive...


Batch Variable how to


Report •

Related Solutions

#4
December 22, 2009 at 05:44:53
Thank you, Judago.
that was the point, now its working quite fine!
*wave*
neX

Report •


Ask Question