Batch removal of single file from server list….

Since I posted how to deploys splash page using VBscript I figured it was only appropriate to post an article on how to remove it. Original post here.


Requirements:
1. VBscripting skills.
2. splash.html (HTML file that will be removed).
3. servers.txt (txt file listing each server to delete/remove the splash.html file from).
4. Assumes you have appropriate user privileges.


dim filePath,mapPoint, netStr, filename
On Error Resume Next

'VB objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network") 
Set logfile = objFSO.CreateTextFile("logfile.txt",True)

'main loop
ForReading = 1
	Set objTextFile = objFSO.OpenTextFile("servers.txt", ForReading)
	Do Until objTextFile.AtEndOfStream
	
		strComputer = objTextFile.ReadLine
		strComputer = UCASE(strComputer)
		logfile.WriteLine(Now & "| Current Server: " & strComputer)

Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
Set colFiles = objWMIService. _
    ExecQuery("SELECT * FROM CIM_DataFile WHERE Path = '\Documents and Settings\All Users\Start Menu\Programs\Startup\' " & _
        "AND Drive = 'C:' AND Extension = 'htm'")

logfile.WriteLine(Now & "| Number of .htm files found: " + colFiles.Count)

If colFiles.Count = 0 Then
	logfile.WriteLine(Now & "| ERROR!! -- No .htm files found in Startup for :"  + strComputer)
End If

For Each item in colFiles
	filename = item.FileName + ".htm"
	filePath = "Q:" + item.FileName + ".htm"

	netStr = "\" + strComputer + "c$" + item.Path
	netStr = Left(netStr,Len(netStr)-1)
	
	objNetwork.MapNetworkDrive "Q:", netStr
	WScript.Sleep(750)

	If objFSO.FileExists(filePath) Then
 	  	objFSO.DeleteFile(filePath)
		logfile.WriteLine(Now & "| " & filename + " was successfully deleted from " + strComputer)
 	End If
	
        'give time for Windows to unmap the drive
	WScript.Sleep(250)
	objNetwork.RemoveNetworkDrive "Q:"	
	
Next ' For Each loop

Loop ' main loop

Leave a Reply

↓