在Windows批处理文件中将变量的首字母大写 [英] Capitalize first letter of the variable in the windows batch file

查看:339
本文介绍了在Windows批处理文件中将变量的首字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将%COMPUTERNAME%变量的输出的首字母大写.我尝试了以下无效的代码.

I'm trying to capitalize the first letter of the output from the %COMPUTERNAME% variable. I have tried the below code which doesn't work.

set host=%COMPUTERNAME%
echo %host% | sed 's/^\(.\)/\1\u\2/g/'

输出应为Beast而不是BEASTbeast

推荐答案

我经常倾向于为此使用powershell命令:

I ever often tend to wrap a powershell command for this:

:: Q:\Test\2018\10\12\SO_52769852.cmd
@Echo off
For /f %%A in ('
  Powershell -NoP -C "$Env:COMPUTERNAME.Substring(0,1).ToUpper()+$Env:COMPUTERNAME.Substring(1).ToLower()"
') do set host=%%A
Echo:%host%

这篇关于在Windows批处理文件中将变量的首字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆