24

ドット バット スクリプトを使用して一部のソース管理ソフトウェア機能を自動化していますが、svn リポジトリが *NIX ボックスでホストされていることを考えると、これら 2 つの世界の間の永遠のケースの問題に直面しています。

Windows システム変数 %USERNAME% の値を小文字に変換する cmd.exe 関数はありますか?

よろしくお願いします!

4

9 に答える 9

17

さて、私はいくつかの構文をブラウジングしていて、このページに出くわしました。私はそれが古いことを知っていますが、休憩を取って脳に少しキックを与えると思いました.

これは、もう少し短くて扱いやすいものです。これは、実際の文字が文字列に存在するかどうかに関係なく、すべての大文字を小文字に「強引に強制」します。したがって、関数ループは、文字列の長さに関係なく、正確に 26 回実行されます。

これが誰かに役立つことを願っています。

@echo off
cls
setlocal enabledelayedexpansion

REM ***** Modify as necessary for the string source. *****
set "_STRING=%*"
if not defined _STRING set "_STRING=%USERNAME%"
set _STRING
REM ***** Modify as necessary for the string source. *****

set "_UCASE=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_LCASE=abcdefghijklmnopqrstuvwxyz"

for /l %%a in (0,1,25) do (
   call set "_FROM=%%_UCASE:~%%a,1%%
   call set "_TO=%%_LCASE:~%%a,1%%
   call set "_STRING=%%_STRING:!_FROM!=!_TO!%%
)

set _STRING
endlocal

例:

E:\OS.ADMIN>LCASE.BAT The Quick Fox Jumps Over The Brown Fence.

結果:

_STRING=The Quick Fox Jumps Over The Brown Fence.
_STRING=the quick fox jumps over the brown fence.
于 2014-05-22T12:13:27.930 に答える
10

簡単なグーグルがこれを見つけまし...

@echo off
goto :end_remarks
*************************************************************************************
*
*
*    authored:Sam Wofford
*    Returns lowercase of a string
*    12:13 PM 11/13/02
**************************************************************************************
:end_remarks
setlocal
set errorlevel=-1
if {%1}=={} echo NO ARG GIVEN&call :Help &goto :endit
if {%1}=={/?} call :Help &goto :endit
call :set_LCASE_array 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

:start
set input=%1
set input=%input:"=%
set totparams=0
call :COUNT_PARAMS %input%
call :MAKE_LOWERCASE %input%
set errorlevel=
echo %convertedstring%
endlocal
goto :eof
:endit
echo %errorlevel%
endlocal
goto :eof

:MAKE_LOWERCASE
:nextstring
if {%1}=={} goto :eof
set string=%1
set /a params+=1
set STRINGCONVERTED=
set pos=0
:NEXT_CHAR
set onechar=%%string^:^~%pos%,1%%
for /f "tokens=1,2 delims==" %%a in ('set onechar') do for /f %%c in ('echo %%b') do call :checkit %%c
if not defined STRINGCONVERTED goto :NEXT_CHAR
shift /1
if %params% LSS %totparams% set convertedstring=%convertedstring% &:add one space,but not at end
goto :nextstring
goto :eof

:Help
echo USAGE:%~n0 string OR %~n0 "with spaces"
echo function returns the lowercase of the string or -1 (error)
echo strings with embedded spaces needs to be in quotes Ex. "lower case"
echo in a batch NTscript "for /f %%%%A in ('lcase STRING') do set var=%%%%A"
set errorlevel=
goto :eof

:checkit
set LCFOUND=
if /i {%1}=={echo} set STRINGCONVERTED=Y&goto :eof
set char=%1
for /f "tokens=2 delims=_=" %%A in ('set LCASE_') do call :findit %%A %char%
:skipit
if defined LCFOUND (set convertedstring=%convertedstring%%ucletter%) else (set convertedstring=%convertedstring%%char%)
set /a pos+=1
goto :eof

:set_LCASE_array
:setit
if {%1}=={} goto :eof
set LCASE_%1_=%1
SHIFT /1
goto :setit

:findit
if defined LCFOUND goto :eof
set ucletter=%1
set lcchar=%2
if /i {%ucletter%}=={%lcchar%} set LCFOUND=yes
goto :eof

:COUNT_PARAMS
:COUNTPARAMS
if {%1}=={} goto :eof
set /a totparams+=1
shift /1
goto :COUNTPARAMS 

それをファイル(lowercase.cmd)としてパスに追加すると、それを「Lowercase.cmd %Username%」として呼び出すことができるはずです。必要に応じて別のコマンドにパイプできます。

于 2008-11-12T18:22:18.687 に答える
7

http://short.stop.home.att.net/freesoft/unix.htmから DOS 用の UNIX ユーティリティをダウンロードし、 tr.exe (文字の変換) を使用します。

echo %USERNAME% | tr "[A-Z]" "[a-z]" 

また、コマンド @lower が組み込まれている 4NT という名前の DOS 拡張コマンド置換も使用します。

echo %@lower[%USERNAME%]
于 2008-11-12T21:10:53.573 に答える
7

http://www.dzone.com/snippets/lowercasing-string-bat-files

下.バット

echo>%1
dir /b/l %1>lower.tmp
set /p result=<lower.tmp
echo %result%

コマンド

lower "Mein BinnenMajuskel"

結果

mein binnenmajuskel

注意: 素早く汚いだけでなく、安全性が低く危険な亜種です。2 つのファイルを作成するためです。1 つは指定された文字列のように呼び出され、もう 1 つは、下げられた文字列を含む lower.tmp と呼ばれます。lower "UserName"このファイルまたはディレクトリが既に存在するディレクトリで実行するとどうなりますか? 特に後でこのファイルを削除すると...

改良版:

echo>%Temp%\%1
dir /b/l %Temp%\%1>%Temp%\lower.tmp
set /p result=<%Temp%\lower.tmp
del %Temp%\%1
del %Temp%\lower.tmp
于 2015-03-18T09:50:19.133 に答える
5

スクリプト言語がインストールされている場合FOR、変数を設定するためにそれを使用できます。

@FOR /F %%s IN ('<<some script oneliner>>') DO @set MYVARIABLE=%%s

文字列を小文字に変換して結果を出力できるスクリプト言語であれば、どのようなスクリプト言語でも使用できます。

Perl 5 を使用した例:

@FOR /F %%s IN ('perl -e "print lc(pop)" %USERNAME%') DO @set USERNAME=%%s

PowerShell を使用した例:

@FOR /F %%s IN ('powershell -command "(get-item env:'USERNAME').Value.ToLower()"') DO @set USERNAME=%%s

最近では、PowerShell が既定で既にインストールされている可能性があります。

于 2015-12-04T10:02:33.017 に答える
4

私のバッチ ファイルでは、%USERNAME% と CSV ファイルの比較を行っています。

ユーザーが大文字のユーザー名でログインしている場合、プログラムは機能しませんでした。

例:
ログイン : GB2NOGU // 機能しない
ログイン : gb2nogu // 機能する

ここで、鈍感な比較を行うことで問題を解決できました。

if /i %USERNAME%==gb2nogu (
     // Code here
)

パラメータ /i は、大文字と小文字を区別しない比較を行うように cmd に指示するため、小文字と大文字の違いは無視されます。

于 2019-08-02T13:20:42.637 に答える
3

おそらくこれは、マクロを使用し、一時ファイルがないため、バッチ ファイルで文字列を小文字に変換する最速の方法です (生成された文字列を という変数に保存しますresult)。

@echo off

set LowerCaseMacro=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"
%LowerCaseMacro%%string%

echo %result%
于 2020-05-26T17:48:17.550 に答える
2
:: UPcase.bat ==> Store in environment variable _UPcase_ the upper case of %1
:: -> Use quotes "" when the first argument has blanks or special characteres
::
:: Adapted from -> http://www.netikka.net/tsneti/info/tscmd039.htm
::
:: Note that the substitution method is case insensitive, which means that
:: while working for this application, it is not useful for all character
:: substitution tasks.
::
:: More concisely, one can capitalize (if you pardon the pun) on the fact
:: that in for and the substitution lower and upper case source are
:: equivalent.
@echo off

:: %~1 -> removes quotes from the first command line argument
:: http://steve-jansen.github.io/guides/windows-batch-scripting/part-2-variables.html
@echo off
::setlocal EnableExtensions
    :: echo %_UPcase_%
    call :ToUpcaseWithFor "%~1" _UPcase_
    :: echo %_UPcase_% _doit_1_
::endlocal & goto :EOF
goto :EOF
::
:: ======================
:ToUpcaseWithFor
setlocal EnableExtensions EnableDelayedExpansion
  set var_=%~1
  for %%c 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 var_=!var_:%%c=%%c!
  )
endlocal & set %2=%var_%& goto :EOF

:EOF
:: UPcase.bat ==> EOF
于 2014-10-03T16:13:47.690 に答える