Joy of TCC and TakeCommand - Thinking the TakeCommand way.

As you know, I'm a fan of using TakeCommand on Windows. It's a great interactive shell but it also provides some pretty powerful scripting tools too.  Personally, I wouldn't write long scripts in it but it's adequate for many tasks and certainly better than CMD.EXE.  That's just torture.


Functions, not piping, for the win.

Long form:

Using TakeCommand is not like using Unix shells, like Bash or KornShell.  The Windows operating system is more function-oriented than byte stream-oriented.  Piping is possible but not natural.

As a result, it took me a long time to figure out a simple problem.

Find the total amount of space that my index.js files occupy, in a tree of directories?

I tried piping.  On Unix, it's a one liner:

find . -name index.js -printf "%s %p\n" |awk 'BEGIN{sum=0}{ sum+=$1 }END{print sum}'

But that actually doesn't work correctly under the new Bash on Windows.  File sizes appear to be messed up.

I tried the FOR loop in TakeCommand.  This monstrosity is a direct descendant from DOS, which makes sense because TakeCommand is an extended DOS/Windows shell, not a replacement or alternative.  I could read lines from the "@CON" file (yes, that's the name when using the FOR loop). And then I'd be summing lines one at a time using the Take Command language.  While I/O would dominate the time, this just seemed clunky.

The final solution was simple but not obvious to find. It has changed how I think about using TakeCommand.

TakeCommand lets to you sophisticate file matching, more than just file globbing as in Unix.  It's more like VAX/VMS (blast from the past!)  To match all the index.js files recursively, type:

index.js /S

In addition, TakeCommand has tons of built-in functions.  There's one called @FILESIZE.  Combine the two ideas and you get a filesize function that can recurse through a list of files (!)

Thus, the solution to my problem is this:

echo %@COMMA[%@FILESIZE[index.js/S]]

Which gives the result, comma separated I might add.  And it's quite fast.

You can use the /S form in any of the built-in TakeCommand commands, like COPY or MOVE. You can select files by size, date-range, even attributes (like HIDDEN or ARCHIVE).


Popular Posts