Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any option to run a task when specific event occurs (i.e. Flash Drive inserted), but to do it just once a month?

I am trying to hook my backup script on some auto event..

OS: Windows 7 x64 Professional

share|improve this question
up vote 2 down vote accepted

Do you have experience with WMI (Windows Management Instrumentation)? It's perfect for this kind of tasks. Also, it's a part of Windows so it should cause minimal overhead.

Below is an example of a Perl script that executes some simple actions when a USB drive is plugged in. Your script doesn't have to be in Perl, WMI supports a whole range of scripting and programming languages.

Some variables

use Win32::OLE::Const 'Microsoft WMI Scripting';

my $ComputerName = "localhost";
my $NameSpace = "root/cimv2";
my $WbemServices = Win32::OLE->GetObject("winmgmts://$ComputerName/$NameSpace");

Register event (usb plugged in)

my $Instance = $WbemServices->Get(__EventFilter)->SpawnInstance_();
$Instance->{Name}          = "myfilter";
$Instance->{QueryLanguage} = "WQL";
$Instance->{Query} = qq[SELECT * FROM __InstanceCreationEvent WITHIN 1
                           WHERE TargetInstance ISA 'Win32_LogicalDisk' 
                           and TargetInstance.Name<>'B:' 
                           and TargetInstance.Name<>'A:'];
    # there are other queries possible

my $Filter = $Instance->Put_(wbemFlagUseAmendedQualifiers);
my $Filterpath = $Filter->{path};

Define action

# example 1 : execute script
my $Instance = $WbemServices->Get(ActiveScriptEventConsumer)->SpawnInstance_();
$Instance->{Name}            = "myscript";
$Instance->{ScriptingEngine} = "PerlScript";
$Instance->{ScriptText}      = q[open FILE, ">>C:\\\\usb.txt";print FILE "USB plugged in\n";];
    # you could call your backup script / check for dates / etc.

# example 2 : execute command
my $Instance = $WbemServices->Get(CommandLineEventConsumer)->SpawnInstance_();
$Instance->{Name}                = "mycommand";
$Instance->{CommandLineTemplate} = "echo Hello world!";
    # you could call your backup script / check for dates / etc.

my $Consumer = $Instance->Put_(wbemFlagUseAmendedQualifiers);
my $Consumerpath = $Consumer->{path};

Link event and action

my $Instance = $WbemServices->Get(__FilterToConsumerBinding)->SpawnInstance_();
$Instance->{Filter}   = $Filterpath;
$Instance->{Consumer} = $Consumerpath;
my $Result = $Instance->Put_(wbemFlagUseAmendedQualifiers);

This is a form of permanent event registration. To disable and clean the entire process up, remove the created objects:

  • the __EventFilter instance "myfilter"
  • the __EventConsumer instance "myscript" or "mycommand"
  • the __EventToConsumerBinding instance

You can search for the objects using WMI CIM Studio. Downloadable from Microsoft for free, here.

share|improve this answer
You could also use WMI CIM Studio to create the required objects and avoid creating them with a script... For example: search for "__EventFilter", locate the 'create instance' button, fill out the minimal required attributes (see script) and finish by hitting the 'save' button. – Sander Jan 3 '13 at 16:21

Have the script :

  • check if a file (/etc/last_backup) exist AND has less than X days. Doesn't backup otherwise (as it's not worth it: another backup occured less than 30 days ago)

  • if doesn't exist, or it's older than X dayx, it backups. AND it touch that file (touch /etc/last_backup) once backup is 100% complete.

That way you can also know when the last full backup occured (look at /etc/last_backup)

A way do know if file exist and is LESS than 30 days:

find /etc -mtime -30 | grep /etc/last_backup >/dev/null 2>/dev/null

and immediately after, $? will be "0" if grep could find it

That way, to force backup, just remove the /etc/last_backup file and next time the script will start the backup.

share|improve this answer
oh my... windows. You could use the GNU utils package for windows (or cygwin) for the script. And I think you can easily add this to the autorun.inf of the drive itself (google for "start script when plugging usb drive windows 7") (if you still allow autorun.inf commands, which is not really recommended...) – Olivier Dulac Jan 3 '13 at 13:54

I suggest Task Scheduler would be a simpler idea. However, success depends on finding a trigger to the event, and for that I would look in the Event logs.

My thinking is to master the backup command, save in a script, then use the power of the Task Scheduler service to handle the timing.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.