Reputation: 47
Supposedly I want to delete logfiles from the C:\ drive. (XXX-Log1.log XXX-Log2.log)
Question: if a file is deleted (I'm just using the del /f /q C:\*.log
command), there's obviously no output.
How can I write output to a logfile when a file is deleted and only then? I know for writing to a logfile you can use >>"D:\What\Ever\Deleted.log"
, but I'd like to have displayed which files were deleted, if any.
Upvotes: 0
Views: 2052
Reputation: 7095
Here is a vbscript you can use to monitor a folder for deletion events. This might do what you want. Just call it with cscript like cscript /nologo monitorfolder.vbs
You'll need to edit it to monitor in your path. I just used my C:\temp folder for testing.
MonitorFolder()
Function MonitorFolder()
intInterval = "2"
strDrive = "C:"
strFolder = "\\temp\\"
strComputer = "."
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.CreateTextFile("C:\temp\Deleted.log")
Set objWMIService = GetObject( "winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2" )
strQuery = _
"Select * From __InstanceOperationEvent" _
& " Within " & intInterval _
& " Where Targetinstance Isa 'CIM_DataFile'" _
& " And TargetInstance.Drive='" & strDrive & "'" _
& " And TargetInstance.Path='" & strFolder & "'"
Set colEvents = objWMIService.ExecNotificationQuery (strQuery)
WScript.Echo "Monitoring events...[Ctl-C] to end"
Do
Set objEvent = colEvents.NextEvent()
Set objTargetInst = objEvent.TargetInstance
Select Case objEvent.Path_.Class
Case "__InstanceDeletionEvent"
objFile.WriteLine(objTargetInst.Name)
End Select
Loop
End Function
Upvotes: 1
Reputation: 41234
If the folder does not have any subdirectories, then this will work:
@echo off
setlocal enableextensions
del /s "c:\folder\*.log" >file.log
pause
Upvotes: 1