I'm new to VBS and need help creating several scripts. I'm trying to find and delete files and folders, I can do it if I know the path to the file or folder but I do not know the location of the files or folders.

Script #1 Search every folder/sub folder on C drive for a specific file, lets say "badfile.exe" and then delete it, pop up a message saying "badfile.exe deleted successfully" or "file not found".

Script #2 Search C drive for a folder, lets say "badfolder" then delete it and its content, pop up a message saying "badfolder deleted successfully" or "folder not found".

Script #3 Search C drive for a folder lets say "badprogram" then look for a file in that folder lets say "uninstall.exe" and run the file to uninstall the badprogram or pop up a message saying "file not found"

Thanks
1 Comment   [ + ] Show Comment

Comments

  • I assume there is suppose to be a box that pops up where I enter the file name to serch for and another to enterwhere I want it to start searching but it never pops up I get the following error on line

    53 MsgBox "Found it in: " & folder", 48, "File Found"

    VB Sripts\ninja.vbs(53, 32) Microsoft VBScript compilation error: Expected end of statement
Please log in to comment

Answers

0
Here's a generic script that shows some logic on scanning the drive.  It's not exactly what you're asking for, but should get you started.

Plus, I learned most of my scripting by my good friend Google and typing "VBSCRIPT Delete file".  Or something like that.  There are a lot of examples out there for the "borrowing".

dig through this a while and see what you get.
After the MSGBOX at the bottom (Line 53) that says:  "Found it..." you can put a "oFSO.DeleteFile File.Name" line.  Just be warned, it will delete it without asking.  You would have to put a question in there if you wanted to protect yourself.  

***Start Script***
Option Explicit

Dim oFSO
Dim RootFolder
Dim Startfolder, SearchFile


Set ofso = CreateObject("Scripting.FileSystemObject")

SearchFile = Inputbox ("Name of File to search for:" & vbcrlf & vbcrlf & "Enter name exactly as it would appear.", "File Name")
If SearchFile = "" Then
wscript.Quit
End If

Startfolder = Inputbox ("What folder do you want to start" & vbcrlf & "the search at?", "Start Folder")
If Startfolder = "" Then
wscript.Quit
End if

On Error Resume Next ' Ignore errors if folder not found
Set RootFolder = ofso.GetFolder(Startfolder)

' Check to see if the starting folder is a valid folder
IF NOT ofso.FolderExists(RootFolder) Then
MsgBox "Starting Folder does not exist.", 16, "Folder not found"
WScript.Quit
End If
On error goto 0 ' Turn on error checking

FindFiles RootFolder, SearchFile

Sub FindFiles(Folder, SearchFile)
   Dim SubFolders
   Dim Files
   Dim File
   Dim Folder2
   wscript.Echo folder

   Set SubFolders = Folder.SubFolders  '=== Get the collection of Folders in this folder
   Set Files = Folder.files     '=== Get the collection of Files in this folder

   '=== If there are subfolders, process them first.
IF SubFolders.Count <> 0 Then
For each Folder2 in SubFolders
FindFiles Folder2, SearchFile
Next
End If

   '=== If this folder isn't empty, process each file
IF Files.Count <> 0 Then
For Each File in Files
If File.name = SearchFile Then
MsgBox "Found it in: " & folder", 48, "File Found"
End If
Next
   End If

End Sub
Answered 04/06/2015 by: rjtopper
Senior Yellow Belt

  • I assume there is suppose to be a box that pops up where I enter the file name to search for and another to enter where I want it to start searching but they never pop up as I get the following error on line

    53 MsgBox "Found it in: " & folder", 48, "File Found"

    VB Sripts\ninja.vbs(53, 32) Microsoft VBScript compilation error: Expected end of statement
    • Change:

      MsgBox "Found it in: " & folder", 48, "File Found"

      to:

      MsgBox "Found it in: " & folder, 48, "File Found"
Please log in to comment
0

Just go to computerperformance.com. There are scripts of every kind imaginable there. You probably won't ever find a single script that does everything you want but you can mix and match.

If I can offer some advice, though...

- Always assume the worst, i.e. that *nothing* will work and error-trap everything, even creating well-known objects.

- Find and use a logging class (don't be put off by that word - it's just a collection of functions and property gets/sets) so that logging your script's activity becomes second nature and not something you tack on to the end as an after-thought.

- Test *every* scenario, even those that "won't ever happen" because you can bet that one day, "never" comes around!

Answered 04/07/2015 by: VBScab
Red Belt

Please log in to comment
0
Use the PowerShell Get-ChildItem cmdlet, this will greatly simplify what you are trying to do compared to the above examples.
Answered 04/07/2015 by: deliveryboy
Orange Senior Belt

Please log in to comment
Answer this question or Comment on this question for clarity