2009-05-26

Batch file with ampersand in its name won't run under Windows Task Scheduler

I wrote a batch file to run a simple script and then tried to schedule it using the Task Scheduler in Windows Server 2003, but for some reason it just wouldn't run a scheduled, even though I could run it with no problem by double-clicking on the batch file name.

After banging my head against the wall for a while, I finally tried taking the ampersand out of the name of the batch file, and suddenly Task Scheduler was able to run it.

2009-05-22

Copying all the files that match a certain pattern in a directory structure over to a single directory

For a long time I have wanted to collect all the files matching a certain pattern (Inv*.pdf) from a directory structure and copy them all to a single directory.

A Windows utility, xxcopy, has a command line switch to do exactly this.

I also found a forum discussion on how to do it in both Windows and *nix. And another one just about Windows.

With xxcopy the command line syntax to do what I wanted is:

xxcopy d:\contract\inv*.pdf d:\procurement\invoices\ /SG /BI

The /SG switch tells it to recursively (i.e. including from subdirectories) copy all of the files matching the wildcard pattern to the single target directory (i.e. without recreating the subdirectories under the target).

The /BI switch only copies files that either do not exist in the target directory or are newer versions of existing files.




How to use Windows Task Scheduler to copy files from a server in another domain

My problem was that I wanted to do a nightly transfer of files froma Windows machine in another domain to my Windows Server 2003 machine.The script to do the file transfer was easy, but I couldn't find a wayto keep the remote server mapped as a network drive. If I mapped thedrive through the GUI and clicked "Reconnect at Logon" it wouldn'treconnect after the server rebooted. Possible part of what made itdifficult was that the remote server was in a different domain and Ihad a different credentials on the remote server.

I figured out a way to permanently map a network drive to the remote server in a different domain, but then I discovered that batch files run using the Task Scheduler do not see any mapped network drives. Apparently network drives are only mapped to drive letters for real interactive sessions, and a user's network drives are not mapped when you run run a Task Scheduler job as that user.

After poking around a bit I found that the solution was to enter my credentials for the remote server in the Stored User Names and Passwords (Start button -> Control Panel -> Stored User Names and Passwords) utility on the local server. Apparently if you do that, whenever the local machine tries to access a resource on the remote machine Windows automatically uses the stored credentials to log into the remote machine.

So, once I entered my credentials to the remote machine in Stored User Names and Passwords I then mapped a drive letter to that machine in the first line of the batch file like so:

net use n: \\servername\volumename /persistent:no

And then used that drive letter for the copy command in the batch file.

UPDATE 2009-09-04. Somehow my entries in Stored User Names and Passwords on a Windows Server 2003 machine vanished overnight. When I went into the Stored User Names and Passwords GUI application from the Control Panel and re-entered my credentials using the format "\\10.10.10.1\sharename" for the server name the stored credentials wouldn't work when I tried to access shares on remote servers. I then opened a DOS prompt and tried this:

net use \\10.10.10.1\sharename /savecred

which resulted in prompts for me to enter my user name and password, which I did. After that new entries showed up in the Stored User Names and Passwords panel access through the Control Panel, and the stored credentials worked when I tried connecting to remote servers. I did notice that the new entries created through the/savecred method used just 10.10.10.1 as the server name, with no slashes and no share name, so maybe that was the problem all along.

2009-05-20

How to permanently map a network drive to a machine on another domain in Windows Server 2003

After a lot of trial and error trying different combinations of switches on the net use command, here is the network drive mapping method that resulted in a drive mapped to a remote machine in a different domain using different credentials that will survive a reboot of the local machine.

At a command prompt I ran:
net use h: \\server\folder /savecred /persistent:yes

I was then prompted to enter my username and password for the remote server, which I did. The drive was then successfully mapped, and the drive was still mapped after the machine was rebooted.

2009-05-06

How to create a Mac OS X keyboard shortcut to print to PDF

I was getting sick and tired of having to go to the mouse to click the PDF button from the Mac OS X print dialog every time I print a web page to PDF (which is really the only "printing" I do anymore). I googled around and found this awesome tutorial about how to set up a keyboard shortcut for this that I never would have figured out on my own in a million years:

Keyboard Shortcut for “Save as PDF…” in OS X at macsparky.com

This only gotcha I ran into is that you have to enter "Save as PDF..." with "as" in all lowercase in order for it to work. Entering "Save As PDF..." doesn't work.