Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


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 >> Working with Text File
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 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!!
 
 
Post #: 1
 
 Re: Working with Text File - 2/4/2005 1:47:17 PM   
  tnoonan

 

Posts: 364
Score: 0
Joined: 12/14/2004
From:
Status: offline
I would be glad to help. Do you have any code to put on forum to show what you have now. I really don't think anyone "Including myself" is going to write the whole thing for you. Give it a shoot and post what you got and you will get plenty of input.

(in reply to Bushmen)
 
 
Post #: 2
 
 Re: Working with Text File - 2/4/2005 2:19:11 PM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
quote:
Originally posted by Bushmen
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.




First of all, PCs runing TCP/IP, there are 65536 ports available to use. Chances are most PCs will find their first available port very closer to each other.

Secondly, where is the file located ? Unless there is a central location for the file, each machine could have differnet contents for the file.

What exactly are you trying to do here ?

(in reply to Bushmen)
 
 
Post #: 3
 
 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

(in reply to Bushmen)
 
 
Post #: 4
 
 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

(in reply to Bushmen)
 
 
Post #: 5
 
 Re: Working with Text File - 2/5/2005 8:35:51 AM   
  tnoonan

 

Posts: 364
Score: 0
Joined: 12/14/2004
From:
Status: offline
Copied code down and tested. Nice job by the way. Could not get it to add computer name to first avil port always adding to first column ie.

9000 TestPC1 W-MOBILE
9001 TestPC2
9002 TestPC3
9003
9004 TestPC14
9005 TestBox3
9006
9007
9008
9009
9010

Would it not be better to use excel and have a range a1:a10 b1:b10 then search range in b to see if empty then add name and search b for name match ifso remove. Just a thought.

(in reply to Bushmen)
 
 
Post #: 6
 
 Re: Working with Text File - 2/5/2005 9:42:24 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
How is your port.txt constructed ? A tab is required to separate the port and computer name. If there is no tab found, then whatever the current line has will be treated as the port number.

Each line is required to have one of the following:
1. port number
2. port number plus ONE tab
3. port number plus ONE tab plus computerName

and *NO* space before or after the port number.

Having additional code to detect the number of tabs in-between or surrounds the line isn't necessary since the script itself will insert those values correctly. Once a list of port number are entered in the file, the rest should be all automatic.

(in reply to Bushmen)
 
 
Post #: 7
 
 Re: Working with Text File - 2/5/2005 9:47:36 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Maybe, but I am not familiar with Excel :P Care to share some references on scripting Excel ?

Also, for simpler tasks such as this one, I always prefer to use plain text file simply becuase its compatibility and it's easier to manipulate it to the desired format with the appropriate delimiter etc.

(in reply to Bushmen)
 
 
Post #: 8
 
 Re: Working with Text File - 2/5/2005 12:52:47 PM   
  tnoonan

 

Posts: 364
Score: 0
Joined: 12/14/2004
From:
Status: offline
I find alot of scripting references for excel @ http://www.sloppycode.net but you have to drill deep.

(in reply to Bushmen)
 
 
Post #: 9
 
 Re: Working with Text File - 2/5/2005 1:00:13 PM   
  tnoonan

 

Posts: 364
Score: 0
Joined: 12/14/2004
From:
Status: offline
Oh and once again. I did not read your code before posting. I just copied what bushman had for ports.txt. Can you say Dumb@ss That would be me. Script works fine. If I find any more sites showing examples of excel and vbs I'll be sure to post.

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

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
heh.. nah, I wouldn't say so :D

And thanks for the URL, I'll check it out. YES!, please do post URLs if you find more in the future. I'm also looking for references regarding IE and VBS. Please post some if you know them as well.

Greatly appreciated. :>

(in reply to Bushmen)
 
 
Post #: 11
 
 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

(in reply to Bushmen)
 
 
Post #: 12
 
 Re: Working with Text File - 2/5/2005 11:05:14 PM   
  Bushmen

 

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

i've narrowed my problem down a bit. it seems when its reading the 4th line which is empty, then it get the range out of script error. also, when i remove the empty line, it goes through, but i end up with my computername on the 2nd line of ports.txt just below ";File listing Ports and Workstation Names"

=========================================
;File listing Ports and Workstation Names
ComputerName
;....
;....

9000...
=========================================

Is there a way we can cater for the empty line, and am i doing something wrong, or why is it adding the computername to the 2nd line.

Looking forward in hearing back from you guys, i do enjoy this vbscript stuff.. just need a LOT of practise :)

Bushmen

(in reply to Bushmen)
 
 
Post #: 13
 
 Re: Working with Text File - 2/6/2005 5:02:16 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Make sure your file contains nothinig but port number, take out any comments and blank line and try again :)

Each line is required to have one of the following:
1. port number
2. port number plus ONE tab
3. port number plus ONE tab plus computerName

and *NO* space before or after the port number.

(in reply to Bushmen)
 
 
Post #: 14
 
 Re: Working with Text File - 2/6/2005 5:14:30 AM   
  Bushmen

 

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

i've tried what you suggested and that all works great.

if the server team require those 4 first lines which is remmed out with the last line being empty, can we cater for it??

also, if i want to change the vbtab to a " ", can i simply replace all vbtabs in the script with a space? i.e " ". They are not sure if the app requires tabs or spaces and i should have an answer tomorrow morning.

Thanks for your help on this.

Regards,

Ben

(in reply to Bushmen)
 
 
Post #: 15
 
 Re: Working with Text File - 2/6/2005 5:41:21 AM   
  tnoonan

 

Posts: 364
Score: 0
Joined: 12/14/2004
From:
Status: offline
dim vbSpace
vbSpace = " "

replace all vbTab with vbSpace

(in reply to Bushmen)
 
 
Post #: 16
 
 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

(in reply to Bushmen)
 
 
Post #: 17
 
 Re: Working with Text File - 2/6/2005 6:05:22 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
So much for relaxing during the weekends :D

(in reply to Bushmen)
 
 
Post #: 18
 
 Re: Working with Text File - 2/6/2005 6:07:52 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Oh btw, I use the following for ports.txt and the space you see between port number and ComputerName is a tab, not a space.

=====================================================
; File listing Ports and Workstation Names
; Syntax:
; PortNo "TAB" ComputerName
;

9000 TestPC1
9001 TestPC2
9002 TestPC3
9003
9004
9005 TestPC14
9006 TestBox3
9007
9008
9009
9010
=====================================================

(in reply to Bushmen)
 
 
Post #: 19
 
 Re: Working with Text File - 2/6/2005 8:42:57 AM   
  Bushmen

 

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

I would just like to thank you guys for all your input especially for Token, for all your hard work over a weekend!!!

This now works with no problems. I don't know what i would've done without you guys, and one day, maybe i can help you too.

I just need to test the " " instead of the "vbtab" to see if i can change that, and that's it.

Thanks very much.

regards,
Bushmen

(in reply to Bushmen)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

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 >> Working with Text File Page: [1] 2   next >   >>
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