首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >压缩当天的所有文件

压缩当天的所有文件
EN

Stack Overflow用户
提问于 2017-03-05 17:06:02
回答 2查看 1.6K关注 0票数 0

我想每天压缩当天创建的文件(压缩到一个压缩文件夹中),并删除当天未压缩的文件。我有一个用于压缩同名文件的批处理:

代码语言:javascript
复制
@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")

如何更改它以忽略名称但尊重日期?例如,如果我在某一天创建了以下文件:

代码语言:javascript
复制
doc1.dat
doc2.dat
doc3.dat

每天我都在创建这些文件,我想每天压缩它们。我使用的是7zip btw

EN

回答 2

Stack Overflow用户

发布于 2017-03-05 19:32:18

代码语言:javascript
复制
@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
EN

Stack Overflow用户

发布于 2019-01-03 03:22:07

我尝试使用MC ND的解决方案,但当我使用@运算符时,它不起作用,没有找到包含列表的文件。如果只有一个文件,它就会起作用。

在使用@listfile模式时,目标和源参数之前的--参数导致了该问题。

希望这能帮助任何遇到同样问题的人

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42606449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档