[Help] BAT Scripting a variable

Forum for anything else which doesn't fit in the above forums. Site feedback, random talk, whatever, are welcome.
Post Reply
User avatar
twig123
Posts: 105
Joined: Tue Jan 17, 2006 12:19 am

[Help] BAT Scripting a variable

Post by twig123 » Thu May 22, 2008 11:12 am

Hi All,

Figured that I would shoot out a cry for help.
I need to make a bat script that will
* run the "set l" (L) command
* from the results of the command I need to pull only the 3rd,4th & 5th characters and assign it to a variable.
* It will then need to compare those 3 letters against a list that I have and map a drive to a specific server.

I mostly only need help with getting those into a variable in the first place...
Any help would be greatly appreciated.

User avatar
crashfly
Posts: 789
Joined: Thu Mar 13, 2008 11:39 pm
Location: Arkansas, USA

Post by crashfly » Thu May 22, 2008 1:05 pm

Part of that can be done in XP (2000 and above) with a cmd file (which essentially is a batch file). There are "variable" extensions which allow you pull any part of a variable into something else.

Depending on how your date variable is set, you can actually use this:

Code: Select all

set DATE_STAMP=20%date:~7,2%_%date:~3,3%_%date:~0,2%
to pull out certain pieces of what is returned from the date.

I have not tried in depth use of commands to variables, but you might be able to use something similar to what is being done to the date.

I only know about the above usage because I have a batch file backup certain files, have them compressed, and have the date they were "backed up" on the file name.

Hopefully the above will help.
A mind is like a parachute, it only functions when it is open.
--Anonymous

How to Ask Questions the Smart Way

User avatar
twig123
Posts: 105
Joined: Tue Jan 17, 2006 12:19 am

Post by twig123 » Fri May 23, 2008 2:26 pm

I think I ended up getting it figured out... not exactly how I wanted, but it works.

did a:
set l>>logon.txt
find "nameofcodehere" logon.txt >NUL && set server="servertomaptohere"
then I just call %server%

... Unfortunatly I had to make ~60 lines of the same thing (I'm no good with arrays), but all in all, it is working =)

User avatar
crashfly
Posts: 789
Joined: Thu Mar 13, 2008 11:39 pm
Location: Arkansas, USA

Post by crashfly » Fri May 23, 2008 6:07 pm

I may have found a solution for you that really should work.

It will take whatever you have returned by a command line and then return it to a variable (hopefully only one line). Once you get the information into the variable, then you can use some string modifiers to cut out what you want.

Example:

Code: Select all

setlocal
set l > xxxtemp.txt
set /p INFO= < xxxtemp.txt
del xxxtemp.txt

echo %INFO:~3,3%
endlocal
I am still not entirely sure what "set l" should be returning for you, but maybe if you can fill us in with a bit more data ... the above commands could be modified to work for it.

FYI, the command 'set l' returns "LOGONSERVER=\\MYCOMPUTER". Now, if you directly just want to work with the variable %LOGONSERVER%, then all of the above coding would not be necessary. You could use the following code to get what you need:

Code: Select all

setlocal
set INFO=%LOGONSERVER:~3,3%

rem **do something with %INFO% varialbe**
endlocal
If you are doing what I think you are doing, that should return the first three characters of the logon server.

Edit: might need to be modified because I think DOS starts counting at 0, but the first number in %variable:~#,#% gives the starting location and the second number tells how many to grab.
A mind is like a parachute, it only functions when it is open.
--Anonymous

How to Ask Questions the Smart Way

yumeyao
Moderator
Posts: 1718
Joined: Sun Aug 27, 2006 9:24 pm
Location: Taiyuan, Shanxi, PR China

Post by yumeyao » Sat May 24, 2008 11:24 am

well, if you want the 3rd, 4th and 5th letters, it should be %tempvariable:~2,3%
Image
My work list(Hosted by dumpydooby)

User avatar
crashfly
Posts: 789
Joined: Thu Mar 13, 2008 11:39 pm
Location: Arkansas, USA

Post by crashfly » Sat May 24, 2008 12:23 pm

yumeyao wrote:well, if you want the 3rd, 4th and 5th letters, it should be %tempvariable:~2,3%
Yeah, I was going to get around to changing that . However, you beat me to it. Oh well. :wink:
A mind is like a parachute, it only functions when it is open.
--Anonymous

How to Ask Questions the Smart Way

User avatar
twig123
Posts: 105
Joined: Tue Jan 17, 2006 12:19 am

Post by twig123 » Mon May 26, 2008 2:56 pm

Sweet! I knew it should be simple like this, but I just didn't know the code!!
Thank you very much for the help and the description of the code! Slight modification to the code was needed as it counts the \\ as #0 & #1.
So:
set INFO=%LOGONSERVER:~4,3%
The above returns the results I was looking for.

User avatar
crashfly
Posts: 789
Joined: Thu Mar 13, 2008 11:39 pm
Location: Arkansas, USA

Post by crashfly » Mon May 26, 2008 4:36 pm

twig123 wrote:Sweet! I knew it should be simple like this, but I just didn't know the code!!
Thank you very much for the help and the description of the code! Slight modification to the code was needed as it counts the \\ as #0 & #1.
So:
set INFO=%LOGONSERVER:~4,3%
The above returns the results I was looking for.
Cool. Glad that could help. I scratched that up way back when I was looking for something to help make some back up of files. Mine specifically extracts the date for my purposes, but I am glad yours works for you now.
A mind is like a parachute, it only functions when it is open.
--Anonymous

How to Ask Questions the Smart Way

Post Reply