String manipulation using environment variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aijelt
Posts: 3
Joined: 03 Apr 2014 10:32

String manipulation using environment variables

#1 Post by aijelt » 03 Apr 2014 10:42

On this excellent site I've learned how to manipulate strings in DOS. A method that has been helping me a lot is the 'MID' equivalent:

set short_string=%long_string:~0,5%

However, now I don't want a fixed number of characters (the 5 in this case) but I want this value to be calculated:

set chars=5

So I thought it would be doable like this:

set short_string=%long_string:~0,!chars!%
or
set short_string=%long_string:~0,%%chars%%%

Well, neither doesn't work. Naturally. So the question here is, can this be done? If so, where am I going wrong.

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: String manipulation using environment variables

#2 Post by Matt Williamson » 03 Apr 2014 11:40

You just need a Call before set.

Code: Select all

@echo off
setlocal enabledelayedexpansion

set "long_string=The quick brown fox jumped over the lazy dog"
set chars=5
call set short_string=%%long_string:~0,%chars%%%
Echo %short_string%

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: String manipulation using environment variables

#3 Post by foxidrive » 03 Apr 2014 11:45

This is one way:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set string=abcdefghijkl
for /L %%a in (1,1,10) do echo !string:~0,%%a!
pause


Or

Code: Select all

set short_string=!long_string:~0,%chars%!


Or without delayed expansion

Code: Select all

call set short_string=%%long_string:~0,%chars%%%

aijelt
Posts: 3
Joined: 03 Apr 2014 10:32

Re: String manipulation using environment variables

#4 Post by aijelt » 03 Apr 2014 15:08

Thanks guys!

You rock.

The ideas you posted I had tried before in many versions. Now, I pasted the code you guys put up here in my batch file. Again, your code works, mine didn't. What went wrong?

Well... it's all in the details. It with great shame I have to admit that I forgot one % sign in my code.

This does not work:
call set combi=%%lastname:~0,%chars%%
This does work:
call set combi=%%lastname:~0,%chars%%%

So if you please excuse me, I will go hide underneath a rock for a while and be ashamed for a while.

As I feel I'll have to share the final solution I will first finish the batch file and post it here. What it is supposed to do is to create some kind of user code made up by the first name and last name. The code should be 8 characters long, first 4 of the last-name, first 4 of the first-name unless either of them is shorter than 4 characters and that is where the fun begins.

So, John Cooper will be COOPJOHN, Ben Former will be FORMEBEN, and Lillian Do will be DOLILLIA.

aijelt
Posts: 3
Joined: 03 Apr 2014 10:32

Re: String manipulation using environment variables

#5 Post by aijelt » 03 Apr 2014 15:37

Here it is, the finished code. That is... The first-name and last-name are set using a different piece of batch-file but this is working. I've left the strLen function out as it can be found here rather easy.

cls
@echo off
set usg_firstname=Jon
set usg_lastname=Williams
call :strlen usg_firstname usg_flen
call :strlen usg_lastname usg_llen

if 4 GTR %usg_flen% (
set /a usg_fshort=4-%usg_flen%
) else (
set usg_fshort=0
)
set /a usg_fchrs=4+usg_fshort
echo.
if 4 GTR %usg_llen% (
set /a usg_lshort=4-%usg_llen%
) else (
set usg_lshort=0
)
set /a usg_lchrs=4+usg_lshort

set chars=%usg_lshort%
call set combi=%%usg_lastname:~0,%usg_fchrs%%%%%usg_firstname:~0,%usg_lchrs%%%

echo %combi%

Post Reply