-1

Could you please suggest me on how to zip file under folder in it's sub folder and rename the file with it's created date using batch script then delete the original one. It should be able to make the number of day to archive configurable. For instance if i want to archive any files older than 7 days, it should zip up the file older than 7 days. And also make delete file configurable as well.

For instance, file is abc.log created on 9/14/2016 it will be zip and rename as abc.20160914.zip

I search through forum but not found. Really appreciate for your help.

Diện Bio
  • 11
  • 2

1 Answers1

1

I find the answer for this

    @echo off
setlocal enabledelayedexpansion
set Archive=D:\scripts\test\Archive
set Temp=D:\scripts\test\Temp
set DaytoDelete=7

REM Moving file older than %DaytoDelete% days to TEMP folder
for /f "tokens=*" %%G in (D:\scripts\test\Location.txt) do  (
forfiles -p "%%G" -s -m *.log -d -%DaytoDelete% -c "cmd /c move @path %TEMP%"
)
if not exist "%Archive%" (md "%Archive%")
if not exist "%Temp%" (md "%Temp%")

REM Zip file in %Temp% and rename zip file with creation date
for %%a in ("%Temp%\*.log") do (
    echo Processing %%~nxa ...
    set File=%%~fa
    set Archive=D:\scripts\test\Archive

    for /f "tokens=1* delims=," %%a in ('wmic datafile where "name='!File:\=\\!'" get 'CreationDate' /format:csv ^| find /i "%ComputerName%"') do (set CreationDate=%%b)
    echo %%~nxa: !CreationDate!
    set cYear=!CreationDate:~0,4!
    set cMonth=!CreationDate:~4,2!
    set cDay=!CreationDate:~6,2!
    set FileTime=!cYear!!cMonth!!cDay!

    REM zip "%Archive%\temp.zip" %%~fa Issue with zip command, it zips whole full path of file
    "C:\Program Files\7-Zip\7z.exe" a -tzip "%Archive%\temp.zip" %%~fa
    ren %Archive%\temp.zip %%~na.!FileTime!.zip
)

REM Delete file after zipping
DEL /F /S /Q %Temp%\*.*
pause
Diện Bio
  • 11
  • 2