Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-21
13
Medium Priority
?
1,156 Views
Last Modified: 2014-01-30
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!
0
Comment
Question by:llarava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 39797937
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
 

Author Comment

by:llarava
ID: 39797972
Yes, that makes sense. How do you get that implemented?
0
 

Author Comment

by:llarava
ID: 39798005
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 65

Expert Comment

by:RobSampson
ID: 39798442
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 39798550
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
 

Author Comment

by:llarava
ID: 39798984
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 39799038
Hmmm, try changing
Const Open_as_Unicode = True

Open in new window


to
Const Open_as_Unicode = -1

Open in new window


Rob.
0
 

Author Comment

by:llarava
ID: 39799067
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 39799104
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
 

Author Comment

by:llarava
ID: 39799872
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
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 39801363
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
 

Author Comment

by:llarava
ID: 39807782
PERFECT! Thank you!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 39808262
No problem. Thanks for the grade.

Rob.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question