JSI Tip 0568 - How do I convert a file name to lowercase?

NOTE: See tip 8379 ยป How do I convert a file name to lowercase for a better solution.

If you have a folder with numerous mixed case names, locating a file may be difficult due to the sort sequencing. You may have other reasons for using a constant case when naming files.

In tip 537, we learned about environment variable string substitution. In tip 494 we learned how to parse a batch filename parameter into it's constituent parts.

We can combine these techniques in a batch file that I will name LwrCase.bat which should be located in your path:

@echo off
set LC1=%~nx1
set LC1=%LC1:"=%
set LC1=%LC1:A=a%
set LC1=%LC1:B=b%
set LC1=%LC1:C=c%
set LC1=%LC1:D=d%
set LC1=%LC1:E=e%
set LC1=%LC1:F=f%
set LC1=%LC1:G=g%
set LC1=%LC1:H=h%
set LC1=%LC1:I=i%
set LC1=%LC1:J=j%
set LC1=%LC1:K=k%
set LC1=%LC1:L=l%
set LC1=%LC1:M=m%
set LC1=%LC1:N=n%
set LC1=%LC1:O=o%
set LC1=%LC1:P=p%
set LC1=%LC1:Q=q%
set LC1=%LC1:R=r%
set LC1=%LC1:S=s%
set LC1=%LC1:T=t%
set LC1=%LC1:U=u%
set LC1=%LC1:V=v%
set LC1=%LC1:W=w%
set LC1=%LC1:X=x%
set LC1=%LC1:Y=y%
set LC1=%LC1:Z=z%
ren %1 "%LC1%"

We can call the LwrCase.bat batch file with the full path name of a file (or just the file name if it is in the current folder) that we wish to rename. Example:

call LwrCase "%SystemRoot%\System32\Ancient Pathways.dll"

If you wanted to change all the files in a folder, call LwrCase_Folder "<Drive:>\<Path>" where LwrCase_Folder contains:

@echo off
pushd %1
for %%i in (*.*) do call lwrcase "%%i"
popd

If you want to change all the files in a folder, including all sub-folders, call LwrCase_Tree "<Drive:>\<Path>" where LwrCase_Tree contains:

@echo off
pushd %1
dir *.* /b /a-d /s > lwrcase.log
for /f %%i in ('type lwrcase.log') do call lwrcase "%%i"
del /q lwrcase.log
popd



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish