Weird Batch File Issue
Can someone help me with this one? I have a batch file, where I am trying to connect a couple network drives based on my current internal IP address. Problem is, it is outputting the following:
Home 192.168.2.99 Basement
Where it should be just outputting: Home 192.168.2.99
Here's the code:
@echo off @for /F "tokens=2 delims=:" %%i in ('"ipconfig | findstr IP | findstr 192."') do SET LOCAL_IP=%%i @if ("%LOCAL_IP%" == "192.168.2.99") Call ConnectHome else (Call ConnectBasement) :ConnectHome @echo Home %LOCAL_IP% :ConnectBasement @echo Basement @REM net use R: \\192.168.2.98\Storage @REM net use S: \\192.168.2.98\MyStuff @REM net use T: \\192.168.2.98\Server
I've also tried replacing the IF statement with:
@if ("%LOCAL_IP%" == "192.168.2.99") goto ConnectHome else (goto ConnectBasement)
@if ("%LOCAL_IP%" == "192.168.2.99") goto :ConnectHome else (goto :ConnectBasement)
and the results are always the same... OS is Windows 7 Pro
Call is intended to call a different batch file from the one you're currently in (see here). If it's passed a label, it will execute from that label, but it'll execute to the end of the file, so it won't stop when it hits the :ConnectBasement line.
What you probably need is to add a label :END at the bottom of the file, and Goto :END just before your :ConnectBasement line. For consistency I'd probably suggest using Goto instead of Call throughout. (You can also use Goto :EOF without defining a label, if you wish.)
(Or, learn PowerShell and use its functioning capability to implement this more cleanly ;-))
After going to :ConnectHome, it just goes to :ConnectBasement. Add goto :eof just below @echo Home %LOCAL_IP%.
Aren't you forgetting to stop execution after the :ConnectHome part ? I'm not sure, but I think the batch simply continues after that, printing "Basement".
edit: Jeroen beat me to it :)