Page 1 of 2 12>
Topic Options
#39767 - 2003-05-02 12:47 AM Writing to a CSV file
Grasshopper75 Offline
Getting the hang of it

Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
Hi All,

I've just stumbled back into Kix after a 6 month leave of absence. I am currently writing a script to roll-out Windows NT SP6a and have written a file to audit a users' PC.

code:
$AuditFile=("s:\Logs\audit.csv")
$IEVersion = ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer","Version")
$Freespace=GetDiskSpace (C:)
If Open( 3 , $AuditFile , 5 ) = 0
WriteLine( 3 , "@DATE,@TIME,@WKSTA,@USERID,@FULLNAME,$FREESPACE Kb,@PRODUCTTYPE @CSD,Internet Explorer $IEVERSION @CRLF")
Else
Beep
? "failed to open file, error code : [" + @ERROR + "]"
EndIf

Now to avoid the issue of a PC not running the audit if the file is being written to by another user I want to include a WHILE-LOOP but don't know where to put it. Can anyone help ?

Cheers,
Grasshopper75

Top
#39768 - 2003-05-02 12:52 AM Re: Writing to a CSV file
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Two suggestions:
1) Look into WriteLog2()
2) Use the computer name (@wksta) as the audit file name.

This will avoid any chance of a write conflict problem. You then can use another program to consolidate the the data.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#39769 - 2003-05-01 02:55 PM Re: Writing to a CSV file
Rocco Capra Offline
Hey THIS is FUN
*****

Registered: 2002-04-01
Posts: 380
Loc: Mansfield Ohio
If you write it to a text file, the file can be written to while it is open. (I know this works cause I had the same proble you have.)

Then you can go into Excel and Import the data to the spreadsheet. You also have the option to refresh the data every time you open the Excel file.

Open a blank Excel file, click Data -> Get External Data -> Import Text File...

HTH,
Rocco
_________________________
I Love this board!! -------------------- My DEV PC is running KIX 4.22 WINXP Pro

Top
#39770 - 2003-05-01 03:10 PM Re: Writing to a CSV file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
However, if the file has ben opened by one user for write purposes, then it is write-locked for other users. Thus, you'd write a WHILE loop to wait until the other user releases the file. One would also insert a timeout value, otherwise you might end up with a loop that never exits.
_________________________
There are two types of vessels, submarines and targets.

Top
#39771 - 2003-05-01 03:55 PM Re: Writing to a CSV file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
So, you can run this concurrently from multiple computers and they all write to the same file at the same time?
_________________________
There are two types of vessels, submarines and targets.

Top
#39772 - 2003-05-01 04:20 PM Re: Writing to a CSV file
Rocco Capra Offline
Hey THIS is FUN
*****

Registered: 2002-04-01
Posts: 380
Loc: Mansfield Ohio
Boy this is a bad week for me. [Roll Eyes] You are very correct in that I cannot write to the file while it is open and being written to. [Embarrassed]

My confusion was that I can open a file that Kix was writing to and close it without loosing any data.

Sorry Grasshopper for misleading you! [Frown] [Embarrassed]
_________________________
I Love this board!! -------------------- My DEV PC is running KIX 4.22 WINXP Pro

Top
#39773 - 2003-05-01 05:34 PM Re: Writing to a CSV file
Grasshopper75 Offline
Getting the hang of it

Registered: 2002-01-15
Posts: 99
Loc: Melbourne, Australia
Each user will execute the code wen they log on so there is a possibility that more than one user is attempting to write to the file at the same time. I am happy to implement Howard's suggestion of giving the log file the workstation name and creating a log for each workstation though does anyone know of any easy way to consolidate this information ?
Failing that yes I would like a WHILE loop with a timeout but again where do I put that code ?? I'm a bit confused on where to put it based on the error code for the file being open.

Top
#39774 - 2003-05-01 05:41 PM Re: Writing to a CSV file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
The simplest way to consolidate is with the DOS COPY command.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#39775 - 2003-05-01 05:41 PM Re: Writing to a CSV file
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
code:
do
$res = Open( 3 , $AuditFile , 5 )
until $res = 0

_________________________
Eric

Top
#39776 - 2003-05-01 05:45 PM Re: Writing to a CSV file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
code:
$res = Open( freefilehandle() , $AuditFile , 5 )
while $res
sleep 1
$res = Open( freefilehandle() , $AuditFile , 5 )
loop

reduces CPU impact.
_________________________
There are two types of vessels, submarines and targets.

Top
#39777 - 2003-05-01 05:56 PM Re: Writing to a CSV file
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Yeah, I thought about putting a 'sleep' in my loop, but had already added the reply.

Anyway, with using FreeFileHandle() in your open function, how are you supposed to know what the file handle is once the file is opened?

I'd say set a var equal to FreeFileHandle and use the var in the call to open()
_________________________
Eric

Top
#39778 - 2003-05-01 06:00 PM Re: Writing to a CSV file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
maciep, you are correct in that one [Wink]
_________________________
!

download KiXnet

Top
#39779 - 2003-05-01 06:05 PM Re: Writing to a CSV file
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Yes, you are [Cool]
_________________________
There are two types of vessels, submarines and targets.

Top
#39780 - 2003-05-01 06:08 PM Re: Writing to a CSV file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Well, neither of you have an escape in your loop of death. If somone manages to hold a lock on the file, everyone will been stuck in the loop.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#39781 - 2003-05-01 06:11 PM Re: Writing to a CSV file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yep...
well, escape might not be needed...
but some sorta reporting thing... to the console or email or...

thus the thing will be handled.
_________________________
!

download KiXnet

Top
#39782 - 2003-05-01 06:38 PM Re: Writing to a CSV file
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
The easiest way to consolidate the workstation name based files would be to use WSHPIPE, one of the DIR type udfs, or the KiXtart DIR command to gather a list of files in the directory. Then open and read the files using WriteLog2 to write the consolidate data file.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#39783 - 2003-05-01 07:40 PM Re: Writing to a CSV file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
grasshoppa,
If you want an example of one way to deal with contention, take a look at this UDF.
Topic: LOGGER() - Write to log when available
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#39784 - 2003-05-01 07:59 PM Re: Writing to a CSV file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yep...
it looks ok.
damn, how have I missed that...

doesn't even look like udf, but as a part of your script it should be pretty ok.
_________________________
!

download KiXnet

Top
#39785 - 2003-05-01 11:33 PM Re: Writing to a CSV file
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Boy, I'm glad I don't work where you guys are. Way too many LOOPS/WAITS/SLEEPS/etc...

Do your people ever get to logon? [Eek!]

Howard already told you how to do it, and you guys seemed to have ignored his suggestion.

Basically you write a log file for each computer, then you copy that data into a single file and import with Excel.

Here is a basic idea (code has not been tested)



$AuditFile=("s:\Logs\"+@WKSTA+".LOG")
IF EXIST ($AuditFile)
    DEL ($AuditFile)
ENDIF
$IEVersion = ReadValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer","Version")
$Freespace = GetDiskSpace("C:\") /1024
IF OPEN(1,$AuditFile5)= 0
  $out = WriteLine(1"@DATE+"*"+@TIME+"*"+@WKSTA+"*"+@USERID+"*"+@FULLNAME+"*"+$FREESPACE+"*"+@PRODUCTTYPE+"*"+@CSD+"*"+$IEVERSION+"*"+@CRLF")
  $x = CLOSE(1)
ENDIF



Then from your workstation in a batch file you run

@ECHO OFF
COPY s:\Logs\*.LOG C:\REPORTS\INVENTORY.TXT

Then you run Excel and you open C:\REPORTS\INVENTORY.TXT and
you remove all delimiters and add the * as the delimiter
I don't use the , as I find that too often it is used in names or other
areas that will screw up your import to Excel. Have not found very many
applications or names that use the * so that is why I use it.
Now you should have a very nice list of Systems and their data.

And no file locking/waiting to deal with.

Top
#39786 - 2003-05-02 12:34 AM Re: Writing to a CSV file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
EH DOC! No need to get your knickers in a twist. [Wink] Howard's suggestion did not go unnoticed and I see you like my DOS COPY suggestion too. All the other posts were simply direct replies to the initial request.

Cut us some slack eh! It's a slow day on the board and we are like piranha. [Big Grin]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 601 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.077 seconds in which 0.03 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