我已经尝试使用下面的代码使用DOS批处理文件将文件名的大小写更改为小写,但我想知道是否有人可以帮助我添加包含代码来大写每个文件名的第一个字母。
乔治: - )
pushd %varFolderTemp%
setlocal enableDelayedExpansion
for %%f in (*.*) do (
set "filename=%%~nf"
set filename=!filename:A=a!
set filename=!filename:B=b!
set filename=!filename:C=c!
set filename=!filename:D=d!
set filename=!filename:E=e!
set filename=!filename:F=f!
set filename=!filename:G=g!
set filename=!filename:H=h!
set filename=!filename:I=i!
set filename=!filename:J=j!
set filename=!filename:K=k!
set filename=!filename:L=l!
set filename=!filename:M=m!
set filename=!filename:N=n!
set filename=!filename:O=o!
set filename=!filename:P=p!
set filename=!filename:Q=q!
set filename=!filename:R=r!
set filename=!filename:S=s!
set filename=!filename:T=t!
set filename=!filename:U=u!
set filename=!filename:V=v!
set filename=!filename:W=w!
set filename=!filename:X=x!
set filename=!filename:Y=y!
set filename=!filename:Z=z!
ren "%%~nxf" "!filename!%%~xf"
)
popd
答案 0 :(得分:1)
您可以充分利用子字符串替换不区分大小写的事实,即在源字符中忽略大小写,但保留在替换字符中:
@echo off
setlocal EnableDelayedExpansion
rem Capitalize.bat: Change file names with first letter uppercase, the rest
lowercase
for %%f in (*.*) do (
set "filename=%%~Nf"
rem Change all letters to lowercase
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
set "filename=!filename:%%a=%%a!"
)
rem Change first letter to uppercase
set "letter=!filename:~0,1!"
for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
set "letter=!letter:%%A=%%A!"
)
ren "%%f" "!letter!!filename:~1!%%~Xf"
)
答案 1 :(得分:0)
不确定您的代码是否是最佳(可能是)解决方案,但我没有更改它:
@echo off
setlocal enableDelayedExpansion
for %%f in (*.*) do (
set "filename=%%~nf"
set filename=!filename:A=a!
set filename=!filename:B=b!
set filename=!filename:C=c!
set filename=!filename:D=d!
set filename=!filename:E=e!
set filename=!filename:F=f!
set filename=!filename:G=g!
set filename=!filename:H=h!
set filename=!filename:I=i!
set filename=!filename:J=j!
set filename=!filename:K=k!
set filename=!filename:L=l!
set filename=!filename:M=m!
set filename=!filename:N=n!
set filename=!filename:O=o!
set filename=!filename:P=p!
set filename=!filename:Q=q!
set filename=!filename:R=r!
set filename=!filename:S=s!
set filename=!filename:T=t!
set filename=!filename:U=u!
set filename=!filename:V=v!
set filename=!filename:W=w!
set filename=!filename:X=x!
set filename=!filename:Y=y!
set filename=!filename:Z=z!
ren "%%~nxf" "!filename!%%~xf"
)
for %%l in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
for %%f in (*.*) do (
set filename=%%~nf
if /i "!filename:~0,1!" EQU "%%~l" (
ren "%%~nxf" "%%l!filename:~1!%%~xf"
)
)
)
endlocal
popd