Computing.Net > Forums > Programming > Batch - Edit Variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch - Edit Variable

Reply to Message Icon

Name: astroraptor
Date: August 22, 2008 at 20:26:32 Pacific
OS: Windows XP Pro
CPU/Ram: 2.33GHz/3072MB
Product: Lenovo ThinkCentre
Comment:

Is it possible to take a variable with a numerical value for instance and add a comma every 3 characters moving backward? How could this be done?

ie: "20000" would change to "20,000"



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: August 23, 2008 at 21:16:58 Pacific
Reply:

A really handy command for this type of thing is Environment variable substitution. Basically you can use it to carve up your variables.

You specify it as your variable(what ever it may be) followed by a colon, then a ~, then a number then possibly a comma then another number "%yourvariable:~x,x%"

It can be used as follows:
%yourvariable:~1% - This would be all of your variable starting from character 2, as the first character is :~0 and if you don't specify a ending value the remainder of the variable is assumed.

%yourvariable:~1,6% - This would be all of yourvariable starting from character 2, ending at character 7.

%yourvariable:~0,-3% - This would be all of yourvariable starting from the first character, excluding the last three characters.

%yourvariable:~-3% - This would be the last three characters of your variable.

for more information go to the command line and type "set /?" (I think it will be around the third page)


As for your problem, the below seemed to work for me though it's a bit of a quick job, so I can't say it's error free or optimal. You may want to clean it up if you actually use it. Also it will work with letters as well as numbers.
[edit fixed 3 or less characters, twice]


set count=0
set yourvariable=20000
set tempvariable=%yourvariable%
set splitvariable=%yourvariable%
::your variables length
:length
if not defined tempvariable goto nextthing
set /a count+=1
set tempvariable=%tempvariable:~1%
goto length

::checks we have enough characters
::or any at all
:nextthing
if %count% == 0 goto nothg
if %count% leq 3 goto lessthan3
goto splitter

::Set the last 3 characters of splitvariable,
::a comma and whatever is already in
::outputvar into outputvar.
::taking the last three characters off
::splitvariable, then taking three
::off the count, if the count becomes less
::than three then it jumps to output
:splitter
if %count% lss 3 goto output
set outputvar=%splitvariable:~-3%,%outputvar%
set splitvariable=%splitvariable:~0,-3%
set /a count-=3
goto splitter

::If our initial number of characters
::isn't divisable
::buy three then splitvariable
::still holds one or two digits
::setting it to its self and a comma
::at the end finishes the job.
::outputvar has an extra comma at the end
::we don't want, so we get rid of it.
:output
if defined splitvariable set splitvariable=%splitvariable%,
echo %splitvariable%%outputvar:~0,-1%
pause
goto end
:lessthan3
echo %yourvariable%
pause
goto end
:nothg
Echo yourvariable was not set
pause
:end
set count=
set yourvariable=
set tempvariable=
set splitvariable=
set outputvar=


0

Response Number 2
Name: astroraptor
Date: August 24, 2008 at 06:19:48 Pacific
Reply:

Wow, might be a little difficult to implement but that's alright! Thanks alot!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch - Edit Variable

Batch file variable creation www.computing.net/answers/programming/batch-file-variable-creation/14564.html

BATCH using variables www.computing.net/answers/programming/batch-using-variables/15611.html

Batch: replace variable w/ filename www.computing.net/answers/programming/batch-replace-variable-w-filename/15512.html