Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 369 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Batch - Converting variable to uppercase

#1
How would I go about changing the **destl** variable to uppercase before it is used. I assume some sort of character swap, however I couldn't get it working. Code is as follows -


@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

I have tried calling something like the following, but could not get it to work -

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

Sorry about the rough code - I'm quite new to this.

Regards,

Joshua
Reply

#2
<!-- language: lang-dos -->

@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

The method referred to by Aacini at [

[To see links please register here]

][1] fails for some characters, as is demonstrated by the above batch.

This batch will also fail if the string in question contains certain characters (eg `%:`) - it will convert the alphas, but delete `%` and `:`.

The above batch first establishes a string containing miscellaneous characters, displays it, saves it as a file, then converts it using `:upper`.

For comparison, the file is then procesed using the `:upcase` function (derived from the linked response).


----------
Following Aacini's valid comment and further investigation, here are some techniques (including a demo of the 'read-from-file' method)

The `:showline` routine exists to shorten the code by displaying `line` in *delayedexpansion* mode

<!-- language: lang-dos -->

@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



[1]:

[To see links please register here]

Reply

#3
This is pointless since env var names and file names are both case insensitive. But if you insist you can do it like this:

@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%
Reply

#4
The shortest way (without requiring 3rd party downloads) would be to use PowerShell.

<!-- language-all: lang-js -->

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

A faster way but still using less code than any pure batch solution would be to employ 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());

And of course, you can easily make either a function so you can `call` it multiple times as needed.

@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());

----

Or if you want to be really hacksy about it, you could abuse the `tree` command's error message like this:

@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%
Reply

#5
Thanks for the responses guys, I solved it using this -


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
I'm sure your responses are far neater and more efficient, but as mine is doing the trick and I don't want to break anything I will leave it as is!

Thank you for your input!
Reply

#6
I used 2 files, in the batch I called an external js and in the bat I assign the response to an internal var:

Bat:

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

Js (jsfile.js in same directory):

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

#7
If you have any POSIX utilities installed on Windows, like many developers (e.g., GNUWin utilities at

[To see links please register here]

)

You could use the tr.exe utility, which has: [: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!

----------OUTPUT----------

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



Reply

#8
I've expanded upon the great [answer by the OP][1] and made it into a function that can be called throughout the batch file.

```
@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
```

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


[1]:

[To see links please register here]

Reply

#9
This is probably the fastest way to convert string to upper case using batch file - it uses a [macro][1] ,it stores only the upper case letters in the loop using the way of how batch replaces strings and stores the produced string in a variable called `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%

[1]:

[To see links please register here]

Reply

#10
A general purpose translator which seems simple and able to
do the job is -

@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
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through