Page 1 of 1
Finding Characters Within a Variable [solved]
Posted: 12 Sep 2011 22:14
by alleypuppy
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!
Re: Finding Characters Within a Variable
Posted: 13 Sep 2011 02:03
by Bob D
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
Re: Finding Characters Within a Variable
Posted: 13 Sep 2011 11:24
by dbenham
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
Re: Finding Characters Within a Variable
Posted: 13 Sep 2011 15:58
by alleypuppy
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
Re: Finding Characters Within a Variable [solved]
Posted: 13 Sep 2011 16:44
by Bob D
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.