Avatar of Richiep86
Richiep86

asked on 

BATCH SCRIPT HELP - Compress, Move, delete and increment

HI All,

I have the following BAT script:

@echo off

REM Define file and folder locations
set BaseDir=c:\TestFiles
set DestZip=c:\OldLogs\output.zip
set z=c:\program files\7-zip\7z.exe

REM Zip all files and folders
"%z%" a -tzip "%DestZip%" -r "%BaseDir%\*.*"

Open in new window



This script grabs the contents of C:\TestFiles, compresses all the files and stores it in c:\OldLogs\output.zip. This is perfect, although i need the script to now:

1) Delete the old files from C:\TestFile where older than x days
2) The output.zip needs to increment each time the script is run, a time stamp would be ideal here:

20141215_1239
-20141216_1239

and so on...

Many thanks,
Windows Batch

Avatar of undefined
Last Comment
Richiep86
Avatar of oBdA
oBdA

Try this; the script is currently in test mode and will only display the "del" and "7z" commands it would normally run; remove the two uppercase ECHOs in lines 21 and 25 to run it for real.
@echo off
setlocal enabledelayedexpansion

REM Define file and folder locations
set BaseDir=c:\TestFiles
set DestZip=c:\OldLogs\output.zip
set SevenZip=%ProgramFiles%\7-zip\7z.exe
set DeleteOlder=365

REM Zip all files and folders
echo Getting time ...
for /f "tokens=1-9" %%a in ('wmic.exe Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
	set /a Line += 1
	if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
	if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set TimeStamp=%Year%%Month%%Day%_%Hour%%Minute%%Second%

echo Deleting old files ...
forfiles.exe /p "%BaseDir%" /m * /s /d -%DeleteOlder% /c "cmd.exe /c if @isdir==FALSE (ECHO del /f @path)"

echo Compressing '%BaseDir%' ...
for %%a in ("%DestZip%") do (set DestZip=%%~dpna-%TimeStamp%%%~xa)
ECHO "%SevenZip%" a -tzip "%DestZip%" -r "%BaseDir%\*.*"

Open in new window

Avatar of Kimputer
Kimputer

For ease, I usually use VBscript. This should work (though try to figure out the date string by using echo, as it depends on the system regional settings):

Dim fso, startFolder, OlderThanDate, objShell, strCommand, BaseDir, DestZip, zipexe
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

BaseDir = "=c:\TestFiles\" ' folder for source data
DestZip = "c:\OldLogs\" & right(left(Date,8),2)  & right(left(Date,5),2) & left(Date,2) & "-" & left(Time,2) & right(left(Time,5),2) & right(left(Time,8),2) & ".zip"
OlderThanDate = DateAdd("d", -3, Now)
zipexe = "c:\program files\7-zip\7z.exe"



"%z%" a -tzip "%DestZip%" -r "%BaseDir%\*.*"
strCommand = """" & zipexe & """  a -tzip " &  DestZip " """ & BaseDir & "*.*"""
objShell.Run strCommand, 1, True


DeleteOldFiles BaseDir, OlderThanDate
 
Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder
 
   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If file.DateLastModified < BeforeDate Then
           fso.DeleteFile file
      End If
   Next

End Function

Open in new window

Avatar of Richiep86
Richiep86

ASKER

ODBA - Excellent. Thank you, although a slight amendment if i may?

I only want to ZIP AND DELETE the logs which are OVER 30 days old. If they are NOT older than 30 days, can we ignore them?

Thanks for you prompt and professional answer :)

RIch
Avatar of oBdA
oBdA

Sorry, then you need to be a more specific.
In the initial question, you wrote:
1) Delete the old files from C:\TestFile where older than x days
2) The output.zip needs to increment each time the script is run, a time stamp would be ideal here
Now you write:
I only want to ZIP AND DELETE the logs which are OVER 30 days old. If they are NOT older than 30 days, can we ignore them?
Just to make sure I understood you correctly: Basically, you want to move only files older than a certain age to an archive, while keeping the more recent files in the same location?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Richiep86
Richiep86

ASKER

Thank you for your excellent advise and speedy responses,
Windows Batch
Windows Batch

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

13K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo