Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting lines from logon scripts which point to non-existing drives.

Status
Not open for further replies.

johnmoore83

Technical User
Jun 28, 2011
2
GB
Hi I have loads of logon scripts which point to drives which aren't available any more.

So I need a VBScript which goes through each .vbs logon script and deletes a particular line.

I have created a text file c:\names.txt which points to where the .VBS logon files are located. so in the txt file it has

c:\new\personsname1.vbs
c:\new\personsname2.vbs

The script I have developed so far deletes all the contents of the last vbs script and doesn't delete the info in the first one.

-----------------------------------------------------------------

Const ForReading = 1 ' Open file read only
Const ForWriting = 2 ' Open file read write
Set objDictionary = CreateObject("Scripting.Dictionary") ' Creates an array

' Variables
Dim objFso, objFile, strFileName, strNextLine, StrTextall, strLine, strNewContents

'----------------------------------------------------------------

' Specify text file path and assigns it to the variable strfilename
strFileName = "c:\names.txt"

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(strFileName, 1)
i = 0
' If text file exists then do this
If objFSO.FileExists(strFileName) Then

' Reads each line of c:\names.txt and stores each line as strNextLine
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
' Stores each line in objdicionary as strnextline
WScript.Echo("File Location: " & StrNextLine)

Loop
'Opens the vbs script file and reads all contents and stores it as strTextall
Set objFile = objFso_OpenTextFile(strNextLine, ForReading)
StrTextall = ObjFile.ReadAll

' Searches vbs file for text
Do Until objFile.AtEndOfStream
strNextLine = objFile.ReadLine

If InStr(strLine, "\\tasrv") = 0 and InStr(strLine, "\\erp_srv") = 0 Then

strNewContents = Replace(StrNextLine, "\\tasrv", "blank")
strNewContents = Replace(StrNextLine, "\\erp_srv", "blank")

End If

Loop
' Write out file
Set ObjFile = objFSO.OpenTextFile(strNextLine, ForWriting)
objFile.WriteLine strNewContents ' Write out the string contents
objFile.Close ' close the file

objFile.Close ' close the file
Else
'catch exceptions how you please
MsgBox "The ini file: " & strFileName & " does not exist. No changes will be made."
End If
 
Why a dictionary you don't use ?
Why ReadAll and then a loop with ReadLine ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
There are a number of improvements that could be made, but to address the first problem you will need to loop through the dictionary you have and perform actions on each item in your dictionary.

Something like:

Code:
if objDictionary.count<>0 then
  for each key in objDictionary.keys
    'your code to loop through the file and change as necessary
    '...
    '...
  next
end if
 
I have made a few improvements and got it to work, but it just deletes the following in each of the .vbs logon files.

'objNetwork.MapNetworkDrive "N:", "blank"

I need it to delete this whole line that is where I am stuck now.


Code:
Const ForReading = 1 ' Open file read only
Const ForWriting = 2 ' Open file read write
 Set objDictionary = CreateObject("Scripting.Dictionary") ' Creates an array

' Variables
Dim objFso, objFile, strFileName, strNextLine, strNewText
 
'--------------------------------------------------------------------------------

' Specify text file path and assigns it to the variable strFileName
strFileName = "c:\names.txt"
 
Set objFso = CreateObject("Scripting.FileSystemObject")
     Set objTextFile = objFSO.OpenTextFile(strFileName, 1)
i = 0
' If text file exists then do this
If objFSO.FileExists(strFileName) Then
	 
	' Reads each line of c:\names.txt and stores each line as strNextLine  
	Do Until objTextFile.AtEndOfStream 
          strNextLine = objTextFile.Readline
          objDictionary.Add i, strNextLine
          i = i + 1
	  ' Stores each line in objdicionary as strnextline
	  WScript.Echo("File Location: " & StrNextLine)
	
	'Opens the vbs script file and reads all contents and stores it as strNewText
	Set objFile = objFso.OpenTextFile(strNextLine, ForReading)
	strNewText = ObjFile.ReadAll
	objFile.Close
	
	If InStr(strNEWText, "\\tasrv") = 0 then

	strNewContents = strNewContents & strNewText & vbCrLf

	END If

	strNewText = Replace(strNewText, "\\tasrv", "blank")
	'WScript.Echo("value: " & StrNewText)

	Set objFile = objFSO.OpenTextFile(StrNextLine, ForWriting)
	objFile.WriteLine strNewText
	objFile.Close ' close the file
	
	Loop
	
Else
	'catch exceptions how you please
	MsgBox "The ini file: " & strFileName & " does not exist.  No changes will be made."
End If
 
I think you'll want to go back to the "reading line by line" strategy. If "\\tasrv" is not in the line, write the line to the new file; if it is in the line, do nothing (or write something to a log file). Read next line and repeat the checks.
 
open the vbs and read each line. if the line contains content you don't want skip it, otherwise write to a 2nd file. no dictionary needed

Code:
Const ForReading = 1 ' Open file read only
Const ForWriting = 2 ' Open file read write

strNamesFile = "c:\names.txt"
set objFso = CreateObject("Scripting.FileSystemObject")

if (objFSO.FileExists(strNamesFile)) Then
    set objNamesFile = objFso.OpenTextFile(strNamesFile, ForReading)
    do while not (objNamesFile.AtEndOfStream)
        strFile = trim(objNamesFiles.ReadLine)
        if (objFSO.FileExists(strFile)) then
			set objFile = objFso.OpenTextFile(strFile, ForReading)
			strContents = ""
			do while not (objFile.AtEndOfStream)
				strLine = objFile.ReadLine
				if not (inStr(strLine, "\\tasrv")) then
					strLine = Replace(strLine, "\\tasrv", "blank")
					strContents = strContents & strLine & vbCrLf
				end if
			loop
			objFile.close
			set objFile = objFSO.OpenTextFile(strFile, ForWriting)
			objFile.WriteLine strContents
			objFile.Close
		else
			msgbox "The file: " & strFile & " does not exist.  No changes will be made."
		end if
	loop
	objNamesFile.close
else
	msgbox "The file: " & strFile & " does not exist.  No changes will be made."
end if
-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top