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

Wednesday, February 17, 2016

Another HEREDOC style for Windows Batch Files

I wanted to pipe a SQL query to sqlplus in a batch file, without using a temp SQL file.

That technique involves echo'ing the SQL statements to a temp file, then call sqlplus on that file:

REM create the temp SQL file
echo SELECT USER >>temp.sql
echo FROM DUAL;  >>temp.sql

REM execute sqlplus
sqlplus -S / @temp.sql

REM cleanup!
del temp.sql

An alternative is to wrap the echo statements in parentheses "( ... )", so they execute as a block, then pipe "|" the result to sqlplus:

    echo SELECT USER
    echo FROM DUAL;
) | sqlplus -S /

Admittedly, this is not much prettier than the first example, but it's self-contained, and doesn't leave random temp files that may or may not get cleaned up.

Tuesday, February 24, 2015

Perl property method?

package Foo

sub new

# bar property
sub bar
    my ($self, $text) = @_;
    if ($text)
        $self->{bar} = $text;
        return $self->{bar};


my $foo = Foo->new;

$foo->bar; # returns value of $self->bar
$foo->bar("baz"); # sets $self->bar = "baz"

Friday, May 9, 2014

HEREDOC for Windows Batch Files

From Wikipedia: "In computer science, a here document (here-document, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file."

Here's one way to accomplish this in a Windows batch file. Inside the batch file, echo the text to an output redirection, e.g. "> here.txt". Use carets "^" followed by double linebreaks to output on separate lines:


echo This is my ^

multi-line here document ^

that this batch file ^

will print! > here.txt

When executed here_text.bat will create a new file here.txt containing the lines of text:

This is my
multi-line here document
that this batch file
will print!

Monday, March 3, 2014


Noticed this rather defensive SSID in passing from my bus commute. I'm not sure if it's the WiFi or the Wife that's under guard, but the security's pretty robust. 

Thursday, May 16, 2013

Do-it-yourself uptime command for Windows 7

How long has my computer been running? A fundamental question that Unix-y systems can answer with the uptime command.

For some odd reason, Windows 7 has no built-in uptime command. You can simulate this by displaying the date and time Windows 7 last rebooted as reported by the net statistics command. Put this in a file named uptime.bat somewhere on your path:

@echo off
for /f "tokens=1,2,*" %%i in ('net statistics workstations ^| find "since"') do @echo %COMPUTERNAME% up since %%k

Then open a command prompt and type in the freshly minted command:

MYCOMPUTER up since 5/15/2013 5:21:27 PM

Saturday, May 11, 2013

Ruby, Ruby

You enter the j'Edit Five bistro from the steamy Paris evening. A sultry blonde, encased in a tight red dress, leans into the light over the toolbar at the far end of the room. She beckons to you with a heavy-lidded gaze, one eye obscured behind a curtain of flowing golden locks.

You step toward her, confident in that instant you're about to begin a beautiful friendship. You carry your plans and UML diagrams tucked inside the breast pocket of your jacket, a sheaf of ideas and ambition that only she can satisfy.

But as you draw near, you see the faint crow's feet around her eyes, the smudges on her dress, notice the style is seasons out of date. For the first time, you see the stringy, diminutive man in striped coveralls and a bill cap perched on the stool at her far side. A large red stone twinkles from the ring he twists around his pinkie.

You feel the heat from her body as you slide onto the bar stool beside her. "What is your name," you ask.

"Ruby," she breathes, "Ruby One Eight Seven." You inhale the cloying musk of her perfume, notice the slight trembling in her slim fingers as she lifts a tall flute of champagne to crimson lips. A trickle of moisture slides down the groove by her chin, spotting the front of her dress.

You try to mask your disappointment. You had so anticipated this meeting, a chance to make glorious music. But she is old, her skin creasing as she fits on a smile. She is deprecated, you think.

"And your little friend?" you ask, returning the railroader's nod.

"Him? He is only Rails Two Oh Three," her lip curls as she sneers. "He is just hoping to get lucky again tonight, but we are finished."

"You were so good together," you say, "I saw you performing together here years ago, up on the Web Two Oh stage."

"Yes," she says, "but I am tired. I live only to remember how I once danced here. The man who introduced us to j'Edit, he has gone away. And my younger sister, Ruby Two, has taken the limelight. Mais, she cannot dance here, my plugin is too old for her. So she prefers to dance for the Vim on Rue Rivoli, the Cafe Sublime, and le Palais de RubyMine." As she sweeps a cascade of gold away from her furrowed brow, the glitter of tears in her eyes reflects the flash of the red gems on her delicate earlobes.

You turn your gaze to the dim room. It is a good room, the windows edged with chrome, cups of Java steaming on the tables. Many coders and hackers huddle in booths by the glow of their screens, pecking out lines of Perl and Python, HTML and Scala. An aging XML roue nudges a tangled mass of angle brackets through the XSLT parser, chuckling as his XPaths return nodes.

"It could happen again," you say, "I could update the plugin to bring Ruby Two and her companion Rails Four back into the j'Edit. We could see them light up the windows again. She could parse, and colourize, and even debug in this room again. What do you say? Shall we find your little sister, and bring her back here?"

She tips her head back, stilling the quiver in her lip. "I will take you to the source, where we will set out the latest plugin for her, and repair the compiler errors and deprecations. We will update the mode to fit her. I may never dance here again, but my little Ruby Two, she will, and you will see to it. It will be her day, Ruby Two's day."

She demurs when you offer your hand. Instead, she lifts her satin clutch from the dark wood of the bar, and strides on her tall heels toward the door, a new vigor in her step. She pauses, and glances back at you. "Well, shall we?" she asks, not waiting for your answer as she exits, letting in the rush of traffic on the warm air.