VBS脚本查找和删除文件 [英] VBS script find and delete file

查看:84
本文介绍了VBS脚本查找和删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找计算机上的特定文件并将其删除.

I am trying to find a specific file on computer and delete it.

这是我的代码:

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 

代码只能部分工作.当文件date.vbs"在根文件夹 (C:\date.vbs) 中时,它会被删除,但当它在文件夹 (C:\backup\date.vbs) 中时,它不会被删除.你知道我应该修改哪些代码才能删除文件,即使它不是在 root 中而是在计算机的任何地方吗?

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?

谢谢!五、

更新:

代码现在几乎可以正常工作了.我只有删除文件的最后一个问题.我可以将属性从只读更改为正常,但仍然收到拒绝访问的错误.

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.

这是我的代码:

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

感谢您的帮助!

我用来以管理员身份运行脚本的代码.在此之后,它开始显示 MessageBoxes.在它在控制台中运行之前.

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.

推荐答案

你的方法太复杂了.使用简单的递归函数:

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

这篇关于VBS脚本查找和删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆