Const FOR_READING = 1
strFolder = "c:\Users\novatec\Documents\Prueba"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files
For Each objFile In colFiles
abierto = False
IsOpened(objFile.name)
if not abierto then
objFile.Delete
end if
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
abierto = False
IsOpened(objFile.name)
if abierto = False then
objFile.Delete
end if
Next
ShowSubFolders(objSubFolder)
Next
End Sub
Sub IsOpened(sFilePath)
'La estrategia de esta funcion es tratar de abrir el archivo en modo Append (Editar)
'De tal modo que se pueda evaluar su estado, por defecto devolvara FALSE, que significa que NO ESTA ABIERTO.
'TRUE para cuando el archivo se encuentre ABIERTO
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim oFso: Set oFso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Dim nErr: nErr = 0
Dim sDesc: sDesc = ""
Dim oFile: Set oFile = oFso.OpenTextFile(sFilePath, ForAppending)
If Err.Number = 0 Then
'No ha ocurrido un error por lo que el archivo no esta abierto y se puede operar con el.
oFile.Close
abierto = False
Else
Select Case Err.Number
Case 70
' El archivo esta abierto ha mandado el error esperado
IsOpened = True
abierto = True
Wscript.Echo "Abierto:"+sFilePath
Case Else
' 52 - Bad file name or number
' 53 - File not found
' 76 - Path not found
IsOpened = True
nErr = Err.Number
sDesc = Err.Description
abierto = True
Wscript.Echo "Abierto:"+sFilePath
End Select
End If
Set oFile = Nothing
Set oFso = Nothing
On Error GoTo 0
' If nErr Then
' Err.Raise nErr, , sDesc
' End If
End Sub