title: 7-zip an in-use file
body: <p>We are running a number of Apache Tomcat servers.  A good portion of the logs rollover as they should based on the hour or at the end of the day.  However, there are still a number that remain locked by Apache Tomcat, or web services.</p>
  
  <p>Have actually logged this as a bug at - <a href="http://sourceforge.net/p/sevenzip/bugs/1488/" rel="nofollow noreferrer">http://sourceforge.net/p/sevenzip/bugs/1488/</a></p>
  
  <p>Have seen dbenham's <a href="https://stackoverflow.com/a/10520609/1333331">awesome post</a> (NOTE: it is under the second "EDIT" header, to what I am referring to) about how to deal with an in-use file. However, I am still running into contention with in-use files.  I know WinZip Command-line maybe an option, but I really don't want to use that.</p>
  
  <p>We have a nightly process to come in, which is much has more functions than this, but for illustration we are going to use the following:</p>
  
  <pre><code>    FOR /r "C:\tomcat\logs" %%X IN (*) DO (
      7za a -y D:\17\%%~nxX.zip %%X
      )
      PAUSE
  </code></pre>
  
  <p>So, what we see as a result are a number or 22-byte, empty zip files. Yes, we know not to zip an "in-use" file, but if it comes in does a directory list, we see this issue.</p>
  
  <p>Is there something that we can do?</p>
  
  <pre><code>Volume in drive D is New Volume
  Volume Serial Number is A476-BD38
  
  Directory of D:\17
  
  03/07/2015 02:05 PM &lt;DIR&gt; .
  03/07/2015 02:05 PM &lt;DIR&gt; ..
  03/07/2015 02:05 PM 403 catalina.2015-03-06.log.zip
  03/07/2015 02:05 PM 22 catalina.2015-03-07.log.zip
  03/07/2015 02:05 PM 22 host-manager.2015-02-23.log.zip
  03/07/2015 02:05 PM 22 localhost.2015-02-23.log.zip
  03/07/2015 02:05 PM 22 manager.2015-02-24.log.zip
  03/07/2015 02:05 PM 22 probe.log.zip
  6 File(s) 513 bytes
  2 Dir(s) 51,783,634,944 bytes free
  </code></pre>
  
  <p>Normally, I would not really care about 22-byte files, but we have to watch them as they are being sent nightly to a vendor for report processing.</p>
  
  <p>Looking at the script in how we are using it, we have:</p>
  
  <pre><code>REM SET VARIABLES
  FOR /f "tokens=2-4 delims=.:/ " %%a IN ("%date%") DO SET CurrentDate=%%c-%%a-%%b
  
  SETLOCAL ENABLEDELAYEDEXPANSION
  FOR /r "D:\17" %%X IN (*) DO (
      SET FileName=%%~nxX
      FOR /f "tokens=2-4 delims=.:/ " %%a IN ("%%~tX") DO SET FDate=%%c-%%a-%%b
      REM CHECK FOR UNLOCKED FILE
      REM https://stackoverflow.com/a/10520609/1333331
      2&gt;nul (   &gt;&gt;"D:\17\!FileName!" (call )) &amp;&amp; (
      REM DO STUFF HERE
      IF "%CurrentDate%" NEQ "!FDate!" (
          IF %%~zX GTR 0 (
              ECHO ARCHIVING "D:\logs\!FileName!" &gt;&gt;%logfile%
              7za.exe -tzip -y a "D:\Archive\some.zip" "D:\17\!FileName!" &amp;&amp; DEL "D:\17\!FileName!"
          )
      )
      REM END OF UNLOCKED ZONE
      ) || (
      ECHO FILE IS LOCKED
      )
  )
  ENDLOCAL
  </code></pre>
  
  <p>Thanks!</p>