Login | |
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
|
|