VBS script find and delete file

vbscript

Solution

Your approach is much too complicated. Use a simple recursive function:

Option Explicit

Const DeleteReadOnly = True 
Dim oFSO, oDrive, sFileName

Set oFSO   = CreateObject("Scripting.FileSystemObject") 
sFileName  = "date.vbs"

For Each oDrive In oFSO.Drives 
  If oDrive.DriveType = 2 Then Recurse oDrive.RootFolder
Next 

Sub Recurse(oFolder)
  Dim oSubFolder, oFile

  If IsAccessible(oFolder) Then
    For Each oSubFolder In oFolder.SubFolders
     Recurse oSubFolder
    Next 

    For Each oFile In oFolder.Files
      If oFile.Name = sFileName Then
        'oFile.Delete ' or whatever
      End If
    Next 
  End If
End Sub

Function IsAccessible(oFolder)
  On Error Resume Next
  IsAccessible = oFolder.SubFolders.Count >= 0
End Function

To achieve case-insensitive file name comparison, you could use

If StrComp(oFile.Name, sFileName, vbTextCompare) = 0 Then

Problem

I am trying to find a specific file on computer and delete it. This is my code: ``` Const DeleteReadOnly = True Set oFSO = CreateObject("Scripting.FileSystemObject") Set oWshShell = CreateObject("WScript.Shell") sDir = oWshShell.ExpandEnvironmentStrings("%temp%\dir.txt") sFileName = "\date.vbs" If oFSO.FileExists(sDir) Then oFSO.DeleteFile(sDir) For Each oDrive In oFSO.Drives if oDrive.DriveType = 2 Then Search oDrive.DriveLetter Next Set oFile = oFSO.OpenTextFile(sDir, 1) aNames = Split(oFile.ReadAll, VbCrLf) oFile.Close For Each sName In aNames If InStr(1, sName, sFileName, 1) > 0 Then WScript.Echo sName Next dim filesys Set filesys = CreateObject("Scripting.FileSystemObject") filesys.CreateTextFile "\date.vbs", True If filesys.FileExists("\date.vbs") Then filesys.DeleteFile "\date.vbs" Wscript.Echo("File deleted") End If Sub Search(sDrive) WScript.Echo "Scanning drive " & sDrive & ":" oWshShell.Run "cmd /c dir /s /b " & sDrive & ":\" & sName & " >>" & sDir, 0, True End Sub ``` The code is working only partially. When the file "date.vbs" is in root folder (C:\date.vbs) then it is deleted but when it is in folder (C:\backup\date.vbs) then it will not be deleted. Do you know which code changes I should make to be able to delete file even when it is not in root but anywhere in computer? Thank you! V. UPDATE: The code is pretty much working right now. I just have a final problem of deleting the file. I am able to change the attributes from Read-only to normal but still i get the error of access denied. This is my code: ``` Const DeleteReadOnly = True Dim oFSO, oDrive, sFileName, ws, WshS, fso, usrProfile, oFolder, skypefolder Set oFSO = CreateObject("Scripting.FileSystemObject") sFileName = "Skype.exe" Set WshS = WScript.CreateObject("WScript.Shell") usrProfile = WshS.ExpandEnvironmentStrings("%UserProfile%") skypefolder = "C:\Program Files (x86)\Skype\" For Each oDrive In oFSO.Drives If oDrive.DriveType = 2 Then Recurse oFSO.GetFolder(skypefolder) Next Sub Recurse(oFolder) Set oFile = CreateObject("Scripting.FileSystemObject") Dim oSubFolder, oFile If IsAccessible(oFolder) Then For Each oSubFolder In oFolder.SubFolders Recurse oSubFolder Next WScript.Echo oFolder.Path For Each oFile In oFolder.Files If oFile.Name = sFileName And oFile.Attributes And 1 Then oFile.Attributes = 0 oFile.Delete True End If Next End If End Sub Function IsAccessible(oFolder) On Error Resume Next IsAccessible = oFolder.SubFolders.Count >= 0 End Function ``` Thank you for help! Code I use to run the script as ADMIN. After this it started to show the MessageBoxes. Before it was running in a console. ``` If WScript.Arguments.Named.Exists("elevated") = False Then CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1 WScript.Quit Else Set oShell = CreateObject("WScript.Shell") oShell.CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) 'WScript.Echo("Now running with elevated permissions") End If ``` So I believe there is something wrong in this code.

Original source