Monday, March 10, 2014

Un-zipping / Copying to Network Location

I had a lovely little batch script that, among other things, unzipped an incoming file (using 7-zip at the command line) to a network location using this command: 

"%ProgFiles%\7-Zip\7z.exe" e "C:\Temp\zipfile.zip" -o"x: " file.mdb -y -r

In this case, the network location is mapped to the “X” drive.  The command will extract file.mdb wherever it sits within zipfile.zip (-r recurses through all directories) and take all actions (namely overwriting existing files) without prompting (-y switch). 

While the script did not fail all the time, it seemed to fail fairly regularly somewhere in the middle of unzipping the file.  When the script failed, the only error message was that “The system cannot open the device or file specified.”  What file the system couldn't find/open wasn't entirely clear here, but I highly suspected that it was the destination file on the network drive.  The network location didn’t appear unstable (in that it never disappeared when I navigated to it or copied files manually), but my guess was that there was some kind of instability in the connection. 

To minimize this, I changed the zip command to unzip to the local directory and then copy the resulting file to the X drive:

"%ProgFiles%\7-Zip\7z.exe" e "C:\Temp\zipfile.zip" file.mdb -y –r
COPY file.mdb X:\file.mdb /Y

I thought this would minimize the opportunity for the process to fail.  Indeed, it no longer failed during the zip process, but it then started failing during the copy for the same reasons.  In doing some research, I found two switches associated with the XCOPY command that seem to have resolved the issue.  Here is the copy command I now use after unzipping to a local directory:

XCOPY file.mdb X:\* /J /Y /Z

The J switch removes buffering for large files (my file is nearly 1.5 GB).  The Z switch allows for restarting the copy over a network connection.  The COPY command has an equivalent Z switch, but not the J switch.

As an aside, I copy the file to X:\* to avoid the XCOPY command always asking whether the target is a file or directory ("Does X:\file.mdb specify a file name or directory name on the target (F = file, D = directory)?"). 

Since I switched to this method, the script has run like a champ with no failures.