Variable Question

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cander
Posts: 7
Joined: 01 Jan 2009 15:35

Variable Question

#1 Post by Cander » 01 Jan 2009 16:13

Hello,

Was making a batch and ended up with a little problem.

If you for example got the following:

Set HelloThere1=something
Set HelloThere2=something
Set HelloThere3=something

Set B=Hello
Set C=There1

(Just to describe easy. In my case variable B and C is diffrent everytime)

Is there then anyway to get the batch understand that I'm after to make %B%%C% = variable "HelloThere1"?

This is what I'm working on:

Code: Select all

@echo off
::FORSEND.BAT
:: The batch is suppose to take up multiple IP Address/computernames in a variable, sort them out in diffrent variables, and lastly send a message away to all of them using Net Send with FOR.
echo Type the Users IP Address/computernames seperated with a ","
echo.
Set /p connect=Users IP Address/computername:
FOR /F "delims=, tokens=1" %%A in ("%connect%") do set connect1=%%A

FOR /F "delims=, tokens=2" %%B in ("%connect%") do set connect2=%%B
If Test==Test%connect2% (
Set number=1
Goto SEND
)
FOR /F "delims=, tokens=3" %%C in ("%connect%") do set connect3=%%C
If Test==Test%connect3% (
Set number=2
Goto SEND
)
If not Test==Test%connect3% (
Set number=3
Goto SEND
)

:SEND
FOR /L %%Z in (1,1,%number%) do (
::Here's the problem below. I'm trying to make connect%%Z = %connect1%, %connect2% and %connect3% depending where the FOR command is. 
set forsend=connect%%Z
net send %forsend% You got a message!
)
pause
::END


Thanks in advance!

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 05 Jan 2009 17:13

Hi Cander,

try this for indirect variable access

Code: Select all

set connect1=1243
set str=connect1

call set value=%%%str%%%
echo %value%


jeb

Cander
Posts: 7
Joined: 01 Jan 2009 15:35

#3 Post by Cander » 11 Jan 2009 16:38

jeb wrote:Hi Cander,

try this for indirect variable access

Code: Select all

set connect1=1243
set str=connect1

call set value=%%%str%%%
echo %value%


jeb


hmm. The idea here was to get the connect1's variable value "1243" into the variable value?

What does the triple %%% do? Why the call?

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#4 Post by jeb » 12 Jan 2009 13:12

Hi Cander,

hmm. The idea here was to get the connect1's variable value "1243" into the variable value?

That's my intention.


What does the triple %%% do? Why the call?

The triple %%%, read it as (%%)%str%(%%), the rule is:
%% will "expand" to a single % without any other meaning,
so the inner % will expand the %str% to "connect1".
So you got in the first stage "%connect1%".

Now, the call do the next expand phase of "%connect1%" to "1234".

jeb

Cander
Posts: 7
Joined: 01 Jan 2009 15:35

#5 Post by Cander » 12 Jan 2009 18:46

jeb wrote:Hi Cander,

hmm. The idea here was to get the connect1's variable value "1243" into the variable value?

That's my intention.


What does the triple %%% do? Why the call?

The triple %%%, read it as (%%)%str%(%%), the rule is:
%% will "expand" to a single % without any other meaning,
so the inner % will expand the %str% to "connect1".
So you got in the first stage "%connect1%".

Now, the call do the next expand phase of "%connect1%" to "1234".

jeb


Oke. Could you give me a short working script so I can see how it works? Tried, but couldn't got anything like that working.

By the way, I just figured out a other way to make this thing work. Here's Net Send batch that should work pretty well:

Code: Select all

@echo off
::FORSEND.BAT
::The batch file takes up multiple IP Address/computernames in a variable, sort them out and put them into diffrent textfiles in a temp direcotry, and lastly send a message away to all of them using Net Send with FOR command.
::Works only on Windows XP or earlier versions and if the Messenger Service is enabled.
::Sends to 1-3 users.

echo Type the Users IP Address/computernames seperated with a ",". Max 3 in this version.
echo.
echo Example: Computername1,Computername2,Computername3
echo.
:CONNECT
Set /p connect=Users IP Address/computername:
IF Not Defined connect (
echo You did not write anything!
echo.
GOTO CONNECT
)
:MESSAGE
Set /p message=Your message:
IF Not Defined message (
echo You did not write anything!
echo.
GOTO MESSAGE
)

IF Exist %temp%\Forsend Rmdir /s /q %temp%\Forsend
Mkdir %temp%\Forsend

FOR /F "delims=, tokens=1" %%A IN ("%connect%") DO (
echo %%A > %temp%\Forsend\Forconnect1.txt
Set connect1=%%A
)

FOR /F "delims=, tokens=2" %%B IN ("%connect%") DO (
echo %%B > %temp%\Forsend\Forconnect2.txt
Set connect2=%%B
)
IF Not Defined connect2 (
Set /a number=1
GOTO SEND
)

FOR /F "delims=, tokens=3" %%C IN ("%connect%") DO (
echo %%C > %temp%\Forsend\Forconnect3.txt
Set connect3=%%C
)
IF Not Defined connect3 (
Set /a number=2
GOTO SEND
) ELSE (
Set /a number=3
GOTO SEND
)

:SEND
FOR /L %%Z IN (1,1,%number%) DO (
Set /a counter+=1
call:COUNTSEND
)

Rmdir /s /q %temp%\Forsend
Pause
GOTO END

:COUNTSEND
FOR /F "tokens=1" %%X IN (%temp%\Forsend\Forconnect%counter%.txt) DO Set forsend=%%X
net send %forsend% %message%
IF %ERRORLEVEL% GTR 0 (
echo ________________________________________________________________________________
echo Failed to send to user "%forsend%"
echo ________________________________________________________________________________
)

:END

Post Reply