Wednesday, April 21, 2010

Sed on Windows?

I needed sed today on Windows, and though there are versions of it available, or cygwin would work, I don't have privileges to install software on my client's machine.

Well, at least I had groovy.  So I did this:

new File(args[0]).eachLine { line -> println(line.replaceAll(args[1], args[2])) }

  And in a .bat file, this:


echo off
IF /i "%1" == "/?" (
echo [path expression] [match regex] [replace string] [output dir]
) ELSE ( 
for %%f in (%1) DO (echo converting "%%f" to "%4\%%~nxf") && (groovy replaceAll.groovy "%%f" "%2" %3)  > "%4\%%~nxf"
)


Yeah, I could have done the entire thing in groovy, but I've never used 'for' in cmd.exe before, and I just wanted to try it.

I'll probably not do it again, unless I have no other choice.  Anyway, it's here for future reference in case I ever have to use it again.

Friday, April 16, 2010

PVS Award Winner

This PVS award goes to the developer of this code (sanitized to avoid NDA violations).  While trying to troubleshoot an issue one day, the stack trace was always null.  Huh?  null stack trace?  How does that happen? 

The following class is in a shared library jar, and is used as the base class for many different application-specific exception classes in several apps.
 

public class MYException extends Exception {

...

    public MyException(String message, Throwable cause) {
        super(message);
        initializeCause(cause);
    }

    public Throwable initializeCause(Throwable cause)  throws IllegalArgumentException IllegalStateException {
        // ... some code here that conditionally throws those exceptions depending on what cause is, followed by this gem... \\
        mCause = cause;
        mStackTrace = null;  // clear a stack trace
        return this;
    }
...
    public void printStackTrace() { printStackTrace(System.err); }

    public void printStackTrace(PrintStream stream) {
        if (mStackTrace != null) {
            super.printStackTrace();
            if (mCause != null) {
                stream.print("Caused by: ");
                mCause.PrintStackTrace();
            }
        } else {
            stream.print(mStackTrace);
        }
    }

So printStackTrace always prints null.

Thursday, April 15, 2010

When it's the best you've got...

I use mostly Linux and Mac, but I'm frequently forced to use Windows at client sites.  The Windows cmd.exe is pretty lame, but here are some tips to do a few useful things.


start /b  - run something asynchronously

findstr  - poor man's grep

dir /S   - recursive directory list

dir /S *.java  - can do it with file name patterns

dir /S | findstr

dir /b  - bare names

dir /b /S [pattern] | cmd     -- this will open all the files that match the pattern (as long as an editor is registered for the extension)

fc -- not as good as diff, but can get you something when you've got nothing

systeminfo

systeminfo | findstr Time:
(if you shut down your laptop every night when you go home, this shows you how long you've been on the clock)

driverquery

%ERRORLEVEL% - returns the error code of the most recently used command.

%HOMEPATH%  - best we can do without ~/

set  - shows environment variables,  'set p' shows only variables starting with 'p' etc.,  set aslo sets variables.

%RANDOM% - returns random int between 0 and 32767
(for when you need to make a decision. nice coin toss)

reg  - display and work with registry entries, might have to combine with runas

pushd/popd  - push/pop directories for quick in and out

sort  - sort files or pipe standard output to it

tasklist  - same list as taskmanager, but on the command line, useful with...

taskkill - to kill one of those frequently misbehaving windows processes

sc  - control and interact with services

Thursday, April 8, 2010

Reluctant Blogger

For years I refused to start a blog. At first it seemed the epitome of narcissism. Incidently, I now think the most narcissistic things on the web are Twitter and Facebook. But anyway I now have some good reasons to keep a blog.