Post your useful CMD scripts
Since the post is already resurected...
Here is my take on the "Delete files older then 7 days" script. The problem I've always had with that concept is, if you are pruning a folder that get log dumps or backups or something, you ONLY want to delete things if there are new data to take its place! So this script will instead keep 7 days worth of files in the target folder. It doesn't matter how many days have been between the oldest and newest, or how many files are modfied on 1 given day, it will keep 7 days of files.
In the marked section, edit the 3 constants to customize the file. Set the DEBUGOUTPUT to FALSE if you are running from a sceduled task or something, so there is no output the console. Comment out the "objFSO.DeleteFile(objRS.Fields("Path"))" line, and it will run as a test, reporting any files it WOULD delete as successful, but NOT deleting anything. It's a VBScript, so name it PruneFiles.vbs or whatever and run it as CScript PruneFiles.vbs (batch files suck.. ), Enjoy.
CONST adFldIsNullable = &H00000020
CONST adVarChar = 200
CONST adDate = 7
CONST adOpenDynamic = 2
CONST adUseClient = 3
Const ForReading = 1, ForWriting = 2, ForAppending = 8
' Set DEBUGOUTPUT to true to output to the console as well as logfile
CONST DEBUGOUTPUT = TRUE
Dim objFSO
Dim objFolder, colFiles, File
Dim objRunningLogFile
Dim objRS, intFailedDays, intSuccessfulDays, dteWorkingDate, dteNewestDate
Dim intDeleteFilesCount
' ******************************************************************************************
' Customization section:
' strRunningLogFile will contain lists all last successful days, failed days, and deleted files
' strTargetFolder is the location to scan for files, not recrusive
' DaysOfBackupsToKeep is # of days worth of data to store. If the backups are weekly, set to 30 or more
' ******************************************************************************************
CONST strRunningLogFile = "C:\Scripts\RunningLog.txt"
CONST strTargetFolder = "c:\test"
CONST DayOfBackupsToKeep = 7
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objfolder = objFSO.GetFolder(strTargetFolder)
Set objRS = CreateObject("ADODB.Recordset")
With objRS
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Fields.Append "Name", adVarChar, 255, adFldIsNullable
.Fields.Append "Path", adVarChar, 255, adFldIsNullable
.Fields.Append "DateMod", adDate, 32, adFldIsNullable
.Open
End With
Set colFiles = objFolder.Files
For Each File in colFiles
With objRS
.AddNew
.Fields("Name") = File.Name
.Fields("Path") = File.Path
.Fields("DateMod") = File.DateLastModified
.Update
End With
Next
objRS.Sort = "DateMod DESC"
If NOT objRS.EOF Then
objRS.MoveFirst
End If
intFailedDays = 0
intSuccessfulDays = 0
intDeleteFilesCount = 0
dteWorkingDate = Date
Set objRunningLogFile = objFSO.OpenTextFile(strRunningLogFile, ForAppending, True)
objRunningLogFile.WriteLine vbCrLf & "------------------------------------------------------------------"
objRunningLogFile.WriteLine "Run time: " & Now
If DEBUGOUTPUT Then
WScript.Echo "Report for " & strTargetFolder & " on " & Now
End IF
If NOT objRS.EOF Then
objRunningLogFile.WriteLine "Most Recent Backup File dated: " & ObjRS.Fields("DateMod") & ", " &_
DateDiff("d", ObjRS.Fields("DateMod"), Date) & " day(s) ago."
If DEBUGOUTPUT Then
WScript.Echo "Most Recent Backup File dated: " & ObjRS.Fields("DateMod") & ", " &_
DateDiff("d", ObjRS.Fields("DateMod"), Date) & " day(s) ago."
End If
End If
Do Until objRS.EOF
If intSuccessfulDays < DayOfBackupsToKeep Then
If DateDiff("d", objRS.Fields("DateMod"), dteWorkingDate) = 0 Then
objRunningLogFile.WriteLine dteWorkingDate & ": " & vbTab & objRS.Fields("Name")
If DEBUGOUTPUT Then
WScript.Echo dteWorkingDate & ": " & vbTab & objRS.Fields("Name")
End If
intSuccessfulDays = intSuccessfulDays +1
If intSuccessfulDays = 1 Then
objRunningLogFile.WriteLine "Newest File: " & objRS.Fields("Name") & " - Modified: " & objRS.Fields("DateMod")
End If
objRS.MoveNext
Do Until objRS.EOF
If DateDiff("d", objRS.Fields("DateMod"), dteWorkingDate) = 0 Then
objRunningLogFile.WriteLine vbTab & vbTab & objRS.Fields("Name")
If DEBUGOUTPUT Then
Wscript.Echo vbTab & vbTab & objRS.Fields("Name")
End If
objRS.MoveNext
Else
dteWorkingDate = DateAdd("d", -1, dteWorkingDate)
Exit Do
End if
Loop
Else
If DateDiff("d", Date, dteWorkingDate) <> 0 Then
objRunningLogFile.WriteLine dteWorkingDate & ": No Backup Found!!"
intFailedDays = intFailedDays + 1
If DEBUGOUTPUT Then
Wscript.Echo dteWorkingDate & ": No Backup Found!!"
End If
End If
dteWorkingDate = DateAdd("d", -1, dteWorkingDate)
End If
Else
On Error Resume Next
objFSO.DeleteFile(objRS.Fields("Path"))
If Err.Number = 0 Then
intDeleteFilesCount = intDeleteFilesCount + 1
objRunningLogFile.WriteLine objRS.Fields("Path") & " Deleted Successfully."
If DEBUGOUTPUT Then
WScript.Echo objRS.Fields("Path") & " Deleted Successfully."
End If
Else
objRunningLogFile.WriteLine objRS.Fields("Path") & " CANNOT BE DELETED!! Error was " &_
CStr(Err.Number) & " " & Err.Description
If DEBUGOUTPUT Then
WScript.Echo objRS.Fields("Path") & " CANNOT BE DELETED!! Error was " &_
CStr(Err.Number) & " " & Err.Description
End If
Err.Clear
End If
On Error Goto 0
objRS.MoveNext
End If
Loop
objRunningLogFile.WriteLine vbCrLf & "Successful Days: " & intSuccessfulDays
objRunningLogFile.WriteLine "Failed Days: " & intFailedDays
objRunningLogFile.WriteLine "Deleted Files: " & intDeleteFilesCount
objRunningLogFile.Close
If DEBUGOUTPUT Then
WScript.Echo vbCrLf & "Successful Days: " & intSuccessfulDays
WScript.Echo "Failed Days: " & intFailedDays
WScript.Echo "Deleted Files: " & intDeleteFilesCount
End If
0 thoughts to “Useful batch files download”