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

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..


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

Julian HansenCommented:
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.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Simon336697Author Commented:
Thank you so much guys....Sorry about the delay in allocating points for this.

Much appreciated.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 2000

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.