HØLLØW Posted February 6, 2010 Posted February 6, 2010 Hey guys,I need your help.I have to create a batch-file that saves the first and the last character of the %computername% in a variable.I hope anyone can help.Thanks
MrJinje Posted February 6, 2010 Posted February 6, 2010 I guess you could use the left string / right string batch equivalents.set VAR=%COMPUTERNAME:~0,1%%COMPUTERNAME:~-1%echo %VAR%
HØLLØW Posted February 6, 2010 Author Posted February 6, 2010 (edited) Hi MrJinje,soooo easy... B) thank you, it works fine. Edited February 6, 2010 by HØLLØW
HØLLØW Posted February 7, 2010 Author Posted February 7, 2010 I've got another little question:Is it possible to change upper-case letters to lower-case letters via batch?Thank you
Yzöwl Posted February 7, 2010 Posted February 7, 2010 Yes it is; however the solution could be relatively simple or particularly awkward depending upon what you are specifically working with.Could you pease provide more information with specifics regarding the data you are wishing to convert.
MrJinje Posted February 7, 2010 Posted February 7, 2010 If you are on Windows 7, Powershell has a built-in method called "ToLower" for this purpose. $a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"$a = $a.ToLower()$ahttp://technet.microsoft.com/en-us/library/ee176893.aspx
jaclaz Posted February 7, 2010 Posted February 7, 2010 A "plain" batch "Capitalize" routine is here:http://web.archive.org/web/*/http://www.fp...q.faqid+262.htm(Wayback machine seems like having some problems right now )Here is a simplified version:@ECHO OFFSET String_in=%1CALL :Capitalize %String_in%SET String_outGOTO :EOF:Capitalize:: Capitalize:: This routine has been published by Frank-Peter Schultze:: [url="http://www.fpschultze.de/smartfaq+faq.faqid+262.htm"]http://www.fpschultze.de/smartfaq+faq.faqid+262.htm[/url]:: and slightly simplified by meSet s=%1For %%A In ( aA bB cC dD eE fF gG hH iI jJ kK lL mM nN oO pP qQ rR sS tT uU vV wW xX yY zZ) Do Call :UCase %%ASET String_out=%s%GOTO :EOF:UCaseSet _=%1Call Set s=%%s:%_:~0,1%=%_:~1,1%%%Goto :EOF(no spaces)jaclaz
Yzöwl Posted February 7, 2010 Posted February 7, 2010 I'd offer a similar generic routine, which allows for a user provided switch for conversion to Upper or Lower case.chg_case.cmd@ECHO OFF&SETLOCALIF %1' EQU ' GOTO :EOFSET VAR=%*FOR %%# IN (/ -) DO IF %VAR:~,1% EQU %%# (FOR %%$ IN ( U L) DO IF /I %VAR:~1,1% EQU %%$ (SET "YC=%%$"&&( CALL SET VAR=%%VAR:%VAR:~0,3%=%%)))IF NOT DEFINED YC (SET YC=L)SET "LC_=abcdefghijklmnopqrstuvwxyz"SET "UC_=ABCDEFGHIJKLMNOPQRSTUVWXYZ"FOR /L %%? IN (0,1,25) DO CALL :CC %%LC_:~%%?,1%% %%UC_:~%%?,1%%ECHO=[%VAR%]GOTO :EOF:CCIF /I %YC% NEQ U (CALL SET "VAR=%%VAR:%2=%1%%") ELSE ( CALL SET "VAR=%%VAR:%1=%2%%")This allows you to pass your string to be converted to the script following your choice of case. If a choice of case isn't provided then all to lower case will be the default.E.g. String To convert: TeSt StRinG = "I'm A StaR!"Microsoft Windows [Version 6.0.6002]Copyright © 2006 Microsoft Corporation. All rights reserved.C:\Users\Yzöwl>Desktop\chg_case.cmd TeSt StRinG = "I'm A StaR!"[test string = "i'm a star!"]C:\Users\Yzöwl>Desktop\chg_case.cmd /u TeSt StRinG = "I'm A StaR!"[TEST STRING = "I'M A STAR!"]C:\Users\Yzöwl>Desktop\chg_case.cmd /l TeSt StRinG = "I'm A StaR!"[test string = "i'm a star!"]C:\Users\Yzöwl>Desktop\chg_case.cmd -u TeSt StRinG = "I'm A StaR!"[TEST STRING = "I'M A STAR!"]C:\Users\Yzöwl>Desktop\chg_case.cmd -l TeSt StRinG = "I'm A StaR!"[test string = "i'm a star!"]C:\Users\Yzöwl>You can see that your choice of case can be applied using a forward slash or a hyphen and can itself be upper or lower case. You may also note that you are not required to enclose your string in double-quotes.
gunsmokingman Posted February 7, 2010 Posted February 7, 2010 Now I know you did not ask for a VBS Script, but here is what you ask for done as a VBS script.Save as a vbs scriptDim Act :Set Act = CreateObject("Wscript.Shell")Dim Cn :Cn = Act.ExpandEnvironmentStrings("%ComputerName%") MsgBox "Demo Upper Case " & UCase(Left(Cn,1) & Right(Cn,1)) & vbCrLf & _ "Demo Lower Case " & LCase(Left(Cn,1) & Right(Cn,1)),4128,"String Manipulation Demo"
HØLLØW Posted February 7, 2010 Author Posted February 7, 2010 Hi there,I'm searching for a solution to convert my string, saved in the variable %VAR% to lower-case characters, because it's always upper-case:I guess you could use the left string / right string batch equivalents.set VAR=%COMPUTERNAME:~0,1%%COMPUTERNAME:~-1%echo %VAR%Thank you
MrJinje Posted February 7, 2010 Posted February 7, 2010 You will have to edit the samples on this page to fit your needs.http://www.robvanderwoude.com/battech_convertcase.php
Yzöwl Posted February 7, 2010 Posted February 7, 2010 I suppose you could just use this:ChCaseCN.cmd@ECHO OFF&SETLOCALSET VAR=%COMPUTERNAME:~0,1%%COMPUTERNAME:~-1%PUSHD %TEMP%TYPE NUL>%VAR%.yzlFOR /F "DELIMS=." %%# IN ('DIR/B/L %VAR%.yzl') DO SET VAR=%%#DEL %VAR%.yzlECHO=%%VAR%%=%VAR%POPD&PAUSE
HØLLØW Posted February 10, 2010 Author Posted February 10, 2010 (edited) Hi there,thank you Yzöwl, it's working perfectly for me. Edited February 10, 2010 by HØLLØW
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now