Lower and remove special character of file name - windows

I want to create a script, which lower and remove special character multiple text files.
my files in folder like this:
- ⚡ Document.txt
- [Review] Test File.txt
i want remove special char of filename like this
- document.txt
- review test file.txt
i've tried like this, but only lower filename. how to remove special character?
#echo off
setlocal enableDelayedExpansion
pushd %currentfolder%
for %%f in (*) do (
set "filename=%%~f"
for %%A in (a b c d e f g h i j k l m n o p q r s t u w x y z) do (
set "filename=!filename:%%A=%%A!"
)
ren "%%f" "!filename!" >nul 2>&1
)
endlocal
Before

#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\your files"
set "validchars=abcdefghijklmnopqrstuvwxyz1234567890. "
pushd "%sourcedir%"
FOR %%b IN (*) DO (
set "newname="
set "oldname=%%b"
call :validate
if /i "%%b" neq "!newname!" ren "%%~sb" "!newname!"
)
popd
GOTO :EOF
:validate
if not defined oldname goto :eof
set "c1=%oldname:~0,1%"
set "oldname=%oldname:~1%"
if "!validchars:%c1%=!" neq "%validchars%" set "newname=%newname%%c1%"
goto validate
Always verify against a test directory before applying to real data.
I predict it will have problems with some unicode characters and the usual suspects.

You could use pure powershell for this, or if you feel like continuing the use batch-file, just call powershell to assist:
#echo off
for %%i in (*) do for /f "delims=" %%a in ('powershell "$string='%%~ni';$String.tolower() -replace '[\W]', ''"') do echo ren "%%~i" "%%a%%~xi"
Note the echo at the end of the line, that is to test functionality by printing to screen before you do the actual renaming. Only remove echo when you're happy with the printed results.

Related

Edit text after dot with batch script

I want to remove three digits after dot in last 3 columns with batch file (windows). Note that dots can be present in other columns.
This is a sample of my data:
4216118,'0806010709','ljubičasti ','Hita kirška ambnta',1,'Eriiti (vk, kk)','X','Uđaj za heološke prege','Celyn1800 ','Hni Sak','Hemlogja','2016-06-08 11:42:05.040','2016-06-08 11:41:42.122','2016-06-08 11:49:49.370'
4216081,'0806010387','ljubičasti ','Oća doven.amb. - VANJA',1,'Erii (vk, kk)',,'Urj za heoške prage','Adia 120 R','Reni','Hlogija','2016-06-08 08:52:13.962','2016-06-08 08:51:57.067','2016-06-08 11:08:26.504'
4216667,'1506010909','ljčasti ','tna ambuta kke za invne bolesti',1,'Erciti (vk, kk)',,'Uj za hemloške prge','Cell-Dyn 1800 R','Hi','Hemagija','2016-06-15 21:24:14.646','2016-06-15 21:24:03.523','2016-06-15 21:26:58.871'
4213710,'0905010991','ljubičasti ','Hna kira amnta',1,'Eociti (vk, kk)','X','Uđaj za hemloške prage','Cel1800 ','Hi Sak','Hemlogja','2016-05-09 17:52:32.231','2016-05-09 17:52:26.319','2016-05-09 18:31:33.643'
Example:
Before:
'2016-06-08 11:49:49.370'
After:
'2016-06-08 11:49:49'
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q47642335.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "delims=" %%a IN ('more %filename1%') DO (
SET "line=%%a"
ECHO !line:~0,-57!!line:~-53,-31!!line:~-27,-5!!line:~-1!
)
)>"%outfile%"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q47642335.txt containing your data for my testing.
Produces the file defined as %outfile%
Sadly, cmd doesn't play nicely with unicode files, so there will be some modification to the data. Essentially, read each line and pick the line-portions to be concatenated, using - substringing values to select from the end of the line, which is of a consistent structure.
Try this, without any guaranties. Regarding your example, expect you want remove last three digits AND dot, unlike your describe to remove three digits AFTER dot.
#echo off & setlocal ENABLEDELAYEDEXPANSION > out.txt
for /f "tokens=1-15 delims=," %%a in (data.txt) do (
set "string="
call :process "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j" "%%k" "%%l" "%%m" "%%n" "%%o"
)
exit /B
:process
echo %~1| findstr /R /C:"[0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*\.[0-9][0-9][0-9]" >NUL
if not errorlevel 1 (
set "str=%~1"
set "str=!str:~0,-5!"
if defined string (set "string=!string!,!str!'") else (set "string='!str!'")
) else (
set "str=%~1"
if defined string (set "string=!string!,!str!") else (set "string='!str!'")
)
shift
if not "%~1"=="" (goto :process) else (echo !string!>>out.txt)
GOTO:EOF

Windows batch: replace strings in a file case sensitively

I've spent a lot of time writing a script to generate new files using a "template" (coding env.) project.
While on Unix the shell script is laughably easy I spent days doing the same on Windows...
My current batch file does almost everything I need, except that the string replacement is case-insensitive... That is, it replaces "emptyproject" with "EMPTYPROJECT", being the first statement...
Code:
#echo off & setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set argc=0
for %%x in (%*) do set /A argc+=1
if /I "%argc%" EQU "0" (
echo Usage: %0 PROJECT_NAME >&2
exit /B 1
)
set curr_dir=%~dp0
set project_name=%1
set project_name_lower=%1
set project_name_upper=%1
set project_file="vstudio\projects\%project_name%.vcxproj"
set project_filters="vstudio\projects\%project_name%.vcxproj.filters"
call :toupper project_name_upper
call :tolower project_name_lower
if not exist %project_file% (
copy /y "vstudio\projects\EmptyProject.vcxproj" %project_file% >nul
copy /y "vstudio\projects\EmptyProject.vcxproj.filters" %project_filters% >nul
)
set project_source_dir=..\projects\source\%project_name_lower%
if not exist %project_source_dir% (
mkdir %project_source_dir%
call :copy_and_replace_strings "..\projects\source\emptyproject\main.cpp" "%project_source_dir%\main.cpp" %project_name% %project_name_lower% %project_name_upper%
call :copy_and_replace_strings "..\projects\source\emptyproject\emptyproject.h" "%project_source_dir%\%project_name_lower%.h" %project_name% %project_name_lower% %project_name_upper%
call :copy_and_replace_strings "..\projects\source\emptyproject\emptyproject_main.cpp" "%project_source_dir%\%project_name_lower%_main.cpp" %project_name% %project_name_lower% %project_name_upper%
call :copy_and_replace_strings "..\projects\source\emptyproject\emptyproject_eventhandlers.cpp" "%project_source_dir%\%project_name_lower%_eventhandlers.cpp" %project_name% %project_name_lower% %project_name_upper%
)
endlocal
goto :EOF
REM functions
:toupper
for %%L 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:%%L=%%L!
goto :EOF
:tolower
for %%L 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:%%L=%%L!
goto :EOF
:copy_and_replace_strings <source_file> <target_file> <name> <lower> <upper>
for /f "tokens=1,* delims=]" %%a in ('find /n /v "" ^< %1') do (
set token1=%%a
set token2=%%b
if defined token2 (
REM not case sensitive
set token2=!token2:EMPTYPROJECT=%5!
set token2=!token2:EmptyProject=%3!
set token2=!token2:emptyproject=%4!
echo !token2!>>%2
) else (
echo[>>%2
)
)
goto :EOF
I read a lot about this on this forum, but couldn't find a suitable solution. Could someone help me please?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%o IN (EMPTYPROJECT EmptyProject emptyproject EmPtYpRoJeCt) DO (
SET "token2=A line containing %%o in it"
CALL :magic
ECHO !token2! -^> !result!
)
GOTO :EOF
:magic
SET "result=!token2!"
ECHO !token2!|FINDSTR "EMPTYPROJECT" >NUL
IF NOT ERRORLEVEL 1 set "result=!token2:EMPTYPROJECT=capiTaLs!"
ECHO !token2!|FINDSTR "EmptyProject" >NUL
IF NOT ERRORLEVEL 1 set "result=!token2:EMPTYPROJECT=Mixed!"
ECHO !token2!|FINDSTR "emptyproject" >NUL
IF NOT ERRORLEVEL 1 set "result=!token2:EMPTYPROJECT=lower!"
GOTO :eof
Perhaps this will be of assistance.
To be rather honest, your quickest method to keep it pure batch is to trick the system and use powershell in the background.
#echo off
setlocal enabledelayedexpansion
set "input=i want this upper case"
for /f "usebackq delims=" %%a in (`powershell "\"%input%\".toUpper()"`) do set "lower=%%~a"
set "input=I WANT THIS LOWER CASE"
for /f "usebackq delims=" %%a in (`powershell "\"%input%\".toLower()"`) do set "upper=%%~a"
echo !lower!
echo !upper!
RESULT
I WANT THIS UPPER CASE
i want this lower case

BAT file - replace multiple characters in filename

Couldn't find an answer that wasn't very specific to someone else's problem.
I'd like to place a bat file in a directory and run it to achieve the following:
Replace all initial '-' (hyphen) with ' - ' (space-hyphen-space)
Replace any 3 char Month names (Jan,Feb,...Dec) with two-digit month number preceeded and followed by a hyphen ('Jan' = '-01-' , 'Mar' = '-03-')
So the following:
32432492-2015Jan23-2015Feb23.pdf
32432492-2015Feb24-2015Mar24.pdf
32432492-2015Mar25-2015Apr29.pdf
becomes:
32432492 - 2015-01-23 - 2015-02-23.pdf
32432492 - 2015-02-24 - 2015-03-24.pdf
32432492 - 2015-03-25 - 2015-04-29.pdf
I'd like the "rename" to only run once (instead of renaming all files over and over). It should do this for all files in current directory (except the current bat file of course).
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*-*" '
) DO (
SET "newname=%%a"
FOR %%b IN ("Jan=-01-" "Feb=-02-" "Mar=-03-" ) DO SET "newname=!newname:%%~b!"
FOR /f "tokens=1*delims=-" %%b IN ("!newname!") DO SET "newname=%%b - %%c"
IF /i NOT "!newname!"=="%%a" ECHO(REN "%sourcedir%\%%a" "!newname!"
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I expect that you would have the sense to complete the month/number set in the form given.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
Revision
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*-*" '
) DO (
SET "newname=%%a"
FOR %%b IN ("Jan=/01/" "Feb=/02/" "Mar=/03/" ) DO SET "newname=!newname:%%~b!"
FOR /f "tokens=1,2*delims=-" %%b IN ("!newname!") DO SET "newname=%%b - %%c - %%c"
IF /i NOT "!newname!"=="%%a" ECHO REN "%sourcedir%\%%a" "!newname:/=-!"
)
GOTO :EOF
You said "Replace all initial '-' (hyphen) with ' - ' (space-hyphen-space)" which I took to mean "the initial hyphen in the name".

Windows batch file to find duplicates in a tree

I need a batch file ( Windows CMD is the interpreter, a .bat ) to do this type of task:
1) Search through a folder and its subfolders
2) Find files with the same filename and extension ( aka duplicates )
3) Check if they have the same size
4) If same name + same size, echo all the files except the first one ( practically I need to delete all except one copy )
Thanks for any type of help
This is only an initial script, just for check the files, in a folder and its subfolders, and their size:
#Echo off
Setlocal EnableDelayedExpansion
Set Dir=C:\NewFolder
For /r "%Dir%" %%i in (*) do (
Set FileName=%%~nxi
Set FullPath=%%i
Set Size=%%~zi
Echo "!FullPath!" - SIZE: !Size!
)
Echo.
Pause
This script does what you ask. Just set the ROOT variable at the top to point to the root of your tree.
#echo off
setlocal disableDelayedExpansion
set root="c:\test"
set "prevTest=none"
set "prevFile=none"
for /f "tokens=1-3 delims=:" %%A in (
'"(for /r "%root%" %%F in (*) do #echo %%~znxF:%%~fF:)|sort"'
) do (
set "currTest=%%A"
set "currFile=%%B:%%C"
setlocal enableDelayedExpansion
if !currTest! equ !prevTest! echo "!currFile!"
endlocal
set "prevTest=%%A"
)
But you can make the test more precise by using FC to compare the contents of the files. Also, you can incorporate the DEL command directly in the script. The script below prints out the commands that would delete the duplicate files. Remove the ECHO before the DEL command when you are ready to actually delete the files.
#echo off
setlocal disableDelayedExpansion
set root="c:\test"
set "prevTest=none"
set "prevFile=none"
for /f "tokens=1-3 delims=:" %%A in (
'"(for /r "%root%" %%F in (*) do #echo %%~znxF:%%~fF:)|sort"'
) do (
set "currTest=%%A"
set "currFile=%%B:%%C"
setlocal enableDelayedExpansion
set "match="
if !currTest! equ !prevTest! fc /b "!prevFile!" "!currFile!" >nul && set match=1
if defined match (
echo del "!currFile!"
endlocal
) else (
endlocal
set "prevTest=%%A"
set "prevFile=%%B:%%C"
)
)
Both sets of code may seem overly complicated, but it is only because I have structured the code to be robust and avoid problems that can plague simple solutions. For example, ! in file names can cause problems with FOR variables if delayed expansion is enabled, and = in file name causes a problem with npocmoka's solution.
#echo off
setlocal
for /f "tokens=1 delims==" %%# in ('set _') do (
set "%%#="
)
for /r %%a in (*.*) do (
if not defined _%%~nxa%%~za (
set "_%%~nxa%%~za=%%~fa"
) else (
echo %%~fa
)
)
endlocal

How to convert the value of %USERNAME% to lowercase within a Windows batch script?

I'm automating some source control software functionality using a dot bat script but given that our svn repos are hosted in a *NIX box, I'm facing the eternal case problem between these two worlds.
Is there any cmd.exe function to convert the value of the Windows system variable %USERNAME% to lower case?
Thanks much in advance!
Well, I was browsing for some syntax and stumbled upon this page. I know its old but I thought I'd take a break and give the brain a little kick.
Here's something a little shorter and manageable. This just "brute forces" all uppercase letters to lowercase letters without regards to whether the actual letter exists in the string or not. Thus the functional loop runs exactly 26 times no matter the length of the string.
Hope this helps someone.
#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
Example:
E:\OS.ADMIN>LCASE.BAT The Quick Fox Jumps Over The Brown Fence.
Result:
_STRING=The Quick Fox Jumps Over The Brown Fence.
_STRING=the quick fox jumps over the brown fence.
a quick google found this...
#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
add that as a file (lowercase.cmd) to your path and you should be able to call it as "Lowercase.cmd %Username%", you could pipe it into another command if needed.
download some unix utilities for DOS from http://short.stop.home.att.net/freesoft/unix.htm
and use tr.exe (translate characters)
echo %USERNAME% | tr "[A-Z]" "[a-z]"
I also use a DOS extended cmd replacement named 4NT which has a built in command #lower
echo %#lower[%USERNAME%]
http://www.dzone.com/snippets/lowercasing-string-bat-files
lower.bat
echo>%1
dir /b/l %1>lower.tmp
set /p result=<lower.tmp
echo %result%
cmd
lower "Mein BinnenMajuskel"
result
mein binnenmajuskel
CAUTION: Quick & dirty, but also insecure and dangerous variant. Because you create two files. One called like the given string and another called lower.tmp, which contains the lowered string. What happens if you execute lower "UserName" in a directory, where this file or directory already exists? Especially if you delete this files afterwards ...
Improved version:
echo>%Temp%\%1
dir /b/l %Temp%\%1>%Temp%\lower.tmp
set /p result=<%Temp%\lower.tmp
del %Temp%\%1
del %Temp%\lower.tmp
When a scripting language is installed then that can be used with a FOR to set a variable.
#FOR /F "delims=" %%s IN ('<<some script oneliner>>') DO #set MYVARIABLE=%%s
Reference: For F Loop
Any scripting language can be used if it can convert a string to lowercase and output the result.
An example using Perl 5 :
#FOR /F "delims=" %%s IN ('perl -e "print lc(pop)" %USERNAME%') DO #set USERNAME=%%s
An example using PowerShell :
#FOR /F "delims=" %%s IN ('powershell -command "(get-item env:'USERNAME').Value.ToLower()"') DO #set USERNAME=%%s
These days, odds are that PowerShell is already installed by default.
In my batch file I'm doing a comparsion between %USERNAME% and a CSV file.
The program would not work if user was logged in UperCase username.
Ex:
Login : GB2NOGU // Won't work
Login : gb2nogu // Works
Here I could solve my problem doing a insensitive comparison.
if /i %USERNAME%==gb2nogu (
// Code here
)
The parameter /i tells the cmd to do a insensitive case comparison, so it'll ignore the difference between lowercase and uppercase letters.
Probably this is the fastest way to convert a string to lowercase in batch file as it uses macro and there are no temp files (it saves the produced string in variable called 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%
:: 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
This is the same answer /by #It Wasn't Me here
For a predictive character set, this substring Set !var:A=a! works, and only working with predefined substring in bat/cmd.
For this type of task, why not get a little help with c#, which can make it possible to work with unconventional accents and consonants è, È, ä, Ä, ñ, Ñ, ç, Ç etc.
Where the bat/cmd will generate c# code, it will be compiled and executed at run time ....
Which solves possible user inputs, in which the sequence comes with accents and the vowels/consonants are different from the conventional ones [a-z] and/or [A_Z]
#echo off & setlocal EnableDelayedExpansion
cd /d "%~dp0" && title <nul && title ...\%~dpnx0 /// !time:~0,8! !date!
if exist "%tmp%\ToUpLower.cs" 2>nul >nul del /q /f "%tmp%\ToUpLower.cs"
set "_where=%__appdir__%where.exe" && set "_csc=%windir%\Microsoft.NET"
>"%temp%\ToUpLower.cs" (
echo= using System; namespace SUQ1522019 ^{class Program ^{static void Main(string[] args^) ^{
echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-l"^) ^{Console.WriteLine(args[1].ToLower(^)^);^}
echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-u"^) ^{Console.WriteLine(args[1].ToUpper(^)^);^}^}^}^}
)
set "_arg=/t:exe /out:"%tmp%\ToUpLower.exe" "%tmp%\ToUpLower.cs" /platform:anycpu "
for /f tokens^=* %%i in ('!_where! /r "!_csc!" "csc.exe"^|findstr /lic:"k\v2\."
')do "%%~i" !_arg! /unsafe+ /w:0 /o /nologo
for /f tokens^=* %%U in ('"%tmp%\ToUpLower.exe" -u %USERNAME%')do set "_up_case=%%U"
for /f tokens^=* %%l in ('"%tmp%\ToUpLower.exe" -l %USERNAME%')do set "_low_case=%%l"
echo/ Your username upcase is: !_up_case!
echo/ Your username lowcase is: !_low_case!
echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "."
del /q /f "%tmp%\ToUpLower.*" >nul 2>nul && endlocal & goto :EOF
Outputs for %USERNAME%
Your username upcase is: USERNAME
Your username lowcase is: username
The ToUpLower.cs c# code with no escaping:
using System; namespace SUQ1522019 {class Program {static void Main(string[] args) {
if (args.Length==2 && args[0].ToLower()=="-l") {Console.WriteLine(args[1].ToLower());}
if (args.Length==2 && args[0].ToLower()=="-u") {Console.WriteLine(args[1].ToUpper());}}}}
The ToUpLower.cs c# code with no escaping and indented:
using System
namespace SUQ1522019
{
class Program
{
static void Main(string[] args)
{
if (args.Length==2 && args[0].ToLower()=="-l")
{
Console.WriteLine(args[1].ToLower());
}
if (args.Length==2 && args[0].ToLower()=="-u")
{
Console.WriteLine(args[1].ToUpper());
}
}
}
}
This c# code was compiled/tested on csc.exe versions:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
c:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
This is the command line used to compile the c# code:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:"%tmp%\ToUpLower.exe" "%tmp%\ToUpLower.cs" /platform:anycpu /unsafe+ /w:0 /o /nologo
ToUpLower.exe usage UPPER to -> lower
ToUpLower.exe -l STRING
:: or ..
ToUpLower.exe -L STRING
ToUpLower.exe usage lower to -> UPPER
ToUpLower.exe -u string
:: or ..
ToUpLower.exe -U string
To keep ToUpLower.exe, remove echo/ from copy command:
echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "."
This command line will copy ToUpLower.exe from %temp% to same the same folder where your bat is running.
Programming Guide C#:
Args
ToLower
ToUpper
Sorry my limited English

Resources