In my previous post about RDC, I didn’t fully like the batch file I wrote which required me to specify a parameter for swapping connections, versus hibernating.  In fact, my favorite batch file is one where the action can be specified by the operator, or on the command line directly as a parameter.

So I rewrote the COMMSWAP.bat process to do just that.   If you launch it with no parameters, it displays a list of options which the operator can select with a letter and pressing ENTER.  If it launched with a parameter, it will directly execute the action (useful for schedule or other event launches).

The functionality hasn’t changed.  You can also see an example of how SET /P variable=”prompt” is used to prompt the user and record the answer.  Enjoy.

(Commswap.bat)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@echo off
 
REM CommSwap.bat -- release network resources (IP addresses), optionally put the laptop
REM into hibernation or shutdown. Reads the command line argument, or prompts user if no argument supplied.
REM
REM CommSwap.bat s   -- (swap) release network resources, wait 5 seconds, then renew resources.
REM CommSwap.bat h   -- (hibernate) release network resources, then hibernate.
REM
 
set xAction=%1
if not "%1" == "" goto CHECKPARM
 
:ASK
echo.
echo Network resource release, with optional renew.  Use this process when
echo switching between wireless and wired connections, or to gracefully
echo prepare the computer to hibernate before changing location.
echo.
echo Select from the following:
echo.
echo S -- (Swap) Release all adapters, wait 5 seconds, the renew all adapters.
echo H -- (Hibernate) Release all adapters, then initiate hibernation.
echo X -- Exit: don't do anything.
echo.
set /P xAction="Enter your choice: "
 
:CHECKPARM
if /I "%xAction%" == "h" goto OK
if /I "%xAction%" == "s" goto OK
if /I "%xAction%" == "x" exit
 
echo.
echo Command "%xAction%" not understood.. please try again.
goto ASK
 
:OK
 
ipconfig /release *
 
if /I "%xAction%" == "h" start rundll32 Powrprof.dll,SetSuspendState
if /I "%xAction%" == "h" exit
 
echo Waiting 5 seconds for you to disconnect a network cable or
echo turn off the wireless connection.
echo.
echo WScript.Sleep (5000) > c:\$$tmp$$.vbs
cscript //nologo //b c:\$$tmp$$.vbs
del c:\$$tmp$$.vbs
 
ipconfig /renew *
 
exit