Home » excel » Zip batch command and Excel

# Zip batch command and Excel

Questions:

I have many excel files that I need zipped into each of their own zip folders. I thought I had this ironed out, but I have co-workers coming back to me saying that they cannot open the excel file because it has become corrupted. I went back and checked the original file, and it opens up just fine. But when I open up the same version of the file that was zipped, I too get the corrupted error. I’m on Office 2010, which is able to repair it, but my co-workers are all Office 2007 which does not seem to be able to fix the file. My batch code is as follows:

for /r %%X in (*.xlsm) do "C:\Program Files-Zipz.exe" a -tzip "%%~nX" "%%X"


I think you might be using a wrong value as the first parameter to the 7zip executable. According to the documentation on FOR:

%~nI – expands %I to a file name only

And according to the 7zip documentation:

You can use the “a” command with the single letter a. This command stands for ‘archive’ or ‘add’. Use it to put files in an archive. You have to specify the destination archive, and the source files (in that order).

So, using your script with an example file, it seems to me that your command line becomes:

"C:\Program Files-Zipz.exe" a -tzip "somefile.xlsm" "C:\path\to\somefile.xlsm"


Shouldn’t the first parameter have a .zip file extension on the end? So the line is modified to look like this:

for /r %%X in (*.xlsm) do "C:\Program Files-Zipz.exe" a -tzip "%%~nX.zip" "%%X"


As annoying as it is, file extensions actually mean something in Windows. Your previous line was creating a zip file with the .xlsm extension. When people try opening those files, Excel complains (because it’s a zip file; not a .xlsm).

@Echo OFF
PUSHD "C:\Program Files-Zip" && (
FOR /R "%CD%" %%# in (*.xlms) DO (7z a -tzip "%%~n#.zip" "%%#")
POPD
)
REM Don't worry about the PUSHD command, the %CD% variable isn't expanded, that's the trick.
Pause&Exit


And you can use the dynamic operator * (asterisk) and 7zip -recursive parameter if you want all together in one file:

7z a -r -tzip "ALL.zip" "*.xlsm"