zip /M fails to delete file after adding to zip file

Jul 31, 2010
6
0
setlocal

rem TCC 23.00.25 x64 Windows 10 [Version 10.0.17134.471]
ver

set CurDir=C:\tmp\cwd
if not isdir %[CurDir] md %[CurDir]
set LogDir=C:\tmp\zip
if not isdir %[LogDir] md %[LogDir]

cdd %[CurDir]
del %[LogDir]\test.zip %[LogDir]\*.txt
echo Test1 >%[LogDir]\Test1.txt
echo Test2 >%[LogDir]\Test2.txt

rem zip /M does not delete the file when
rem 1: The zip file already exists
rem 2: the zip file is not in the current working directory
rem Uncomment this cdd to successfully delete Test2.txt after adding it to the zip file
rem cdd %[LogDir]
zip /m %[LogDir]\test.zip %[LogDir]\Test1.txt
zip /m %[LogDir]\test.zip %[LogDir]\Test2.txt

rem Views Test1.txt and Test2.txt in the zip file
zip /v %[LogDir]\test.zip
rem Lists Test2.txt, it is not deleted (although Test1.txt IS deleted)
dir /fa:-d %[LogDir]
 

rconn

Administrator
Staff member
May 14, 2008
10,886
97
The /M option was intended to be used with the /R option (though it also works with /U and /F).

The problem with using a /M with a (default) append operation is that the ZIP file stores the filename without any path info. ZIP /M scans the zip file after the files are successfully added (in a batch operation), and removes the matching local file. Without a /R, no path info is present so ZIP can't delete a file unless it's in the local directory.

/U and /F work because they add each file individually, so after a successful add they know it's OK to delete the source file. But appending files individually for the default add operation would slow ZIP down enormously (as in at least an order of magnitude for multiple files).