Login | |
|
 |
Working with Text File - 2/4/2005 8:58:21 AM
|
|
 |
|
| |
Bushmen
Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
|
Hi, I am quite new to vbscript, so I need all the help I can get. Basically, I have a file on the server say ports.txt which looks like this ========================================================================== ;File listing Ports and Workstation Names ;Syntax: ;PortNo "TAB" ComputerName 9000 TestPC1 9001 TestPC2 9002 TestPC3 9003 9004 TestPC14 9005 TestBox3 9006 9007 9008 9009 9010 ======================================================================= Basically when running the script on a pc, it needs to check which port is the next available portno, and then write the %ComputerName% environment variable string next to the port number in the file. Then I need Another script that I can run when the pc is no longer using that port, which can search the txt file untill it finds its %computername% in the text file and then remove it without removing the portnumber next to it. The script will run from within an application setup. When installing the application, the script will run, add the workstation to the ports file, and when uninstalling the application, removing the workstation from the ports file. I will really appreciate any help I can get on this. Regards
_____________________________
Scripting is Fab!!
|
|
| |
|
|
|
 |
Re: Working with Text File - 2/4/2005 9:13:21 PM
|
|
 |
|
| |
Bushmen
Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
|
hey guys, thanks for a response at least guys, here goes, this is what i've got so far.. i might be totally off the track but please bare with me. 1. Regarding the File. This is not tcpip ports, but specific ports to an application that will range from 9000 to 9999 as a maximum. on installation of the application the script needs to check which is the next port available and write the current computername against that port example 9009 "tab" Mypcname. Then on uninstallation of the application, the script needs to search for its name, and if found, delete only its name from the file and save the file. The file will be in a central location and the user will have access to the file, a network drive, something like x:\AppShare\Ports.txt 2. This is what i've got so far. ====================================================================================== Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Dim FSO,text,Readfile Dim Contents() Set FSO = CreateObject("Scripting.FileSystemObject") Set Readfile = FSO.OpenTextFile("D:\Script\Ports.txt", ForReading, False) Set Writefile = FSO.OpenTextFile("D:\Script\Portsnew.txt",ForWriting,True) 'tempfile i = 0 Do Until Readfile.AtEndOfStream Redim Preserve Contents(i) Contents(i) = Readfile.ReadLine If Contents(i) <> "" Then 'Read Contents(i) 'Remove any spaces or tabs in die line 'Do string functions that if "" exist after the number, i.e. 9000 Then 'use the port by either writing it to a temp file ?? 'Else if something exist, then do nothing and move on to next line. 'MsgBox Contents(i) & " something" Else 'Do nothing, (it shouldn't really be nothing, since endofstream goes to last line 'Or if nothing (which is empty line) then delete that line 'MsgBox Contents(i) & " nothing" End If i = i + 1 Loop Readfile.close Writefile.close ======================================================================================= That is it.. i've managed to step through the current ports.txt file and show each line that its read... but now performing string functions, i am not so clued up, or writing it to another file, not sure if you can edit a file and just save it, or do you have to right a new file and at the end rename it to the original file.? I know this is not much code, the lines thats been remmed out, is what i think it need to do in words, not sure how to put that in code though. Regards, Bushmen
|
|
| |
|
|
|
 |
Re: Working with Text File - 2/5/2005 7:49:52 AM
|
|
 |
|
| |
token
Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
|
The following code will scan throught the file x:\AppShare\ports.txt and add the computer name from which this script is run to the first available port or remove itself from the list of port/computername pairs if one found. NOTE: Each line is required to have one of the following: 1. port number 2. port number plus exactly ONE tab 3. port number plus exactly ONE tab plus ComputerName and *NO* space before and/or after the port number. =============================================================================================== Option Explicit Dim fso, portDir, portFile, input, temp, hash, key, network, port, found, tempFile, tempFileName portDir = "x:\AppShare" portFile = "ports.txt" Set fso = CreateObject("Scripting.FileSystemObject") Set hash = CreateObject("Scripting.Dictionary") Set network = CreateObject("WScript.Network") port = "" found = False If fso.FileExists(fso.BuildPath(portDir,portFile)) Then Set input = fso.OpenTextFile(fso.BuildPath(portDir,portFile),1) Do Until input.AtEndOfStream temp = input.ReadLine If Not hash.Exists(Split(temp,vbTab)(0)) Then If UBound(Split(temp,vbTab)) < 1 Then hash.Item(Split(temp,vbTab)(0)) = "" If port = "" Then port = Split(temp,vbTab)(0) End If Else if Split(temp,vbTab)(1) = "" Then hash.Item(Split(temp,vbTab)(0)) = "" If port = "" Then port = Split(temp,vbTab)(0) End If Else If UCase(network.ComputerName) = UCase(Split(temp,vbTab)(1)) Then hash.Item(Split(temp,vbTab)(0)) = "" found = True Else hash.Item(Split(temp,vbTab)(0)) = Split(temp,vbTab)(1) End If End If End If End If Loop input.Close If found = False and port <> "" Then hash.Item(port) = network.ComputerName End If tempFileName = fso.GetTempName Set tempFile = fso.CreateTextFile(fso.BuildPath(portDir,tempFileName), True) For Each key In hash tempFile.WriteLine key & vbTab & hash.Item(key) Next tempFile.Close fso.DeleteFile fso.BuildPath(portDir,portFile), True fso.MoveFile fso.BuildPath(portDir,tempFileName), fso.BuildPath(portDir,portFile) Else WScript.Echo "File cannot be found: " & portFile End If
|
|
| |
|
|
|
 |
Re: Working with Text File - 2/5/2005 9:18:40 PM
|
|
 |
|
| |
Bushmen
Posts: 122
Score: 0
Joined: 2/4/2005
From:
Status: offline
|
First of all I would like to say thank you to Token and Tnoonan for your input, especially over a weekend, that is some great going. Secondly, I've copied the code into a blank vbs file. Created just for a test, a folder on the X drive called "appshare", so it can not be the path... but I am getting an error "Vbscript runtime Error; Line 16, Char 1, Subscript out of range: '[number:0]' which looks like the following line: "If Not hash.Exists(Split(temp,vbTab)(0)) Then" Unfortunately, I don't have a clue what you are refering to "hash", and I don't even know where to start looking for a problem. I've noted that you use the dictionary object, "is that to look out for a certain character or characters?" Also, just a quick question, if the server team decide for some reason (they probably won't, but i just want to be prepared, decide to change the "tab" in die files to a space, " ", would it be as easy as changing all the "vbtab"s in the script to " "? Regards, Bushmen
|
|
| |
|
|
|
 |
Re: Working with Text File - 2/6/2005 5:57:51 AM
|
|
 |
|
| |
token
Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
|
You don't make my life easily, do you ? :D The following script will ignore any blank lines and comment (denoted by the comment varible, set to ";" for now). It will also "clean up" any spaces before and after the "port <delim> ComputerName" line, and "clean up" any delimiters (if multiple of them exist) in between the port and ComputerName so that only exactly ONE exists between them. Everytime the script runs, it will also APPEND the delimiter AFTER the port number so that if for any reason you wish to add a computer name manually, you can just start typing without worry about typing the delimiter. Any comment lines or blank lines can only exist ONCE in the file. If these lines contains the same "content" (eg: "; blah" and "; blah" are the same), only the line that occurs first will be preserved. So multiple blank lines will be reduced to only ONE, and if a blank lines occur before and after the LIST of port/ComputerName pair, blank lines AFTER the list will be removed to make the content of the file "cleaner" and easily to manage. Also, the delimiter can be EXACTLY ONE character ONLY, whether it's a space, tab, or any other character, and NEVER mixed different characters for the delimiter (if you ever want to manually modify the contents of the file, which I do NOT recommend). change the delimiter (delim variable), and comment (comment variable) as YOU desired. :) ================================================================================ Option Explicit Dim fso, portDir, portFile, input, temp, hash, key, network, port, found, tempFile, tempFileName, delim, comment, re Set fso = CreateObject("Scripting.FileSystemObject") Set hash = CreateObject("Scripting.Dictionary") Set network = CreateObject("WScript.Network") portDir = "F:\1" portFile = "ports.txt" delim = vbTab comment = ";" port = "" found = False 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 port = "" Then port = Split(temp,delim)(0) End If Else if Split(temp,delim)(1) = "" Then hash.Item(Split(temp,delim)(0)) = "" If port = "" Then port = Split(temp,delim)(0) 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) End If End If End If End If End If Loop input.Close If found = False and port <> "" Then hash.Item(port) = network.ComputerName End If tempFileName = fso.GetTempName Set tempFile = fso.CreateTextFile(fso.BuildPath(portDir,tempFileName), True) For Each key In hash tempFile.WriteLine key & delim & hash.Item(key) Next tempFile.Close fso.DeleteFile fso.BuildPath(portDir,portFile), True fso.MoveFile fso.BuildPath(portDir,tempFileName), fso.BuildPath(portDir,portFile) Else WScript.Echo "File cannot be found: " & portFile End If
|
|
| |
|
|
|
|
|