Posts tagged windows

Command Line Zip for Windows

Windows doesn't have a command line utility for compressing files (that I know of), and I had to come up with a way to automate some file transfers today. So I whipped up a tiny Perl script that will zip up a file. I figure someone else might need it. (Or someone might know how to do this in one line.) You'll need the Archive::Zip module, and the following code—which is pretty much right out of the documentation:
# Grab the incoming file
my $argv = join(' ', @ARGV) or die "Usage: zipme.pl [file location]\n";

# Grab the file name
my($dir, $file) = $argv =~ m/(.*\\)(.*)$/;

# Create a Zip file
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
my $zip = Archive::Zip->new();

# Add the file
my $file_member = $zip->addFile($argv, $file);

# Save the Zip file
unless ( $zip->writeToFileNamed($argv.'.zip') == AZ_OK ) {
    die 'couldn\'t zip';
}
Save this code as zipme.pl, and you'll be set. Pass in a filename, and you'll get a compressed file of the same name plus the .zip extension. So:

C:\>perl zipme.pl C:\path\to\giant.file

will give you C:\path\to\giant.file.zip. It works well with Windows batch files, and will save me a bunch of bandwidth.

Compiling Perl modules on Windows

I've been doing quite a bit of Perl scripting for Yahoo! Hacks, and I can safely say it's my scripting language of choice now. A big reason for this is the abundance of pre-existing modules available. Why reinvent the wheel if someone else has been there before? I also develop most of the scripts on a Windows machine running ActivePerl. It works well, but the modules available via the ActiveState Perl Package Manager leave something to be desired. Many of the modules I want to use are only available via CPAN, which means I have to compile the modules for Windows myself.

Compiling Perl modules isn't normally a big deal. In fact, on my Mac it's a snap. I simply downloaded the Mac Developer Tools and started make-ing stuff all day long. I assume the same is true for Linux. Windows is another story.

Compiling modules would probably be easy if I was a C++ developer, and had a copy of Visual Studio Something-or-other with its own compiler. But I'm a thrifty scripter, and I just want some Perl modules to work. So I had to go through a series of steps, and put all of the pieces into place to be able to compile modules. I thought I'd write it up here in case anyone else out there is pulling their hair out like I was.

How to compile Perl modules on Windows without a Visual Studio:
  • Get nmake. (direct link to file.) This is your compiler.
  • Sometimes you'll need a file called Windows.h. Install the Windows SDK.
  • Sometimes you'll need a file called msvcrt.lib or msvcr70.lib. Install the .NET framework SDK.
  • For everything else you need install the Visual C++ Toolkit.
  • At this point, you will have installation fatigue. Take a break.
  • Inside the VC++ Toolkit is an important file called vcvars32.bat that adds some key directories to your system PATH variable. Edit this file so that the lib and bin directories of each of the kits you just installed will be added to PATH when you run this batch file.
With these behemoth kits in place, you should be set to start compiling Perl packages. Once in a while you may need the odd odbc32.lib—it's included with the SQL Server developer tools. Don't have it? Start installing. ;) And when you're done, don't forget to add its directory to your PATH.

Run vcvars32.bat before you start compiling a module to get your paths in order, and then run nmake like you would make:

nmake test
nmake install

I was almost in tears when things were compiling correctly. :) I have to let you know that I'm not a Perl expert, and compiling modules like this may wreak havoc with your system. But it's working fine for me.

SpamAssassin for Windows

I finally have SpamAssassin working on my Windows server. It's actually really good at catching spam. Once in a while one slips through, but for the most part it's made my email more useful. I thought I'd share the solution in case anyone else wants to do it.

Here's what you need to mirror my setup: You just install the Outlook plugin—even if you don't have Outlook on the server. And then add an entry that calls the WSH script to any tab file in the Xmail filters folder. I have more detailed instructions in the WSH file. Just want to share the spam-filtering goodness!

Font Browser

The Short Font Story

I made this font browser thing that shows all of your installed fonts (along with a sentence in the face) on a web page. (screenshot) If you're on Windows 2000 or XP and you want a quick look at your fonts, check it out:

Font Browser
(right-click, "Save Target As...", then double click the icon.)

The Long Font Story

The other day I needed to choose a font and I couldn't find a good way to browse all of the fonts on my system at once. I could only look at them one at a time with Microsoft's Font Viewer. That was time consuming, but it worked. (I know there are probably programs out there to help me out, but what I want to do is very simple and I shouldn't need to install a big program just to browse fonts.) I decided WSH (Windows Scripting Host) could be my answer.

Windows has this fine scripting API, but they don't make it simple to use. The standard FileSystem object and the standard Registry functions were no help in reading font names so I delved into the world of WMI (Windows Management Instrumentation). It's a dark murky land where commands are cryptic and documentation is lacking. It did the trick, though. And along the way I learned that I can check the temperature of my processor and manually fire up the fan. And other neat tricks.

The Font Disclaimer

There's nothing out of the ordinary happening in the script, but it's always a good idea to take a look at the source before you run it. I tried to note what each function does in the code.