我想每天压缩当天创建的文件(压缩到一个压缩文件夹中),并删除当天未压缩的文件。我有一个用于压缩同名文件的批处理:
@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
发布于 2017-03-05 19:32:18
@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%"
发布于 2019-01-03 03:22:07
我尝试使用MC ND的解决方案,但当我使用@运算符时,它不起作用,没有找到包含列表的文件。如果只有一个文件,它就会起作用。
在使用@listfile模式时,目标和源参数之前的--参数导致了该问题。
希望这能帮助任何遇到同样问题的人
https://stackoverflow.com/questions/42606449
复制相似问题