Jump to content

Recommended Posts

Posted

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


Posted

I guess you could use the left string / right string batch equivalents.

set VAR=%COMPUTERNAME:~0,1%%COMPUTERNAME:~-1%

echo %VAR%

Posted

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.

Posted

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 OFF
SET String_in=%1
CALL :Capitalize %String_in%
SET String_out
GOTO :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 me

Set s=%1

For %%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 %%A

SET String_out=%s%
GOTO :EOF

:UCase
Set _=%1
Call Set s=%%s:%_:~0,1%=%_:~1,1%%%
Goto :EOF

(no spaces)

jaclaz

Posted

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&SETLOCAL
IF %1' EQU ' GOTO :EOF
SET 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
:CC
IF /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.
Posted

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 script

Dim 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"

Posted

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

Posted

I suppose you could just use this:

ChCaseCN.cmd

@ECHO OFF&SETLOCAL
SET VAR=%COMPUTERNAME:~0,1%%COMPUTERNAME:~-1%
PUSHD %TEMP%
TYPE NUL>%VAR%.yzl
FOR /F "DELIMS=." %%# IN ('DIR/B/L %VAR%.yzl') DO SET VAR=%%#
DEL %VAR%.yzl
ECHO=%%VAR%%=%VAR%
POPD&PAUSE

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...