Wednesday, May 1, 2019

Perl - get the script path

# C:\Foo\Bar\

use strict;
use warnings;

use FindBin qw($Bin);

print "My script lives in $Bin\n"; # My script lives in C:/Foo/Bar

Note that there is no trailing slash, and Windows back-slashes '\' are converted to forward slashes '/'

Wednesday, April 3, 2019

How to Roll Back (revert) an SVN commit

Crap. My last commit in Subversion broke Jenkins. How do I undo it?

Sometimes TortoiseSVN gives us too much of a good thing. There are several options with similar names for "reverting" and "updating" files in Subversion, and I can never keep them straight, thus another post that tries to sum up the options and their effects.

First, just a simple "undo":

Revert a single file:
  1. Right-Click -> TortoiseSVN -> Show log
  2. Find the revision/changeset you want to rollback to (i.e. the revision just before the change you want to remove)
  3. Right-click on the single file in the list of files -> Revert to this Revision
Revert the entire changeset:
  1. Right-Click -> TortoiseSVN -> Show log. 
  2. Find the revision/changeset you want to rollback to
  3. Right-click on that revision -> Revert to this Revision
(Paraphrased from eduncan911's lucid instructions in this post on Stack Overflow)

Wait a minute. The context menu in TortoiseSVN (1.9.7.x) shows three similar commands when selecting a revision in the list:
  1. Update item to revision
  2. Revert to this revision
  3. Revert changes from this revision
and just the last one, "Revert changes from this revision," when selecting an item in the list of files.


Here's what each "Revert" option does, according to JB Nizet again on Stack Overflow:
Let's say you have these N successive commits: 1, 2, 3 and 4.
If you select the commit 2 and choose "Revert to this revision", your working copy will contain the changes brought by commits 1 and 2. Commits 3 and 4 will be "canceled".
If you select the commit 2 and choose "Revert changes from this revision", your working copy will contain the changes brought by commits 1, 3 and 4. Commit 2 will be "canceled", or rather, played in reverse on the top of commit 4: if a line was added, it will be removed. If a line was removed, it will be re-added.
(Emphasis and spelling corrections are mine)

So it would follow that either option has the same effect when undoing just the last commit on a file.

How about "Update item to revision" vs "Revert to this revision"? Here's Peter Parker's explanation on Stack Overflow:
Update to revision will only update files of your working copy to your chosen revision. But you cannot continue to work on this revision, as SVN will complain that your working copy is out of date.
revert to this revision will undo all changes in your working copy which were made after the selected revision (in your example rev. 96,97,98,99,100) Your working copy is now in modified state.
The file content of both scenarios is same, however in first case, you have an unmodified working copy and you cannot commit your changes (as your working copy is not pointing to HEAD rev 100), in second case you have a modified working copy pointing to head and you can continue to work and commit
(spelling and punctuation corrections are mine)

Wednesday, December 19, 2018

Count directories with Windows command line

cd C:\Foo

REM Foo contains 2 files and 3 folders
dir /b

REM Filter out files and '.' and '..' folders
for /f %i in ('dir /b /ad-s-h ^| find /i /v /c ""') do @echo %i

Tuesday, September 11, 2018

Perl: How to capture a regex match in one line

my ($match) = "some string" =~ /(some).*/;
print $match; # prints "some"

Friday, March 17, 2017

Ten Years On

Just realized I've had this blog up on Blogger since February 2007.

My site is now no longer hosted on GitHub pages at

See you there here!

[Edit - 11 Sept 2018: decided to move back, GitHub pages is too much work!]

Sunday, March 5, 2017

Control+Shift+Enter to launch a Windows program as admin!

To launch any Windows program with elevated (admin) privileges, hold Control+Shift keys, then double-click the program's icon, even on the taskbar.

Alternatively, hit the Windows key, type the program name, e.g. cmd then type Control+Shift+Enter to launch the program with elevated (admin) privileges. You might have to enter admin credentials in the UAC popup.

See this LifeHacker article for more details.

Wednesday, April 13, 2016

How to check for admin rights from a Windows batch file

Simple check for administrative permissions in a batch file:

REM Check for admin rights - net session must run as admin
net session >nul 2>&1
if %ErrorLevel% NEQ 0 (
    echo You must right-click and select "RUN AS ADMINISTRATOR" to run this script.
    exit /B