批处理 - 将变量转换为大写

Jos*_*son 6 cmd batch-file robocopy

在使用之前,我如何将destl变量更改为大写.我假设某种字符交换,但是我无法使它工作.代码如下 -

@echo off
echo.

set /P "destf=Enter First Name: "

set /P "destl=Enter Last Name: "

set "findest=Z:\ProjectIT\copy\%destl%, %destf%"

robocopy Z:\ProjectIT\copy\test "%findest%" /e /NFL /NDL /NJH /NJS

robocopy Z:\ProjectIT\copy\Construction "%findest%"\1-BLANK-%destl% /e /NFL /NDL /NJH /NJS"

echo Construction folder has been created for "%destl%"
echo.

pause
Run Code Online (Sandbox Code Playgroud)

我试过调用类似下面的东西,但无法让它工作 -

:Uppercase
set %~1=!%1:a=A!
set %~1=!%1:b=B!
set %~1=!%1:c=C!
set %~1=!%1:d=D!
set %~1=!%1:e=E!
set %~1=!%1:f=F!
set %~1=!%1:g=G!
set %~1=!%1:h=H!
set %~1=!%1:i=I!
set %~1=!%1:j=J!
set %~1=!%1:k=K!
set %~1=!%1:l=L!
set %~1=!%1:m=M!
set %~1=!%1:n=N!
set %~1=!%1:o=O!
set %~1=!%1:p=P!
set %~1=!%1:q=Q!
set %~1=!%1:r=R!
set %~1=!%1:s=S!
set %~1=!%1:t=T!
set %~1=!%1:u=U!
set %~1=!%1:v=V!
set %~1=!%1:w=W!
set %~1=!%1:x=X!
set %~1=!%1:y=Y!
set %~1=!%1:z=Z!
Run Code Online (Sandbox Code Playgroud)

抱歉粗略的代码 - 我对此很新.

问候,

约书亚

roj*_*ojo 18

最短的方式(不需要第三方下载)将使用PowerShell.

set "str=The quick brown fox"
for /f "usebackq delims=" %%I in (`powershell "\"%str%\".toUpper()"`) do set "upper=%%~I"
Run Code Online (Sandbox Code Playgroud)

一种更快的方法,但仍然使用比任何纯批处理解决方案更少的代码将采用WSH.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "str=The quick brown fox"
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%str%"') do set "upper=%%~I"
set upper
goto :EOF

@end // end Batch / begin JScript hybrid
WSH.Echo(WSH.Arguments(0).toUpperCase());
Run Code Online (Sandbox Code Playgroud)

当然,您可以轻松地创建一个功能,以便您可以call根据需要多次使用它.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

call :toUpper upper1 "The quick brown fox"
call :toUpper upper2 "jumps over the lazy dog."
set upper
goto :EOF

:toUpper <return_var> <str>
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%~2"') do set "%~1=%%~I"
goto :EOF

@end // end Batch / begin JScript hybrid
WSH.Echo(WSH.Arguments(0).toUpperCase());
Run Code Online (Sandbox Code Playgroud)

或者如果你想对它真的很讨厌,你可以滥用tree命令的错误信息,如下所示:

@echo off & setlocal

set upper=
set "str=Make me all uppercase!"
for /f "skip=2 delims=" %%I in ('tree "\%str%"') do if not defined upper set "upper=%%~I"
set "upper=%upper:~3%"
echo %upper%
Run Code Online (Sandbox Code Playgroud)

  • 最初在 [this answer](http://stackoverflow.com/questions/15648560/windows-batch-file-read-text-file-and-convert-all-to-uppercase) 使用的转换代码是这一行: `对于 (ABCDEFGHIJKLMNOPQRSTU VWXYZ) 中的 %%b 设置“str=!str:%%b=%%b!”`。我还没有测试过,但是这种纯 Batch 方法可能与 WSH 方法一样快,因为加载 `cscript.exe` 文件的开销很大。 (2认同)
  • 我非常喜欢它!**`:))**+1它甚至可以正常使用西班牙语字符!尝试:"树"\ Elpapáylamamádeliñogouero"` (2认同)
  • 哇 - 从未想过滥用"树"+1.略微增强:`for/f"skip = 2 tokens = 1,*delims = \"%% I .... do ... set"upper = %% J"` (2认同)

Jos*_*son 9

感谢回复的人,我用这个解决了 -

if not defined %~1 EXIT /b
for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
        "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
        "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä"
        "ö=Ö" "ü=Ü") do (
call set %~1=%%%~1:%%~a%%
)
EXIT /b
Run Code Online (Sandbox Code Playgroud)

我确信你的反应更加整洁,效率更高,但是当我正在做的时候,我不想打破任何东西,我会把它留下来!

谢谢您的意见!