Need help with VBS code to modify the content of a file

Hi,

We need modify the following file "exception.sites" (no extension) from the following path

C:\Users\username\AppData\LocalLow\Sun\Java\Deployment\security\

We need to add a few sites to the current "exception.sites" the sites have to be entered one per line, for example:

http://url1 
http://url2

The file can be modified by the users since they might be entering a site that has to be excluded from Java.

The script will run as part as the logon script and will add the sites to the local file on a per user basis.

We are trying to find a way to manage the new Java 7 U 51 exception list and warranty that the business sites are included in the exception.list file and also that the users will be able to manage their own exceptions.  

https://blogs.oracle.com/java-platform-group/entry/upcoming_exception_site_list_in

Anyone can give us a hand with the script?

Thank you!
llaravaAsked:
Who is Participating?
 
RobSampsonCommented:
Sure, I think just moving the CreateTextFile part out of the If statement should do that.

Regards,

Rob.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Open_as_Unicode = -1

Set objNetwork = CreateObject("WScript.Network")

strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Set dctURLs = CreateObject("Scripting.Dictionary")
dctURLs.CompareMode = vbTextCompare

' Add corporate URLs that are required here
dctURLs.Add "http://URL1", 0
dctURLs.Add "http://URL2", 0

If (FSO.FileExists(strPath) = True) Then
	Set objFile = FSO.OpenTextFile(strPath, ForReading, 0)
	' Read through the current list to maintain any user added URLs
	While Not objFile.AtEndOfStream
		strURL = Trim(objFile.ReadLine)
		If strURL <> "" Then
			If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
		End If
	Wend
	objFile.Close
	Set objFile = Nothing
End If
Set objFile = FSO.CreateTextFile(strPath, True, 0)
For Each strURL In dctURLs
	objFile.WriteLine strURL
Next
objFile.Close
Set objFile = Nothing

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
I recommend to change that to use a corporate (system) file and a user file, and merging those together on login. Otherwise you'll need to check whether the corporate sites are not already in the exception list - nothing really difficult, but sounds unnecessary.
0
 
llaravaAuthor Commented:
Yes, that makes sense. How do you get that implemented?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
llaravaAuthor Commented:
So far I have the following:

Const ForAppending = 8
Const Open_as_Unicode = True

strFolder = "C:\Users\testuser\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

If (FSO.FileExists(strPath) = True) Then
  Set objFile = FSO.OpenTextFile(strPath, ForAppending, Open_as_Unicode)
  objFile.Writeline("test")
    objFile.Close : Set objFile = Nothing
End If

But I need to change - strFolder = "C:\Users\testuser\AppData\LocalLow\Sun\Java\Deployment\security" for a variable that captures the different users.

Also a way to control that sites are only added one time.

Could someone help to adjust to script?

Thank you!
0
 
RobSampsonCommented:
Hi, I haven't tested this yet, but it should work.

Regards,

Rob.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Open_as_Unicode = True

Set objNetwork = CreateObject("WScript.Network")

strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Set dctURLs = CreateObject("Scripting.Dictionary")
dctURLs.CompareMode = vbTextCompare

' Add corporate URLs that are required here
dctURLs.Add "http://URL1", 0
dctURLs.Add "http://URL2", 0

If (FSO.FileExists(strPath) = True) Then
	Set objFile = FSO.OpenTextFile(strPath, ForReading, Open_as_Unicode)
	' Read through the current list to maintain any user added URLs
	While Not objFile.AtEndOfStream
		strURL = Trim(objFile.ReadLine)
		If strURL <> "" Then
			If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
		End If
	Wend
	objFile.Close
	Set objFile = Nothing
	Set objFile = FSO.CreateTextFile(strPath, True, Open_as_Unicode)
	For Each strURL In dctURLs
		objFile.WriteLine strURL
	Next
	objFile.Close
	Set objFile = Nothing
End If 

Open in new window

0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That script assumes that the root folder for user profiles is C:\Users, but I reckon that's sufficient in your case. Since it is only adding URLs, you can't tell to remove, but should you need that too, it is nothing difficult to implement.
0
 
llaravaAuthor Commented:
Hi Rob,

Thanks for your reply.

I have executed the script 3 times and the URLs are added to the file every time the script is executed as shown below,

Notice how the first time the URL's are being added correctly then they are added again in a strange format, etc.

Since the script is going to run as part as the login script can the URL's be added just once.  

http://URL1
http://URL2
ÿþh t t p : / / U R L 1
 
 h t t p : / / U R L 2
 
 ÿ þ h   t   t   p   :   /   /   U   R   L   1  
   
 
   h   t   t   p   :   /   /   U   R   L   2  

Any thoughts?
0
 
RobSampsonCommented:
Hmmm, try changing
Const Open_as_Unicode = True

Open in new window


to
Const Open_as_Unicode = -1

Open in new window


Rob.
0
 
llaravaAuthor Commented:
I I have made the change as indicated but still


I have made the changed as indicated. I have executed the script 3 times again and this is the content of the file.

http://URL1
http://URL2
ÿþh t t p : / / U R L 1
 
 h t t p : / / U R L 2
 
 ÿ þ h   t   t   p   :   /   /   U   R   L   1  
   
 
   h   t   t   p   :   /   /   U   R   L   2  
   
 
   ÿ   þ
0
 
RobSampsonCommented:
Perhaps it's not supposed to be Unicode....try this.

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const Open_as_Unicode = -1

Set objNetwork = CreateObject("WScript.Network")

strFolder = "C:\Users\" & objNetwork.UserName & "\AppData\LocalLow\Sun\Java\Deployment\security"
strFile = "exception.sites"

strPath = strFolder & "\" & strFile
Set FSO = CreateObject("Scripting.FileSystemObject")

Set dctURLs = CreateObject("Scripting.Dictionary")
dctURLs.CompareMode = vbTextCompare

' Add corporate URLs that are required here
dctURLs.Add "http://URL1", 0
dctURLs.Add "http://URL2", 0

If (FSO.FileExists(strPath) = True) Then
	Set objFile = FSO.OpenTextFile(strPath, ForReading, 0)
	' Read through the current list to maintain any user added URLs
	While Not objFile.AtEndOfStream
		strURL = Trim(objFile.ReadLine)
		If strURL <> "" Then
			If dctURLs.Exists(strURL) = False Then dctURLs.Add strURL, 0
		End If
	Wend
	objFile.Close
	Set objFile = Nothing
	Set objFile = FSO.CreateTextFile(strPath, True, 0)
	For Each strURL In dctURLs
		objFile.WriteLine strURL
	Next
	objFile.Close
	Set objFile = Nothing
End If 

Open in new window


If that still fails, try to copy an unmodified file back to your user account and try the script again.

Rob.
0
 
llaravaAuthor Commented:
Hi,

I made the change and the script works like a champ. One more quick question. The "exception.sites" doesn't get created until the user executes the Java Exception List and adds a file to it.

Could the script create the the file "exception.sites" in the following path:

User\AppData\LocalLow\Sun\Java\Deployment\security\

Then append the sites in the script to it.

Finally if the file "exception.sites" exists then do not create the file but execute the part of the script that adds/checks if the internal sites are there.

Thank you!
0
 
llaravaAuthor Commented:
PERFECT! Thank you!
0
 
RobSampsonCommented:
No problem. Thanks for the grade.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.