Finding Characters Within a Variable [solved]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Finding Characters Within a Variable [solved]

#1 Post by alleypuppy » 12 Sep 2011 22:14

Hi all,

I need help with finding characters within a variable.

I need to identify if the user typed a network path (\\[server]\[sharename]) or a drive and path (C:\[folder]). Basically I need the file to determine if the variable that was set through the SET /P command (user input) contains "\\" or not, and based on this, the file will do different commands. I'm thinking I need to use a FOR /F loop, but I'm not entirely sure how to do this.

Any help will be greatly appreciated!

Thanks!
Last edited by alleypuppy on 13 Sep 2011 15:58, edited 1 time in total.

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: Finding Characters Within a Variable

#2 Post by Bob D » 13 Sep 2011 02:03

If the character(s) are in a known position in the variable you can use the substring function of the set command. Follows is a partial copy out of the Help for Set.
May also specify substrings for an expansion.

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result. If the length is not specified, then it defaults to the
remainder of the variable value. If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

%PATH:~-10%

would extract the last 10 characters of the PATH variable.

%PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

The quote uses the path variable but just use any variable of your choice.

Code: Select all

 
set var1=abc\\def
set var2=%var1:~3,2%
if %var2% EQU \\ (echo \\ found) else (echo no \\ found)

No help with the FOR command. I'm still coming to grips with that one.

Bob

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Finding Characters Within a Variable

#3 Post by dbenham » 13 Sep 2011 11:24

If you want to determine if a substring exists anywhere within the string then you can simply do a search and replace on the string:
%var:searchString=replaceString%.

Search for the substring and replace it with nothing. If the result differs from the original string then the substring exists within the original string.

Code: Select all

if %var:\\=% neq %var% (echo found \\ within %var%) else (echo \\ not found in %var%)


Dave Benham

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Finding Characters Within a Variable

#4 Post by alleypuppy » 13 Sep 2011 15:58

Bob D wrote:If the character(s) are in a known position in the variable you can use the substring function of the set command. Follows is a partial copy out of the Help for Set.
May also specify substrings for an expansion.

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result. If the length is not specified, then it defaults to the
remainder of the variable value. If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

%PATH:~-10%

would extract the last 10 characters of the PATH variable.

%PATH:~0,-2%

would extract all but the last 2 characters of the PATH variable.

The quote uses the path variable but just use any variable of your choice.

Code: Select all

 
set var1=abc\\def
set var2=%var1:~3,2%
if %var2% EQU \\ (echo \\ found) else (echo no \\ found)

No help with the FOR command. I'm still coming to grips with that one.

Bob

Thanks man! That's exactly what I'm looking for! I completely forgot you could do that lol

Bob D
Posts: 20
Joined: 07 Sep 2011 18:32
Location: Eastern Australia

Re: Finding Characters Within a Variable [solved]

#5 Post by Bob D » 13 Sep 2011 16:44

In this case it will work because your target is at a fixed location but I like Dave Benham's solution better. It is more robust in that if the target is not where you thought it was then it still works.

Post Reply