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

时间:2016-01-11 03:09:17

标签: batch-file cmd 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

我尝试过调用以下内容,但无法让它起作用 -

: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!

对于粗略的代码感到抱歉 - 我对此很陌生。

此致

约书亚

9 个答案:

答案 0 :(得分:14)

最短的方式(无需第三方下载)将使用PowerShell。

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

更快的方法,但仍然使用比任何纯批处理解决方案更少的代码将采用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());

当然,您可以轻松地创建一个函数,以便根据需要多次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());

或者如果你想对它真的很讨厌,你可以滥用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%

答案 1 :(得分:7)

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

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

我确定你的回答更加整洁,效率更高,但是当我正在做伎俩而且我不想打破任何东西时我会保留原样!

感谢您的投入!

答案 2 :(得分:3)

@ECHO Off
SETLOCAL
SET "betabet=abcdefghijklmnopqrstuvwxyz1234567890!*$^&^^+=-\^|^>;'.,/?^<"
ECHO %betabet%
>u:\betabet.file ECHO %betabet%
CALL :upper betabet
ECHO %betabet%
CALL :upcase u:\betabet.file 
GOTO :EOF

:upper
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 CALL SET "%1=%%%1:%%a=%%a%%%"
GOTO :EOF

:upcase
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (%1) do (
   set "line=%%a"
   for %%b 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 "line=!line:%%b=%%b!"
   )
   echo !line!
)
endlocal
GOTO :eof

Aacini在Windows batch file read text file and convert all to uppercase引用的方法对某些字符失败,如上述批次所示。

如果相关字符串包含某些字符(例如%:),此批次也会失败 - 它会转换字母,但会删除%:

上述批次首先建立一个包含杂项字符的字符串,显示它,将其保存为文件,然后使用:upper进行转换。

为了进行比较,然后使用:upcase函数(从链接的响应派生)来处理该文件。

根据Aacini的有效评论和进一步调查,以下是一些技巧(包括&#39;文件读取&#39;方法的演示)

通过在 delayedexpansion 模式中显示:showlineline例程可以缩短代码

@ECHO Off
SETLOCAL
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
SETLOCAL enabledelayedexpansion
>u:\betabet.file ECHO "!line!"
endlocal
CALL :showline
ECHO --- show conversion using "upper - caret disappears
CALL :upper line
CALL :showline
ECHO ------------------------------
ECHO --- show actual file contents ^& data read from file
type u:\betabet.file 
CALL :upcase u:\betabet.file 
ECHO ------------------------------
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
CALL :showline
ECHO --- show conversion using "inline-conversion - caret PRESERVED
setlocal ENABLEDELAYEDEXPANSION
for %%b 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 "line=!line:%%b=%%b!"
echo "!line!"   ^<--- in inline conversion
endlocal&SET "line=%line:^=^^%"
CALL :showline
ECHO ------------------------------
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
CALL :showline
ECHO --- show conversion using "upcase2 - caret disappears
CALL :upcase2 line
CALL :showline
GOTO :EOF

:upper
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 CALL SET "%1=%%%1:%%a=%%a%%%"
GOTO :EOF

:upcase
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (%1) do (
 ECHO read%%a
   set "line=%%a"
   for %%b 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 "line=!line:%%b=%%b!"
   )
   echo conv!line!
)
endlocal
GOTO :eof

:upcase2
setlocal ENABLEDELAYEDEXPANSION
for %%b 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 "%1=!%1:%%b=%%b!"
endlocal&CALL SET "%1=%%%1%%"
GOTO :eof

:showline
SETLOCAL enabledelayedexpansion
ECHO "!line!"
endlocal
GOTO :eof

答案 3 :(得分:2)

这是没有意义的,因为env var名称和文件名都不区分大小写。但如果你坚持你可以这样做:

@echo off

setlocal enabledelayedexpansion
set lower=john smith 123
set alpha=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set i=0
:letter
set !alpha:~%i%,1!_=!alpha:~%i%,1!
set /a i += 1
if %i% LSS 26 goto letter
set i=0
set upper=
:uppercase
set let=!lower:~%i%,1!
if "%let%" == "" goto done
if defined %let%_ (set upper=%upper%!%let%_!) else (set upper=%upper%%let%)
set /a i += 1
goto uppercase
:done
echo %upper%

答案 4 :(得分:0)

我使用了2个文件,在批处理中,我称为外部js,在蝙蝠中,我将响应分配给了内部var:

蝙蝠:

@echo off
for /f %%i in ('cscript //nologo //E:jscript jsfile.js %1') do set VAR=%%i
echo %Var%

js(同一目录中的jsfile.js):

WScript.echo(WSH.Arguments(0).toUpperCase())

答案 5 :(得分:0)

如果您像许多开发人员一样在Windows上安装了POSIX实用程序(例如https://sourceforge.net/projects/gnuwin32/的GNUWin实用程序)

您可以使用tr.exe实用程序,该实用程序具有:[:upper:]

@echo off
SETLOCAL EnableDelayedExpansion
echo Before: SCEINST is: %SCEINST%
set upper=
for /f "usebackq delims=" %%g in (`echo %SCEINST% ^| tr [:lower:] [:upper:]`) do (
    SET upper=%%g
    :: get rid of any leading whitespace
    set upper=!upper: =!
    goto :done
)
:done
echo After: SCEINST (in upper) is now: !upper!

----------输出----------

Before: SCEINST is: scprd
After: SCEINST (in upper) is now: SCPRD

答案 6 :(得分:0)

我已经扩展了出色的answer by the OP,并将其变成可以在整个批处理文件中调用的函数。

@echo off

set "str=Make meeeee all uppercaseeeee!"
echo %str% (Old variable)
call :TOUPPERCASE str
echo %str%
goto :eof

rem FUNCTIONS AT VERY BOTTOM
:TOUPPERCASE
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%%
)
goto :eof

结果

Make meeeee all uppercaseeeee! (Old variable)
MAKE MEEEEE ALL UPPERCASEEEEE!

答案 7 :(得分:0)

这可能是使用批处理文件将字符串转换为大写字母的最快方法-它使用macro,它使用批处理替换字符串并存储产生的结果的方式在循环中仅存储大写字母名为result的变量中的字符串:

@echo off

set UpperCaseMacro=for /L %%n in (1 1 2) do if %%n==2 (for %%# 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 "result=!result:%%#=%%#!") else setlocal enableDelayedExpansion ^& set result=

set "string=SOme STrinG WiTH lowerCAse letterS and UPCase leTTErs"
%UpperCaseMacro%%string%

echo %result%

答案 8 :(得分:0)

一个通用的翻译器,看起来简单而且能够 做的工作是 -

@echo off
call:@translate WhIiPpiTy abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
echo %_answer%
call:@translate "12:37 am" apm APM
echo %_answer%
pause
exit  /b
:@translate
setlocal
set j=%~1
set f=%~2
set t=%~3
set x=0
:translate_loop
call set r=%%f:~%x%,1%%=%%t:~%x%,1%%
call set j=%%j:%r%%%  >NUL 2>&1
if %ERRORLEVEL%==1 goto translate_end
set /a x+=1
goto translate_loop
:translate_end
endlocal & set _answer=%j%
exit /b