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

Posted on 2004-11-29
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
    LVL 49

    Expert Comment

    by:Julian Hansen
    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 49

    Assisted Solution

    by:Julian Hansen
    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 82

    Accepted Solution

    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
    LVL 1

    Author Comment

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

    Much appreciated.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    At the beginning of the year, the IT world was taken hostage by the shareholders of LogMeIn. Their free product, which had been free for ten years, all of the sudden became a "pay" product. Now, I am the first person who will say that software maker…
    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…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now