Third party cookies may be stored when visiting this site. Please see the cookie information.

Windows script programming - lack of functionality in batch files - Perl portable

At work I use Microsoft Visio which I do like, but the file format is very temperamental. Recently we've had a few documents that have been unusable due to a corrupt .vsd file. Fortunately we had backups allowing us to recover from an older version (although difficult to reinstall), but this could have resulted in lost data as by the time we had noticed the recent backups were also of the corrupt file.

Fear not I thought - I already have a batch file which copies the documents to an intranet site, I just need to add an entry for it to add a daily backup. It was then that it hit me just how little Windows batch files have advanced since the 1980s. There are some tools that I found that could provide some of the functionality I needed, but whilst they had licenses allowing these to be used free for personal use a license needed to be purchased for commercial use. Although not expensive there is already an official backup procedure so I was not going to be able to get funding for this.

The annoying thing is that if this was Linux rather than Windows I was running (my work laptop is the only computer I've got that regularly boots into Windows) then I had so many great tools available to me.

For Windows if you want to go beyond basic launching of applications and copying files then you need to look at installing a programming environment and build an executable, but all I wanted was a basic shell script equivalent.

On Linux I could have done this using Bash, Perl, Python, PHP or several other scripting languages.

Installing Perl on Windows

In the past I've solved similar problems by installing Perl on Windows. This is available through a few different ways most notably using Activeperl from Activestate or using Cygwin (see Cygwin, running Linux programs on Windows).

I was unable to use these on this example as it needs to be able to run on other colleagues computers as well and I didn't want to go through installing these onto their computers.

Strawberry Perl portable

I have now found a solution in the form of Strawberry Perl and in particular the Strawberry Perl Portable release. The portable release is quite recent and is not available on the current release, but is on the previous stable release which is adequate for this requirement.

Perl portable is a version of Strawberry Perl configured so that it can be run from a portable / flash / usb disk drive without needing to install it onto the computer. This is designed for use in Internet cafes or when on someone else's PC where you need to run a perl programme but don't want to / have permissions to install perl onto the computer.

In my case rather than putting it onto a flash drive I installed Strawberry Perl portable onto a Windows Share drive so it is available to everyone with permission to the share.

Backup script

The backup script looks to see if the document has been updated recently and if so creates a backup file with todays date included in the filename.

I am not able to provide the source code for the program I created for a few reasons:

  1. It was developed at work - so belongs to my employer (it's not worth trying to get permission to make this available - because of reasons below)
  2. It's very specific to the install we have at work, so would need significant amount of changes for anyone else
  3. It's quite simple and most perl mongers should be able to hack something similar in no time

I will however give a few pointers to how it works. It's a bit of a hack designed to keep it simple. Anything more serious would need significantly more time to develop to make it bulletproof.

It has a config file which I change the modified time whenever a successful backup is run (alternative is to track the actual backup dates or search through the directory for when the latest backup was created)

I get the modified time of the file to be backed up and compare it with the modified time of the config file (done using time since Epoch - ie. stat($filename)->mtime ).

If the document has changed then it creates a new filename using gmtime(time) to establish the date and adds that to the filename for the backup file.

It then uses File::Copy to copy the existing file to the backup file.

Error checking is fairly basic (as we do still have an official backup - and this is just as a more convenient backup), but if this was mission critical then more sophisticated error handling would be needed.

In total this is 54 lines of code (including comments etc.), plus a config file listing the files to be backed up.

It seams that code developed for Unix [the perl interpreter] can still help with some of Windows shortfalls.


» PenguinTutor Facebook page