1

I am trying to get the current directory folder name in lowercase form.

I understand I can get the current folder name with the following:

for %I in (.) do echo %~nxI

but I have no idea on how to convert that to lowercase.

I need to use it in a single line command.

for %I in (.) do echo %~nxI

What i am after:

c:\Program Files
for %I in (.) do echo %~nxI ---edited
program files

Currently getting

c:\Program Files
for %I in (.) do echo %~nxI
Program Files
Community
  • 1
  • 1
MrKatz
  • 13
  • 1
  • 6
  • You can take a look at this [How to convert the value of %USERNAME% to lowercase within a Windows batch script?](https://stackoverflow.com/questions/284776/how-to-convert-the-value-of-username-to-lowercase-within-a-windows-batch-scrip) – Hackoo Oct 23 '19 at 01:02

3 Answers3

1

Using pure batch, with temp file:

Single line cmd:

@for %i in (.) do echo>"%temp%\%~nxi" & @dir /b /l "%temp%\%~nxi" & @del /Q "%temp%\%~nxi"

or in a batch-file

@echo off
for %%i in (.) do set "var=%%~nxi"
echo>"%temp%\%var%"
for /f "delims=" %%a in ('dir /b /l "%temp%\%var%"') do echo %%a & del /Q "%temp%\%var%"

or slightly shorter, without echo:

@echo off
for %%i in (.) do (
  echo>"%temp%\%%~nxi"
  dir /b /l "%temp%\%%~nxi"
  del /Q "%temp%\%%~nxi"
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • works great, but a little worried about creating and then deleting files. But as it creates the file in the temp directory. Risk is minimal. – MrKatz Oct 23 '19 at 12:45
  • yeah, well quick answer I made without having time to do more, but @dbenham already made the needed changes in his answer. – Gerhard Oct 23 '19 at 13:10
  • similar, but will only work for files and directories.. to use `dir /l` for other strings you still require temp files. `@for %i in (.) do for /f "tokens=*" %a in ('dir /b/l ..\^|findstr /xic:"%~nxi"') do @echo %a` – Gerhard Oct 23 '19 at 13:38
1

Here is a method using dir /b /l, but unlike Gerhard's answer, without need of a temp file.

From the command line:

for %A in (.) do @for /f "eol=: delims=" %F in ('dir /b /l /ad "..\%~nxA*"^|findstr /xic:"%~nxA"') do @echo %F

Within a batch script:

@echo off
for %%A in (.) do for /f "eol=: delims=" %%F in (
  'dir /b /l /ad "..\%%~nxA*" ^| findstr /xic:"%%~nxA"'
) do echo %%F
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Great answer. Thanks. Works exactly as needed. – MrKatz Oct 23 '19 at 12:46
  • Yeah, this is a better option for this specific case. We need to revert to temp file however if any other strings needs to be converted and we use `dir /l`, but yeah, you win `:D` – Gerhard Oct 23 '19 at 13:22
0

If you are on a supported Windows platform, it will have PowerShell.

powershell -NoLogo -NoProfile -Command "(Split-Path -Leaf -Path (Get-Location)).ToLower()"

If you want the result in a variable in a .bat file script:

FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command "(Split-Path -Leaf -Path (Get-Location)).ToLower()"') DO (SET "LCCD=%%~A")
ECHO %LCCD%

Of course, if you could use PowerShell without cmd.exe, it would just be:

(Split-Path -Leaf -Path (Get-Location)).ToLower()
lit
  • 14,456
  • 10
  • 65
  • 119