title: VBS script find and delete file
body: <p>I am trying to find a specific file on computer and delete it.</p>
  
  <p>This is my code:</p>
  
  <pre><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) &gt; 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 " &amp; sDrive &amp; ":" 
  oWshShell.Run "cmd /c dir /s /b " &amp; sDrive &amp; ":\" &amp; sName &amp; " &gt;&gt;" &amp; sDir, 0, True 
  End Sub 
  </code></pre>
  
  <p>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?</p>
  
  <p>Thank you! V.</p>
  
  <p>UPDATE:</p>
  
  <p>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. </p>
  
  <p>This is my code:</p>
  
  <pre><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 &gt;= 0
  End Function
  </code></pre>
  
  <p>Thank you for help!</p>
  
  <p>Code I use to run the script as ADMIN. After this it started to show the MessageBoxes. Before it was running in a console.</p>
  
  <pre><code>If WScript.Arguments.Named.Exists("elevated") = False Then
  
      CreateObject("Shell.Application").ShellExecute "wscript.exe", """" &amp;     WScript.ScriptFullName &amp; """ /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
  </code></pre>
  
  <p>So I believe there is something wrong in this code.</p>