如何在目錄下將修改日期為前一天的檔案一起用7-zip加密成一個檔案?
例如:我要在d:\test目錄下將修改日期為前一天的檔案(test目錄下會有很多目錄)用7-zip加密成一個檔案。請問批次如何寫啊?
思路:
targetDir:要處理的目錄
tempDir:暫存的目錄
finalDir:壓縮檔產出的目錄
sevenZ:7z.exe路徑
@echo off
setlocal ENABLEEXTENSIONS
set targetDir=C:\Users\aaa\bbb
set tempDir=D:\myTemp
set finalDir=D:\
set sevenZ="c:/Program Files/7-Zip/7z.exe"
md %tempDir%
call :Date2Day %date:~0,10% days
set /a days-=1
call :Day2Date %days% yesterday
for /f "tokens=1-3 delims=$$" %%i in ('FORFILES /P %targetDir% /S /C "cmd /c echo @isdir$$@fdate$$@path"') do (
if %%i equ FALSE (
if %%j equ %yesterday% (
copy %%k %tempDir%
)
)
)
%sevenZ% a -tzip %finalDir%archive.zip %tempDir%*
rd /q /s %tempDir%
pause
:Date2Day
setlocal ENABLEEXTENSIONS
for /f "tokens=1-3 delims=/-, " %%a in ('echo/%1') do (
set yy=%%a
set mm=%%b
set dd=%%c
)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
endlocal&set %2=%j%
goto :EOF
:Day2Date
setlocal ENABLEEXTENSIONS
set /a i=%1,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
::(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%/%mm%/%dd%
goto :EOF
如果你也用PYTHON的話,會比較簡單(WINDOWS)......
下例當批次檔案,請先安裝python for window直譯器(不用編譯EXE)
https://www.python.org/downloads/windows/
import os
from subprocess import run
import time
import math
yesterday = math.floor(time.time()/86400) - 1
# 昨天(小數點為時間,就不要了),那就是今天 - 1
command7z = ['7z', 'a', '-t7z', r'壓縮檔.7z']
pathTarget = 's:/test/'
for file in os.listdir(pathTarget):
fullfilename = os.path.join(pathTarget, file)
cdateFile = math.floor(os.path.getmtime(fullfilename)/86400)
# 上一行取得檔案建立時間,一樣取整數位就好
if(cdateFile == yesterday and os.path.isfile(fullfilename)):
# 建立日期為昨天,且......不是目錄的話
command7z.append(fullfilename)
# 把檔案加入7z的指令串裡就對了
if len(command7z) > 4:
# 有加東西才執行
run(command7z)