MrKatz
MrKatz

Reputation: 13

How to get current folder name in lowercase as variable in cmd?

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

Upvotes: 1

Views: 2461

Answers (3)

user7818749
user7818749

Reputation:

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"
)

Upvotes: 1

lit
lit

Reputation: 16266

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()

Upvotes: 0

dbenham
dbenham

Reputation: 130929

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

Upvotes: 1

Related Questions