Assistance required with Advanced SET syntax

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

Assistance required with Advanced SET syntax

#1 Post by sherlock99 » 08 Oct 2010 08:25

Hi all,

hope someone can help. I need help on string manipulation using the SET command. I found the link http://www.dostips.com/DtTipsStringManipulation.php which has been very useful but lacks the exact script I'm after.

Quite simply, I want to be able to strip away all but the last 3 characters of an environment variable. So for example value=12345, would be changed to 345. It is not sufficient to simply delete the first 2 characters from the beginning because the variable length may not always be 5 characters. So, for example value=123456 would return 3456 instead of 456

In addition I would be very interested in learning how to use all of the advanced, extended characters for scripting with SET, FOR, etc so if anyone has any good links, that too would be greatly appreciated. That way I don't have to keep asking basic questions.

Regards,

Sherlock99.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Assistance required with Advanced SET syntax

#2 Post by ghostmachine4 » 08 Oct 2010 08:41

my $0.02,
you should use a good string manipulation tool such as sed for windows

Code: Select all

C:\test>echo 12345|sed "s/...$//"
12
C:\test>echo 12345|sed -r "s/.*(...)$/\1/"
345


Its a one time download only. However if you hate downloading stuff, you can use native vbscript which has better string manipulation capabilities/functions (eg Trim, split(), replace() etc) than batch.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Assistance required with Advanced SET syntax

#3 Post by aGerman » 08 Oct 2010 15:21

sherlock99 wrote:Quite simply, I want to be able to strip away all but the last 3 characters of an environment variable. So for example value=12345, would be changed to 345. It is not sufficient to simply delete the first 2 characters from the beginning because the variable length may not always be 5 characters. So, for example value=123456 would return 3456 instead of 456

Try

Code: Select all

@echo off
set "value=12345"
set "LastThree=%value:~-3%"
echo %LastThree%
pause


sherlock99 wrote:In addition I would be very interested in learning how to use all of the advanced, extended characters for scripting with SET, FOR, etc so if anyone has any good links, that too would be greatly appreciated. That way I don't have to keep asking basic questions.

Do you mean the basic commands and their options?
http://www.dostips.com/DosCommandIndex.php

Regards
aGerman

sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

Re: Assistance required with Advanced SET syntax

#4 Post by sherlock99 » 09 Oct 2010 05:31

Thanks for the reply ghostmachine4. I'd heard of sed for windows but I was trying to keep it in the Dos only domain so my batch file could be easiliy handed out without the requirement of third party tools .

aGerman, that is exactly what I'm after. Many thanks. With regards the self-training, I was wondering of there were any good web pages that explained in more depth the full power of the available expressions, by which I mean:

() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator

I often have to throw together a Dos batch file for string manipulation. I might have a text file with complicated variations (quotes, variable length lines. etc) from which I want to run a Dos batch file to quickly extract information from all lines. If I had a better understanding of the use of ~,!+-& etc and where to use them it would save me bothing the likes of yourself with such basic questions.

On this occasion I'm just using the SET command but I'd like to learn how to string together multiple commands with pipes and use expressions more powerfully. I know I'm 20 years late learning Dos expressions but it's never too late to learn.

Thanks again.

Regards,

Sherlock99.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Assistance required with Advanced SET syntax

#5 Post by ghostmachine4 » 09 Oct 2010 08:39

sherlock99 wrote:Thanks for the reply ghostmachine4. I'd heard of sed for windows but I was trying to keep it in the Dos only domain so my batch file could be easiliy handed out without the requirement of third party tools .

a lot of people to whom i suggested using the tool always have the impression that they have to download it everytime. NO. You only download it once. Its only a .exe file which you can bring anywhere to be "handed out". Just distribute it together with your batch file.

I know I'm 20 years late learning Dos expressions but it's never too late to learn.

you should already learn something else. If you are inclined to native Windows tools, learn vbscript or powershell. they are much more powerful than batch and provides functions and capabilities that you would learn in any programming class. Forget about batch.

Vbscript example:

Code: Select all

strString= WScript.Arguments(0)
strNum= WScript.Arguments(1)
WScript.Echo Right(strString,strNum)


usage:

Code: Select all

C:\test>cscript //nologo test.vbs 12345 3
345

C:\test>cscript //nologo test.vbs 1234567 4
4567

sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

Re: Assistance required with Advanced SET syntax

#6 Post by sherlock99 » 10 Oct 2010 01:46

I know Ghostmachine4. I've done some VB scripting. As I grew up with COBOL, PASCAL and Dos, I guess I just like the challenge of squeezing every last feature out of Dos before its consigned to the floppy disks of history !!!. I know what you are saying though. Time to move on. Thanks for your replies.

Regards to all.

Sherlock99.

sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

Re: Assistance required with Advanced SET syntax

#7 Post by sherlock99 » 12 Oct 2010 01:58

Hello again.
I knew I'd end up asking another question (hence my quest for self-training). My previous question revolved around capturing a specific set of characters. I would also like to know the most efficient way of capturing a substring. This is an example string:

create volume drives=(21,8 21,9 21,10 21,11 21,12 21,13 21,14 21,15) raidLevel=5 userLabel="vg15-test" owner=B segmentSize=128 capacity=1073741824 Bytes;

I am trying to extract the number that follows the "capacity=" part of the script

Facts:
the string varies in length and varies in the number of substrings so I cant use the FOR command with tokens to pick it out
the number varies in length so I cannot pick that out either
the " Bytes;" that follows the number is constant
the "capacity=" part of the string is constant

My thought was to strip off the " Bytes;", capture the last substring in its entirity and then strip off the preceeding "capacity=". Is this possible or is there a more efficent way of grabbing a value from a string that is variable?.

Regards,

sherlock99.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Assistance required with Advanced SET syntax

#8 Post by amel27 » 12 Oct 2010 02:23

Code: Select all

@set var=create volume drives=(21,8 21,9 21,10 21,11 21,12 21,13 21,14 21,15) raidLevel=5 userLabel="vg15-test" owner=B segmentSize=128 capacity=1073741824 Bytes
@for /f "tokens=1 delims== " %%i in ("%var:*capacity=%") do @set capacity=%%i

@echo capacity: %capacity%
@pause>nul

sherlock99
Posts: 8
Joined: 08 Oct 2010 08:07

Re: Assistance required with Advanced SET syntax

#9 Post by sherlock99 » 12 Oct 2010 03:57

Thanks a lot amel27, that works great. My batch file is now significantly faster.

Regards,

sherlock99.

Post Reply