|
Answer» 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. Code: [Select]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.Quote a few WScript.Echo things might be missing, but it is mostly complete.
Maybe I'm just tired but I can't for the life of me see where the literal ">folder to delete" is generated.
On the other hand you may actually have folders with those names. Could you point out where that message originates?
realized what it was, I took 1 character off of the string for the ENDING / that was never there because I copied and pasted from below.
Would that make this line look something like this Code: [Select]strFolder = Left(objFile.Drive & folderPath, Len(objFile.Drive & folderPath) - 1)
Code: [Select]strFolder = objFile.Drive & folderPath Yeah, that works. Nice thing about VBS is you can use unnamed objects to slice and dice data without ever changing the underlying source data.
Quote from: Sidewinder on April 24, 2008, 04:33:55 PMYeah, that works. Nice thing about VBS is you can use unnamed objects to slice and dice data without ever changing the underlying source data.
Not unique to VBS by any means.
|