Page 1 of 1 1
Topic Options
#163298 - 2006-06-15 03:05 PM If file exists write computer name to text file
philp Offline
Lurker

Registered: 2006-06-15
Posts: 4
Hi,

I wonder if you could help me with what hopefully will be a simple bit of code. I already have a kixtart script running in net logon and want to query the each PC for a particular file. If it exists I would like to append the computer name to a text file to build a log.

Hopefully all PC's could write to the same log file, prefabaly each computer name on a seperate line. I really only want them to write it once rather than every time the computer logs on.

Any ideas how??

Top
#163299 - 2006-06-15 03:39 PM Re: If file exists write computer name to text file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Hi and welcome to the board.

It depends on how you want the script to run.
If you want it to run as a logon script then you could have issues with one computer having the file open for writing all other computers will fail to open the file because it is already open.
If you run it as a remote admin script you could just check each computer one by one. It is recommended to add a method to check if the computer is online before checking if the file exists. You could use the ping udf for this purpose. Just do a search for ping in the udf forum and it will show up.

As a logon script (beware of the open issues):
Code:

Break on

$rc = Open (1, "\\server\share\logfile.txt" ,5)

If Exist ("c:\somefile.ext")
$line = ReadLine (1)
If InStr ($line, @WKSTA) = 0
$rc = WriteLine (1, @WKSTA + "~")
Else
?"Computer is already in the log file."
EndIf
EndIf

$rc = Close (1)



As a remote admin script:
Code:

Break on

$rc = Open (1, "\\server\share\logfile.txt" ,5)

$computers = Split("comp1~comp2~comp3~comp4~comp5", "~")

For Each =$computer in $computers
If Exist ("\\" + $computer + "\c$\somefile.ext")
$line = ReadLine (1)
If InStr ($line, $computer) = 0
$rc = WriteLine (1, $computer + "~")
Else
?"Computer is already in the log file."
EndIf
EndIf
Next

$rc = Close (1)

_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#163300 - 2006-06-15 04:00 PM Re: If file exists write computer name to text file
philp Offline
Lurker

Registered: 2006-06-15
Posts: 4
Thanks for the info.

Pinging or querying the individual PC's is not viable as there are 1200+ machines and there is no common time when there are all on. Hence the logon intention.

How about writing a seperate file for each computer (perhaps called the computer name). If the queried file exists the text file is created. Again it would hopefully only attempt to write the text file once - perhaps by checking if a text file already exists for the PC.

Would this work? How would it be coded?

Thanks for the help

Top
#163301 - 2006-06-15 04:27 PM Re: If file exists write computer name to text file
philp Offline
Lurker

Registered: 2006-06-15
Posts: 4
As another thought - how complicated would it be to write the computer name to a SQL server database?
Top
#163302 - 2006-06-15 04:35 PM Re: If file exists write computer name to text file
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
a bit more complicated at least for me but prolly duable.
for a file per pc:
Code:

Break on
If Exist ("\\server\share\"+@wksta+".txt")
"computer-log exists"?
return
else
$rc = Open (1, "\\server\share\" + @wksta + ".txt" ,5)
If Exist ("c:\somefile.ext")
$rc = WriteLine (1, "somefile.ext exists on "+ @WKSTA + " " + @DATE + " " @TIME + "~")
$rc = Close (1)
EndIf
EndIf


Me making typo's? naaaaw *cough* Mart pointed out and me fixed.


Edited by Björn (2006-06-15 04:49 PM)
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#163303 - 2006-06-15 04:44 PM Re: If file exists write computer name to text file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You got some not existing macro's there Björn. @wrksta And it is in a string

Logging to an SQL DB is not that difficult. If you do a search on SQL (I only searched the UDF forum) you will get lots of hits.

If you want it to create a file for each system that has the file on it this would work.

Code:

Break on


$log = Exist ("\\server\share\" + @WKSTA + ".txt")
$file = Exist ("c:\somefile.ext")

If $file = 1 AND $log = 0
$rc = Open (1, "\\server\share\" + @WKSTA + ".txt" ,5)
$rc = Close (1)
EndIf

_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#163304 - 2006-06-15 04:49 PM Re: If file exists write computer name to text file
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 953
Loc: Stockholm, Sweden.
Lol. Yes Mart, I usually create my own macro's each time
And my code can always be golfed down, like you just proved
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#163305 - 2006-06-15 05:05 PM Re: If file exists write computer name to text file
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Hmmm...
Quote:

Logging to an SQL DB is not that difficult




I'd beg to differ a bit. During logon and all the other tasks happening at the same time and probably well over 100 possible connection error codes for SQL I'm not so sure I'd list it as EASY.

Doable perhaps but with all the normal network connection problems related to simple logon seen around here, I think putting SQL in the mix is a bit more complicated.

Top
#163306 - 2006-06-15 05:33 PM Re: If file exists write computer name to text file
philp Offline
Lurker

Registered: 2006-06-15
Posts: 4
Thanks guys - I've kept it simple and gone for the individual file options - works like a charm.
Top
#163307 - 2006-06-15 09:15 PM Re: If file exists write computer name to text file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Ok, ok. You win Doc. Was it even a contest LOL

I've never seen SQL as a big and complicated thing. Been using it for almost 8 years now and all I ever wanted from it (no not just simple noob stuff) got done nice and fairly easy. But I have to say that I never put it in a logon script. Now I'm planning to do so to build an asset DB with pc name, serials and all that stuff. Anyway I love SQL. SQL is my friend when I need DB stuff.

So I choose SQL over all these separate files for logging. Also SQL is easier to query then checking all these freaking files.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#163308 - 2006-06-15 09:39 PM Re: If file exists write computer name to text file
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
No, not knocking SQL just the word EASY

Does take a bit more work to setup and manage then a text file, but beyond that SQL is much more versatile for massaging the data.

Top
#163309 - 2006-06-16 09:25 AM Re: If file exists write computer name to text file
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Probably a bit late for you now, but the INI file format often wins in this type of scenario as it's a pseudo-database and is very easy to implement.

If you've got 1200 potential files then I'd use some sort of hash or part of the workstation name to create subdirectories, otherwise things will start to slow down as the number of files in the direcroty builds up.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.069 seconds in which 0.029 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org