1.

Solve : Get current folder name?

Answer»

Hi all,

can anyone tell me how can I get the current folder name?

example: If I'm now in C:\XXX\YYY\ZZZ
I would to set a variable var, that takes the current folder name only not the whole path, so when try to print the value of the variable (echo %var%)

the result will be: ZZZ

thanks in advanceTry this but not been tested with LFN or spaces in directory names.

Code: [Select]@echo off & setlocal
cls

set var=%cd%
for /d %%b in (%var%) do set fold=%%~nb

echo Default directory = %fold%
endlocal

Good luckThis also works, uses the fact that the cd command without any parameters echoes the current folder drive\path\name and ~nx will extract the name.

Code: [Select]for /f "delims=" %%A in ('cd') do set foldername=%%~nxA
It is perfectly happy with a long folder name with or without spaces, (the "delims=" sees to that) although that name might need quoting elsewhere, depending on what you intend to do with it. Quote from: Dusty on September 02, 2008, 04:33:09 AM

Try this but not been tested with LFN or spaces in directory names.

doesn't work on folder with spaces. Just returns the part after the last space, e.g. "folder name with spaces" shows up as "spaces".

These modifications made it work plus I trimmed unnecessary code

@echo off
for /f "delims=" %%b in ("%cd%") do set fold=%%~nb
echo %fold%

Thanks alot Hey guys, the ~nx example seems to work fine until I come across a folder with an ampersand (&) in it.

(Btw, the ~nb example does NOT work correctly if there are any periods in the folder name--it always cuts off EVERYTHING after the last period. The ~nx method works fine with periods THOUGH.)

To test it, make a folder called This.is.{ –a ]} crazy.test,}—folder #.[@mad(symbols)!='%^-

The ~nx works fine on that.

Now rename the folder, adding a & symbol anywhere in the folder name, and you get "........blah blah blah....... is not recognized as an internal or external command, operable program or batch file."


Any way around this?Quote from: AVP on September 15, 2008, 10:43:00 AM
Hey guys, the ~nx example seems to work fine until I come across a folder with an ampersand (&) in it.

[...]

Any way around this?

Try quoting the string. E.g.

Code: [Select]set fold="%%~nb"
The ampersand character is called, in NT batch scripting circles, the 'poison character'. This is because it is used in the batch LANGUAGE as a command separator. In a batch, this:

Code: [Select]cls&echo %date%&echo %time%
is processed as if it were this:

Code: [Select]cls
echo %date%
echo %time%
So, therefore, this:

Code: [Select]set fold=Mom&Dad
is treated as if it were this:

Code: [Select]set fold=Mom
Dad
and the result is a message saying this:

Code: [Select]'Dad' is not recognized as an internal or external command,
operable program or batch file.
The above solution may not always work in every situation, and a good policy is to avoid using control characters in file names, (or learn VBscript!)
Thanks Dias -- the quotes do allow the ampersand. I think this will work for what I need, but if I did need the quotes removed later, am I stuck?

Btw, yes, I wish I could control what people are naming their folders, but sometimes they do PUT ampersands in them. Is there a way to rename the folder that has a ampersand into a + instead? Maybe with SET foldernew=%foldername:&=+%You can generate a VBscript and then call it to replace ampersands in a string

Code: [Select]@echo off
setlocal enabledelayedexpansion
echo Set oArgs=WScript.Arguments>%temp%\deAmp.vbs
echo MyString = oArgs(0)>>%temp%\deAmp.vbs
echo Wscript.echo (Replace (MyString, "&", "+"))>>%temp%\deAmp.vbs
for /f "delims==" %%A in ('dir /b') do (
set filename="%%A"
for /f "delims==" %%R in ('cscript /nologo %temp%\DeAmp.vbs ""!filename!""') do set deampname="%%R"
ren !filename! !deampname!
)


Quote from: AVP on September 15, 2008, 11:59:37 AM
if I did need the quotes removed later, am I stuck?

The ~ variable modifier, used by itself, dequotes a quoted string, and leaves an unquoted string unchanged.

Code: [Select]set quotedstring="a string with quotes"
for %%S in (%quotedstring%) do set unquotedstring=%%~S
echo %unquotedstring%


Thanks for all your help Dias! I may also throw up a message that tells the user "hey, stop using ampersands in your folder names" you can get current working directory and its parent folder and current folder name in vbscript
Code: [Select]Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFS = CreateObject("Scripting.FileSystemObject")
strCurrentFolder=WshShell.CurrentDirectory
WScript.Echo strCurrentFolder
'Get parent folder
WScript.Echo objFS.GetParentFolderName(strCurrentFolder)
'Get current folder name
WScript.Echo objFS.GetFolder(strCurrentFolder).Name



Discussion

No Comment Found