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.