Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Re: Working with Text File

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,1984
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Re: Working with Text File
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: <<   < prev  1 [2]
Login
Message << Older Topic   Newer Topic >>
 Re: Working with Text File - 2/6/2005 9:24:11 AM   
  tnoonan

 

Posts: 364
Score: 0
Joined: 12/14/2004
From:
Status: offline
Token did all the work I just added my 2 cent's Nice job token.

(in reply to Bushmen)
 
 
Post #: 21
 
 Re: Working with Text File - 2/6/2005 11:09:47 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Bushmen/tnoonan,

Once again, no problem, glad I could help; it's all about sharing information and help each other learn, at least that's what I believed.

BTW, make sure you change all the tabs in ports.txt to space before you change the delim variable.

(in reply to Bushmen)
 
 
Post #: 22
 
 Re: Working with Text File - 2/7/2005 9:07:48 PM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
Token, Tnoonan, your help needed again.

Basically the requirement has changed slightly.. I apologise for the inconvenience.

1. My requirement is now that I need every computer to be assigned the next 2 available ports in the ports.txt file, i.e. 9000 PC1, 9005 PC1, 9009 PC2, 9010 PC2... which ever two is next, as long as every computer has 2 seperate ports.

2. Then another requirement is that I have the two portnumbers, eg. Port1var, port2var in variables, so that I can use them in my application in variables in an ini file. If I could get them into 2 seperate variables, i will be able to use them for the ini file.


3.On uninstall, as before we need to remove the computername, but obviously remove the computername for the two ports that its written.

4.Then the last thing is they want a message box coming up when the last port has been used, which is 9999. If I could get these port variables, i could possibly check whether they're > 9998 and then have a message box to take care of that.. so that one i'll be able to do.

I know you guys have already spent a lot of time on this post, but I would be greatful if you can help me sort it out once and for all.

Thanks for your help in advance.

REgards,
Bushmen

(in reply to Bushmen)
 
 
Post #: 23
 
 Re: Working with Text File - 2/8/2005 1:57:50 AM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
token,

i've just done more testing and it seems that the script deletes all occurances of the computername anyway, so that is great.

basically the most importand ones to add is no 1 and 2 of the previous post, then I'll try and do no 4.

Regards and hope you could help me with this.

ben

(in reply to Bushmen)
 
 
Post #: 24
 
 Re: Working with Text File - 2/8/2005 7:37:25 AM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
Token, Tnoonan, anyone..

Not sure if you guys are around.. apologies for hassling you like this..

I need your help on the last bit to add in this script as per previous 2 posts..

I've tried everything, but i can not get the script to write the computername to the next 2 ports instead of the original next 1 available port.

The variables i would be able to do.. i found out from a debugging tool where to look for the variable.. its just to add the computername for a 2nd time that i am having problems with.. not sure since its in a loop how to make it do the loop a 2nd time without it deleting the name.

regards and hope to hear from you guys.

bushmen

(in reply to Bushmen)
 
 
Post #: 25
 
 Re: Working with Text File - 2/8/2005 11:14:10 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
quote:
Originally posted by Bushmen
2. Then another requirement is that I have the two portnumbers, eg. Port1var, port2var in variables, so that I can use them in my application in variables in an ini file. If I could get them into 2 seperate variables, i will be able to use them for the ini file.



You mean environment variables ? I don't see how the application is going to use them. Or is it simply output those 2 ports and let the application pick it up from standard output ?

(in reply to Bushmen)
 
 
Post #: 26
 
 Re: Working with Text File - 2/8/2005 11:33:19 AM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
nope, just variables in the script which i can use in my application..

basically, if you set them to port and port2 or anything that i know of, then i can use it later in the script to populate an ini file.

when adding the computername, it seems to also add a blank line at the end.. is there a reason for that?

regards,

ben

(in reply to Bushmen)
 
 
Post #: 27
 
 Re: Working with Text File - 2/8/2005 3:39:08 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
The list of ports used are stored in the port dictionary object, you can access the keys using:
for each key in port.keys
wscript.echo key
next

blank line at the end ? what end ? End of file ?

(in reply to Bushmen)
 
 
Post #: 28
 
 Re: Working with Text File - 2/8/2005 8:35:06 PM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
hi token

yes, the end of the file..

when i add the computername it adds a blank line to the end of the file.. when i remove a computername, it removes it..

i want to try that that line is not added at the end when writing the file into a new file since a lot of computers will add their names to this file, and we might get a lot of blank lines at the end of the file.

bushmen

(in reply to Bushmen)
 
 
Post #: 29
 
 Re: Working with Text File - 2/9/2005 1:40:47 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Haven't got time to see why it inserts a blank line at teh end of the file, but it does this on both operations; adding and removing computers. Because there is only one instance of a line stored, it won't add more than one blank line.

I guess I don't need to explain the code below anymore, have fun :)

==============================================================================
Option Explicit

Dim fso, portDir, portFile, input, temp, hash, key, network, port, found, tempFile, tempFileName, delim, comment, re, perPort, count, totalPorts, lastPorts, msg

Set fso = CreateObject("Scripting.FileSystemObject")
Set hash = CreateObject("Scripting.Dictionary")
Set port = CreateObject("Scripting.Dictionary")
Set network = CreateObject("WScript.Network")

portDir = "F:\1" ' change this value to the location of the port file
portFile = "ports.txt" ' change this value to the name of the file containing list of ports to be used.
perPort = 2 ' change this value to the number of ports required per application
totalPorts = 10000 ' change this value to the actual number of ports
delim = vbTab ' change this value to the desired delimiter used in the port file separting the port nubmer and computer name
comment = ";" ' change this value to the desire comment character
found = False
count = 0
lastPorts = ""

Set re = New RegExp
re.Global = True
re.Pattern = delim & "+"

If fso.FileExists(fso.BuildPath(portDir,portFile)) Then
Set input = fso.OpenTextFile(fso.BuildPath(portDir,portFile),1)
Do Until input.AtEndOfStream
temp = re.Replace(Trim(input.ReadLine),delim)
If temp = "" Or Left(temp,1) = comment Then
If (Not hash.Exists(temp)) Then
hash.Item(temp) = ""
End If
Else
If (Not hash.Exists(Split(temp,delim)(0))) Then
If UBound(Split(temp,delim)) < 1 Then
hash.Item(Split(temp,delim)(0)) = ""
If Not port.Exists(Split(temp,delim)(0)) And port.Count <= perPort -1 Then
port.Item(Split(temp,delim)(0)) = True
End If
Else
if Split(temp,delim)(1) = "" Then
hash.Item(Split(temp,delim)(0)) = ""
If Not port.Exists(Split(temp,delim)(0)) And port.Count <= perPort -1 Then
port.Item(Split(temp,delim)(0)) = True
End If
Else
If UCase(network.ComputerName) = UCase(Split(temp,delim)(1)) Then
hash.Item(Split(temp,delim)(0)) = ""
found = True
Else
hash.Item(Split(temp,delim)(0)) = Split(temp,delim)(1)
count = count + 1
End If
End If
End If
End If
End If
Loop
input.Close
If found = False And port.Count > 0 Then
For Each key In port.Keys
hash.Item(key) = network.ComputerName
lastPorts = Trim(lastPorts & " " & key)
Next
lastPorts = Replace(lastPorts," ",", ")
End If

tempFileName = fso.GetTempName
Set tempFile = fso.CreateTextFile(fso.BuildPath(portDir,tempFileName), True)

For Each key In hash
If key = "" Then
tempFile.WriteLine
Elseif Left(key,1) = comment Then
tempFile.WriteLine key
Else
tempFile.WriteLine key & delim & hash.Item(key)
End If
Next
tempFile.Close
fso.DeleteFile fso.BuildPath(portDir,portFile), True
fso.MoveFile fso.BuildPath(portDir,tempFileName), fso.BuildPath(portDir,portFile)
If found = False Then
If count + perPort = totalPorts Then
If port.Count <= 1 Then
msg = "Warning! All " & totalPorts & " available ports have been used." & VbCrLf & VbCrLf & "The last " & port.Count & " port used is: " & lastPorts
Else
msg = "Warning! All " & totalPorts & " available ports have been used." & VbCrLf & VbCrLf & "The last " & port.Count & " ports used are: " & lastPorts
End If
MsgBox msg, 16, "No more ports are available."
Elseif count + perPort > totalPorts Then
If port.Count <= 1 Then
If port.count = 0 Then
msg = "Warning! All " & totalPorts & " available ports have been used." & VbCrLf & VbCrLf & "There are no more ports left."
Else
msg = "Warning! All " & totalPorts & " available ports have been used." & VbCrLf & VbCrLf & "The last " & port.Count & " port used is: " & lastPorts
End If
If perPort > port.count Then
msg = msg & VbCrLf & VbCrLf & perPort - port.count & " out of " & perPort & " port(s) are still required."
End If
MsgBox msg , 16, "No more ports are available."
Else
msg = "Warning! All " & totalPorts & " available ports have been used." & VbCrLf & VbCrLf & "The last " & port.Count & " ports used are: " & lastPorts
If perPort > port.count Then
msg = msg & VbCrLf & VbCrLf & perPort - port.count & " out of " & perPort & " port(s) are still required."
End If
MsgBox msg , 16, "No more ports are available."
End If
End If
End If
Else
WScript.Echo "File cannot be found: " & portFile
End If

(in reply to Bushmen)
 
 
Post #: 30
 
 Re: Working with Text File - 2/9/2005 3:48:15 AM   
  CaffeineAddiction

 

Posts: 144
Score: 0
Joined: 2/9/2005
From:
Status: offline
This is a snippet taken from

http://www.microsoft.com/technet/scriptcenter/resources/qanda/jan05/hey0131.mspx

and should answer all of Tokens questions about writing to an Excell Spread sheet ... it works tons better than using arrays and comma delemited crap ... take a look


How Can I Make Changes to and Then Re-Save an Existing Excel Spreadsheet?
Q

Hey, Scripting Guy! How can I open an existing Excel spreadsheet, add some additional information to that spreadsheet, and then save my changes? Every time I call the SaveAs method a dialog box pops up asking me if I want to save my changes.

-- RW
A

Hey, RW. Generally speaking, you can avoid that dialog box by using the Save method rather than the SaveAs method. If you open an existing spreadsheet and make some changes to it, just call the Save method; that should save your document without displaying the confirmation dialog box. For example, hereÒs a sample script that opens the file C:\Scripts\Test.xls, writes the current date and time into cell A1, saves the file, and then quits:

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\Test.xls")
Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1, 1).Value = Now
objWorkbook.Save()
objExcel.Quit

In most cases, that should do the trick. However, itÒs also true that the SaveAs method offers more options than the Save method. If you want to add password protection to the spreadsheet, create a backup version of the file, or save in another format, youÒll need to use SaveAs. Likewise, suppose that every morning you run a script that grabs some data and populates cells in a brand-new spreadsheet. Every morning you want to save that file as C:\Scripts\Daily_report.xls. Because this is a new spreadsheet (remember, you didnÒt open the existing Daily_report.xls), you need to use the SaveAs method. And, when you do, a dialog box will pop up telling you that a file by that name already exists and asking you if you want to replace it.

So how do you get around these dialog boxes? The secret is to set the DisplayAlerts property (part of the Excel Application object) to FALSE. The DisplayAlerts property suppresses the display of dialog boxes and alert messages; instead of allowing you to choose whether to do X, Y, or Z, Excel automatically selects the default action for you. The default action for overwriting existing files is Yes. So when DisplayAlerts is set to False, the revised worksheet will be saved and you wonÒt be nagged and bothered by dialog boxes. HereÒs a sample script that sets DisplayAlerts to FALSE and then uses the SaveAs method to save the changes:

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = FALSE

Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\Test.xls")
Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1, 1).Value = Now
objWorkbook.SaveAs("C:\Scripts\Test.xls")
objExcel.Quit

And just for the heck of it, hereÒs a script that creates a new worksheet and then overwrites an existing worksheet, again by setting DisplayAlerts to FALSE and by using SaveAs:

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.DisplayAlerts = FALSE

Set objWorkbook = objExcel.Workbooks.Add
Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1, 1).Value = Now
objWorkbook.SaveAs("C:\Scripts\Test.xls")
objExcel.Quit

If youÒre having problems saving files, setting DisplayAlerts to FALSE will more than likely take care of things for you.
For More Information

Check out the Hey, Scripting Guy! - Archive

(in reply to Bushmen)
 
 
Post #: 31
 
 Re: Working with Text File - 2/9/2005 12:18:59 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Thanks a lot CaffeineAddiction!

However, I DO like the whatever-delimiter crap (but I don't like arrays), they provide portibility which I need and they are easy to work with. The only thing it lacks was perhaps formatting. But then again, most of the stuff I need are just accessing informatation, and when plain text file can't be used, I usually turns to database.

None the less, Excel is good for many situations, and thanks again for the info, I'll definitely take a look.

(in reply to Bushmen)
 
 
Post #: 32
 
 Re: Working with Text File - 2/9/2005 10:29:33 PM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
hi token and CaffeineAddiction,

Firstly thanks to Token for all your help on this, without you, I would've been in lots of trouble. I changed your original script a little bit to add a second port when I could not get hold of you that one day, but now that you've posted this one, I'll rather use your new once, since your script is much more versatile.

As for the blank line at the end of the ports.txt file, I've been debugging your script from front to back and I can not see why or when that line is created.. But, I've tested it simultaneously on 3 boxes and it all seems fine.. i was just worried that if it adds a line everytime when it adds a computer name, i might end up with a whole lot of blank lines. I will keep on looking untill i find the problem with the blank line though, just for my knowledge..

Again, its the first time i come accross the dictionary object you used and I can see (although i don't understand everything in your script) that it can be very usefull.

Then to CaffeineAddiction, thanks for your info on Excel and Hey, Scripting guy website, i've had a look and they've posted some good reading material (questions and answers).

As for the Excel, that is still a bit more advanced for me at the moment, and i wouldn't know where to start. Also, since the ports file will be on a server, most machines i guess will have excel installed, but when run on a box that has not got excel, we might run into problems, although once again, my knowlegde on the topic is very minor. Thanks once again for your comments, and like token, i'll be reading about the excel stuff.

Regards,

Bushmen

(in reply to Bushmen)
 
 
Post #: 33
 
 Re: Working with Text File - 2/10/2005 1:58:25 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
No problem.

Does it still adding blank lines at the end of the file though ?

(in reply to Bushmen)
 
 
Post #: 34
 
 Re: Working with Text File - 2/13/2005 11:14:44 PM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
hi token,

yeah, it is still adding the blank line on adding and deleting the pc names.

i'm not sure why, but at the moment it is not causing any problems. would like to know though what is causing it, since its still bugging me.

regards,

bushmen

(in reply to Bushmen)
 
 
Post #: 35
 
 Re: Working with Text File - 2/14/2005 1:35:13 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
hmm.... If you could email me the ports.txt file that's used, I could take a look.

(in reply to Bushmen)
 
 
Post #: 36
 
 Re: Working with Text File - 2/14/2005 1:58:37 AM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
have emailed that to you token,

cheers for that.

ben

(in reply to Bushmen)
 
 
Post #: 37
 
 Re: Working with Text File - 2/15/2005 4:29:00 AM   
  Bushmen

 

Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
token,

did you get my email??

(in reply to Bushmen)
 
 
Post #: 38
 
 Re: Working with Text File - 2/15/2005 3:28:34 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Yes, I did, thanks.

I believed that the appearent blank line at the end of the file was the result of the CR/LF (0D/0A) (enter key) at the end of each line that made notepad moves the cursor to the beginning of the next line. The "blankline" is actually empty which contains nothing (not even a CR/LF). This behaviour is expected when using most Windows based editor. *Nix treates this different and does not moves the cursor to the beginning of next line.

(in reply to Bushmen)
 
 
Post #: 39
 
 
Page:  <<   < prev  1 [2]
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Re: Working with Text File Page: <<   < prev  1 [2]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts