Executing a batch file only if the laptop is located on a particular subnet.

Posted on 2004-11-29
Medium Priority
Last Modified: 2010-04-14
Hi guys,

Hope you can help.


- Back up a folder on a laptop daily to a network location.
- Only run the backup task IF the user is located on subnet 10.60.10.x
- System in question is a laptop that gets a DHCP address.

I can generate the backup script, but what Id like help si the following:

1. How to code the script so that it only runs IF the laptop is given a DHCP address of 10.60.10.x and not another DHCP address, meaning it is on a different subnet

2. If the user is not connected to the LAN, and the task runs, to not run the script at all. That is, Id like the script to be able to check for a network connection, and if none, to abort the running of the backup.

Any help would be greatly appreciated.

Thank you..


Question by:Simon336697
  • 2
LVL 60

Expert Comment

by:Julian Hansen
ID: 12694969
The easiest way is to look for a known network folder or file and abort if it does not exist


if exist \\computername\share backupfiles

LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 400 total points
ID: 12694979
Let me do this a little better

Folder, file or Share - it should work on all three.

if exist \\computername\share call backupfiles.cmd

where backupfiles.cmd contains the code for backing up the files you want.

If the \\computername\share cannot be found - there will be a small timeout delay as the script tries to find it.

LVL 85

Accepted Solution

oBdA earned 1600 total points
ID: 12695001
Try it with this; this calculates the actual subnet, using the subnet mask. If the IP address isn't coming from a DHCP server, the script won't run the backup part.
Simply add your backup section where indicated.

@echo off
set BackupSubnet=
set TempFile=%Temp%\%~n0.tmp

ipconfig /all >"%TempFile%"
:: *** If no DHCP IP address, abort
type "%TempFile%" ^| find /i "DHCP"
if errorlevel 1 goto NoBackup
:: *** DHCP is used: Calculate the subnet
for /f "tokens=2 delims=:" %%a in ('type "%TempFile%" ^| find /i "IP Address"') do set IPAddress=%%a
for /f "tokens=2 delims=:" %%a in ('type "%TempFile%" ^| find /i "Subnet"') do set IPSubnetMask=%%a
del "%TempFile%"
for /f "tokens=1-4 delims=. " %%a in ("%IPAddress%") do (
  set s1=%%a
  set s2=%%b
  set s3=%%c
  set s4=%%d
for /f "tokens=1-4 delims=." %%a in ("%IPSubnetMask%") do (
  set /a s1 "&=" %%a
  set /a s2 "&=" %%b
  set /a s3 "&=" %%c
  set /a s4 "&=" %%d
set IPSubnet=%s1%.%s2%.%s3%.%s4%
echo Current subnet: %IPSubnet%
if not "%IPSubnet%"=="BackupSubnet" goto NoBackup
:: *** Backup the folder here:

:: *** end of Backup

Author Comment

ID: 12838836
Thank you so much guys....Sorry about the delay in allocating points for this.

Much appreciated.


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question