I have a script that I am working on for my internship. What it does is it searches the hard drive for given file names, and then if it finds the file names, it deletes the folder. Well the only problem is that if the filename is under a folder like C:\windows or C:\program files I don't want it to delete the entire folder, just the folder names and the individual files I list. Currently I am working with this code
(I hope it is the latest version, a few WScript.Echo things might be missing, but it is mostly complete.
Const ForReading = 1
strComputer = "."
i=0
Set fso = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set f = fso.OpenTextFile("H:\gameLocations\gameList.txt", ForReading)
Do Until f.AtEndOfStream = True
retString = f.ReadLine
fname = Split(retstring, ".")(0)
fext = Split(retString, ".")(1)
Set colFiles = objWMIService.ExecQuery _
("Select * From CIM_DataFile Where FileName = '" & fname & "'" & " and Extension = " & "'" & fext & "'" & "")
'WScript.Echo "Searching for " & retstring
For Each objFile in colFiles
If InStr(1, objFile.Caption, "c:\windows") > 0 Then
WScript.Echo "Testing"
WScript.Echo retstring & " Found In Windows...Not Deleting"
'Here is the start of my delete script
searchFileString = "H:\gameLocations\lists\" & fname & "_folders.txt"
WScript.Echo searchFileString
Set d = fso.OpenTextFile(searchFileString, ForReading)
Do Until d.AtEndOfStream = True
folderName = f.ReadLine
folderPath = objFile.Path & folderName
strFolder = Left(objFile.Drive & folderPath, Len(objFile.Drive & folderPath) - 1)
WScript.Echo strFolder
'fso.DeleteFolder strFolder, True
'Here is the end of my test script.
Loop
i=i+1
Else
If InStr(1, objFile.Caption, "c:\program files") > 0 Then
WScript.Echo retstring & " Found In Program Files...Not Deleting"
WScript.Echo "Maybe I will code something to delete all the individual files?"
'Here is the start of my delete script
searchFileString = fname & "_folders.txt"
Set d = fso.OpenTextFile(searchFileString, ForReading)
Do Until d.AtEndOfStream = True
folderName = f.ReadLine
folderPath = objFile.Path & folderName
strFolder = Left(objFile.Drive & folderPath, Len(objFile.Drive & folderPath) - 1)
WScript.Echo strFolder
'fso.DeleteFolder strFolder, True
'Here is the end of my test script.
Loop
i=i+1
Else
WScript.Echo retstring & " Found In " & objFile.Drive & objFile.Path
WScript.Echo "Removing Files/Folder " & objFile.Drive & objFile.Path
strFolder = Left(objFile.Drive & objFile.Path, Len(objFile.Drive & objFile.Path) - 1)
WScript.Echo "Maybe I will code something to delete all the individual files?"
fso.DeleteFolder strFolder, True
End If
End If
Next
Loop
If i > 0 Then
msgbox "Alert!, Found something in the windows directory or the program files." & Chr(13) & Chr(13) & "You might want to reimage the computer to take care of the problem."
Else
msgbox "Success, Computer is clean."
End If
f.Close
The only problem is that it outputs something like this.
If you notice, I have a folders list in the .txt file but on the cmd window it shows something like C:\windows\suppor meaning it lost the
t in support. It is not just that line, it is some of the other lines, but its not every line.