2017-03-05 89 views
0

我想每天將在同一天創建的文件壓縮到一個壓縮文件夾中,並清除當天未壓縮的文件。 我有這個批次荏苒該用相同的名字創建的文件:對同一天的所有文件進行壓縮

@echo off 
for %%A in ("E:\Logs\SmartLogger\*") do (if /I not "%%~xA" == ".zip" 7za.exe a -tzip -mx5 -y -- "%%~dpnA.zip" "%%~A" >nul && del /Q /F "%%~A") 

我怎樣才能改變它忽略的名字,但尊重的日期? 例如,如果我有一個一天創建的是文件:

doc1.dat 
doc2.dat 
doc3.dat 

每一天,我正在創建這些文件,我想壓縮它們每天。 我使用7zip的BTW

+0

你能解釋一下你的意思嗎?「忽略名稱但尊重日期」? –

+0

表示當天創建哪些文件並不重要,我只是想將它們全部壓縮到一個壓縮文件夾中,並刪除那些在壓縮後那天創建的文件。 –

+0

你想刪除哪些文件?目前尚不清楚問題是否要求壓縮文件,但示例代碼會刪除壓縮文件。 –

回答

0
@echo off 
    setlocal enableextensions disabledelayedexpansion 

    rem Try to change to target folder. Leave on error 
    pushd "E:\Logs\SmartLogger" || goto :eof 

    rem Retrieve the today date to use as zip file name 
    set "today=" & for /f "tokens=1-3 delims=/ " %%a in (' 
     robocopy "|" . /njh 
    ') do if not defined today set "today=%%a-%%b-%%c" 

    rem We need a temporary file to store the list of files to process 
    set "listFile=%temp%\%~nx0.%random%%random%%random%%random%.tmp" 

    rem Retrieve the list of files generated today and save to temp file 
    >"%listFile%" forfiles /D 0 /M *.dat 

    rem On sucess (files found) compress the list of selected files 
    if not errorlevel 1 (
     7za -tzip -mx5 -y -- "%today%.zip" @"%listfile%" 
    ) 

    rem On sucess remove the zipped files 
    if not errorlevel 1 (
     >nul 2>nul (
      for /f "usebackq delims=" %%a in ("%listFile%") do del /q /f "%%~fa" 
     ) 
    ) 

    rem Remove the temporary file 
    2>nul del /q /f "%listFile%" 
0

我的第一個建議是使用WinRAR的

該命令行可以直接用作計劃任務命令行,也可以在批處理文件中使用。

"%ProgramFiles%\WinRAR\WinRAR.exe" m -afzip -agYYY-MM-DD -cfg- -ep1 -ibck -inul -m5 -tn1d -x*.zip -y -- "E:\Logs\SmartLogger\Logs_.zip" "E:\Logs\SmartLogger\*" 

的WinRAR的通過點擊GUI的主窗口的WinRAR在菜單上的菜單項幫助主題幫助打開的幫助包含標籤內容列表項命令行模式與幫助頁面:

  • 命令行語法
  • 命令
    • 字母命令列表
  • 開關
    • 字母切換列表

上面的命令行可以很容易地使用這3個幫助頁面進行編寫,並閱讀似乎對當前壓縮/提取任務感興趣的開關的幫助頁面。

查看按字母順序排序的開關列表瞭解使用的開關以及這條單線如何完成整個工作。


我的第二個建議是使用的7-Zip這確實需要一個批處理文件(16.04版)的7-Zip沒有對視(備份/壓縮)操作文件日期開關。

下面是使用的7-Zip與上次修改日期福祉今天的日期每個文件壓縮成與Logs_YYYY-MM-DD.zip一個* .zip文件的文件名,我的解決方案。

@echo off 
setlocal EnableExtensions EnableDelayedExpansion 

set "ListFile=%TEMP%\ListFile.tmp" 
del "%ListFile%" 2>nul 

rem Get todays date in region and language dependent format required for 
rem last modification date evaluation of all files in source directory. 

echo Get todays file date.>"%TEMP%\GetTodaysFileDate.tmp" 
for %%I in ("%TEMP%\GetTodaysFileDate.tmp") do set "Today=%%~tI" 
set "Today=%Today:~0,-6%" 
del "%TEMP%\GetTodaysFileDate.tmp" 
echo Today is: "%Today%" 

rem Set current directory being the directory with the files to process. 
cd /D "E:\Logs\SmartLogger" 

rem Search with DIR for files with todays date as last modification date 
rem output by DIR sorted by last modification date with newest file first. 
rem The found files with todays date are written into a list file. The 
rem loop is exited if the first file is processed not having todays date. 

for /F "delims=" %%I in ('dir /A-D /B /O-D /TW * 2^>nul') do (
    set "FileDate=%%~tI" 
    set "FileDate=!FileDate:~0,-6!" 
    if not "!FileDate!" == "%Today%" goto CompressFiles 
    if /I not "%%~xI" == ".zip" echo %%I>>"%ListFile%" 
) 

:CompressFiles 
if not exist "%ListFile%" goto EndBatch 

rem Get todays date in a Windows region and language settings 
rem independent format for usage in the ZIP file name. 

for /F "skip=1 delims=." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime') do set "CurrentDate=%%I" & goto ReformatDate 
:ReformatDate 
set "ZipFileName=Logs_%CurrentDate:~0,4%-%CurrentDate:~4,2%-%CurrentDate:~6,2%.zip" 

"%ProgramFiles(x86)\7-Zip\7z.exe" a -tzip -mx5 -scsDOS -sdel -y -- %ZipFileName% "@%ListFile%" >nul 

del "%ListFile%" 

rem Restore the environment (environment variables, current directory, 
rem state of command extensions and delayed expansion) before calling 
rem the command SETLOCAL at top of this batch file. 

:EndBatch 
endlocal 

此批處理文件預計,該文件夾不包含在未來的最後修改日期的任何文件相比,今天的日期。此限制是由對日誌文件在目錄中找到的所有文件的文件日期進行優化評估引起的。刪除FOR循環中的goto CompressFiles行時不存在此限制。

而且的7-Zip有一個幫助文件,該文件是7zip.chm在程序文件的7-Zip它通過雙擊打開後標籤包含內容列表項下命令行版本幫助的文件夾

  • 語法
  • 命令開放命令行命令
  • 交換機開放命令行開關

請先檢查線路echo Today is: "%Today%"真正輸出運行計劃任務中定義的用戶帳戶下這個批處理文件在機器上只文件日期(沒有或沒有工作日)不包括空格字符和文件時間。

已使用的用戶帳戶的Windows區域和語言設置確定日期/時間格式%%~tISET命令行下面的兩個命令行%%~tI必須也許適應得到只是今天沒有時間日期。

也可以使用WMIC也可以獲得每個文件的最後修改日期,如Find out if file is older than 4 hours in batch file的回答中所述,但這確實很慢並且需要更多代碼。

爲了解所使用的Windows命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。

  • cd /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

閱讀也是微軟的文章關於Using Command Redirection Operators>nul2>nul的解釋。

相關問題