Standort: Alexander Foken > Projekte > Windows > deepcopy

download deepcopy


Yet Another XCOPY Clone ?
Error Handling

Yet Another XCOPY Clone ?

No! deepcopy is not yet another xcopy clone!

deepcopy was written to work around a very special problem of Microsoft Windows: Although Microsoft promises that you may use long filenames, you beg for trouble if you really do it. It is possible to create directory structures and files within that the Windows Explorer is unable to copy as whole directory tree, because the "full" filename exceeds a certain limit (around 260 characters).

deepcopy can copy those deep directory trees, by using some tricks that no one at Microsoft seemed to have thought of. See below to learn about these tricks.

deepcopy is intended to work unattended with large directory trees. So it tries to do its best to copy the directory tree, if an error occurs, deepcopy logs it and continues to work.


deepcopy sourcedirectory targetdirectory

sourcedirectory and targetdirectory must not contain each other. There is no safety check for this condition, use your brain. Make sure to use quotes around the directory names, at least if they contain spaces and other characters that DOS can't handle. sourcedirectory and targetdirectory should be absolute paths.

The targetdirectory is automatically created if it does not exist.

To copy an entire drive to another, use deepcopy S:\ D:\, where S: is the source drive and D: is the destination drive.

Error handling

A non-existing sourcedirectory or targetdirectory being a file are fatal errors that will stop deepcopy. Errors during copying are written to stderr and to the plain text file deepcopy.err in the current directory. All other errors are ignored.


"Long" and "Short" Names

If you use Windows 95, Windows NT 4 or newer, you can use "long" filenames, i.e. filenames having more than eight characters in the base name and more than tree characters in the extension, the standard DOS filename limits. You are even allowed to use certain characters that were illegal in DOS. To maintain compatibility with old DOS applications, Microsoft invented the "short name alias", a mangled version of the "long" name. The algorithm used to mangle the long name is relatively complex, but in most cases, it takes the first six characters uppercased, appends a tilde (~) and a digit, starting at 1. So "C:\Program Files\" is usually mangled to "C:\PROGRA~1\". But in some cases, the "short name alias" is even longer than the "long name": "C:\B A\" is manled to "C:\BA0129~1\" (at least on my machine). The short name alias is created when a file or directory is created, it is updated whenever the file or directory is renamed.

The full name of a file may be "C:\Program Files\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" (66 characters). It's short name alias is "C:\PROGRA~1\COMMON~1\MICROS~1\MSINFO\MSINFO32.EXE" (49 characters). The short name saves 17 characters. That may seem only a marginal saving, but if you remember that windows has a limit of around 260 characters, each character saved is a file copied sucessfully. ;-)

The Windows API offers a few functions for handling the short name aliases, the most interesting one is GetShortPathName(). It returns the short name alias of an entire path, no matter if the path is given as long names, short name aliases or a mix of both.

The Trick

deepcopy always uses the shorter name of the directory to be copied. This is most times the short name alias, in a few cases, it is the long name. It scans the source directory, and copies all files found using the short directory names and the long filenames from the source directory to the target directory. All subdirectories of the source directory are created in the target directory, and deepcopy repeats scanning and copying for each subdirectory, using the shorter names of both source and target directories. The long name is only used to create subdirectories and to copy files, and only the last part of the path is expressed as long name.