Page 1 of 2 12>
Topic Options
#172407 - 2007-01-04 10:16 PM HELP! use GetFileTime function to add line to log file
beejay Offline
Fresh Scripter

Registered: 2007-01-04
Posts: 12

The purpose of the script I am trying to write is to:

- Check 2 local (c drive) files on user log on
Note: all PCs will have file1.txt present; some (not all) will also
have file2.txt present

Report details of file1 and file2 (time & date) to a text log files auditfile1.txt and auditfile2.txt respectively. (stored on network drive - users have access)

Note: this will be performed on user log-on, so none of the text files mentioned will be open.

I have tried to follow the high level process of:

1 - Create a timestamp (may not be necessary, but I may want to use this later)
2 - Get file time / dates for file1.txt and file2.txt
3 - Open auditfile1
4 - write line in auditfile1
5 - Close auditfile 1
6 - Repeat steps 3-5 for auditfile2
7 - end script

auditfile1.txt and auditfile2.txt are both created and in place;
but I cannot get an entry to appear in either. For reference, the version of kixtart currently used is 3.60.0.0. Here's my try so far: I'd be grateful for any help offered
-------------------------------------------

; * Create the TIMESTAMP
; a TIMESTAMP is not strictly necessary!

IF @MONTHNO < 10
$MONTH= "0" + "@MONTHNO"
ELSE
$MONTH= "@MONTHNO"
ENDIF

IF @MDAYNO < 10
$DAY= "0" + "@MDAYNO"
ELSE
$DAY= "@MDAYNO"
ENDIF

$timestamp = "@YEAR" + "-" + "$MONTH" + "-" + "$DAY" + " " + @TIME
$ComputerName = @WKSTA

; ------------------------------------------------------------------------
; * Create an antry in auditfile1 or auditfile2 relating to date held for
;C:\program files\file1.txt and c:\ program files\file2.txt respectively
; ------------------------------------------------------------------------

$logfile1 = "I:\foldername\auditfile1.txt."
$logfile2 = "I:\foldername\auditfile2.txt"

$Result1 = GetFileTime("$C:\program files\file1.txt")
$Result2 = GetFileTime("$C:\program files\file2.txt")

$ = Open ($logfile1)

if len($Result1)>0
$Result1string = "$TIMESTAMP;$ComputerName;$Result1"
$x = WriteLine( 3 , $Result1string + Chr(13) + Chr(10) )
endif

$ = Close ($logfile1)

$ = Open ($logfile2)

if len($Result2)>0
$Result2string = "$TIMESTAMP;$ComputerName;$Result2"
$x = WriteLine( 3 , $Result2string + Chr(13) + Chr(10) )
endif

$ = Close ($logfile2)

exit
_________________
Thanks
Beejay

Top
#172410 - 2007-01-04 10:29 PM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4459
Loc: USA
Quote:
For reference, the version of kixtart currently used is 3.60.0.0.


Can I suggest moving up to 4.53... many, many updates have been added since the version you have.... not that we can't help you, but most of the regulars here have long since dropped version 3.x.

Top
#172411 - 2007-01-04 10:31 PM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
First off, don't wrap your macros in quotes.
Second, you need to open for WRITE, not READ.
3.60 is old but AFAIK the default for open if mode is unspecified is READ.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#172412 - 2007-01-04 10:32 PM Re: HELP! use GetFileTime function to add line to log file [Re: Les]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Oh, and you are missing the file handles too.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#172413 - 2007-01-04 10:45 PM Re: HELP! use GetFileTime function to add line to log file [Re: Les]
beejay Offline
Fresh Scripter

Registered: 2007-01-04
Posts: 12
Les

Thanks very much for a quick reply. As this is my first bash at a kixtart script, I hope you'll bear with me on this one....

I thought that I had to open a file to use the WriteLine function to add a line in the file.

Would you mind giving me an example line of what I need?

from a kixtart novice - thanks in anticipation

by the way - I take your point re the old version - I've now d/l the latest version.

Top
#172415 - 2007-01-04 10:59 PM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
It's all in the parms.
1st parm is handle #
2nd parm is path\filename
3rd parm is mode

It is well explained in the manual.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#172417 - 2007-01-04 11:20 PM Re: HELP! use GetFileTime function to add line to log file [Re: Les]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
The board is also full of examples opening files in write mode
http://www.kixtart.org/forums/ubbthreads...true#Post168511

Top
#172422 - 2007-01-05 09:08 AM Re: HELP! use GetFileTime function to add line to log file [Re: Witto]
beejay Offline
Fresh Scripter

Registered: 2007-01-04
Posts: 12
Witto

Thanks - I've had a look at the page you provided the link to but it seems to refer to printer problems; my problem is I am missing some "vital" piece in my code - see above - I've had a try -but due to my ignorance don't know where I'm going wrong; I've had a look in the online reference but there is only a minimal entry for the "getfiletime" function

Can anybody help?

I'd really appreciate any help.
Thanks

Beejay

Top
#172423 - 2007-01-05 09:21 AM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
Björn Offline
Korg Regular
*****

Registered: 2005-12-07
Posts: 950
Loc: Stockholm, Sweden.
sidenote - please use the code-tags when posting code-snippets.
second, you really need to update you kixtart-version to latest.
Code:
; * Create the TIMESTAMP
; a TIMESTAMP is not strictly necessary!

;why are you using this below?
IF @MONTHNO < 10
$MONTH= "0" + @MONTHNO ;notice - no " " on macros.
ELSE
$MONTH= @MONTHNO
ENDIF

IF @MDAYNO < 10
$DAY= "0" + @MDAYNO
ELSE
$DAY= @MDAYNO
ENDIF
;Why are you using this above?

$timestamp = "@YEAR" + "-" + "$MONTH" + "-" + "$DAY" + " " + @TIME
$ComputerName = @WKSTA

; ------------------------------------------------------------------------
; * Create an antry in auditfile1 or auditfile2 relating to date held for
;C:\program files\file1.txt and c:\ program files\file2.txt respectively
; ------------------------------------------------------------------------

$logfile1 = "I:\foldername\auditfile1.txt."
$logfile2 = "I:\foldername\auditfile2.txt"

$Result1 = GetFileTime("C:\program files\file1.txt") ; " " is a string,
$Result2 = GetFileTime("C:\program files\file2.txt") ; not a variable.

$ = Open (3,$logfile1) ;need to specify more options for the open.

if len($Result1)>0
$Result1string = "$TIMESTAMP;$ComputerName;$Result1"
$x = WriteLine( 3 , $Result1string + Chr(13) + Chr(10) ) ; you had specified it here tho..
endif

$ = Close ($logfile1) ;but not here

$ = Open ($logfile2) ; not here

if len($Result2)>0
$Result2string = "$TIMESTAMP;$ComputerName;$Result2"
$x = WriteLine( 3 , $Result2string + Chr(13) + Chr(10) )
endif

$ = Close ($logfile2) ;nor here.

exit


Edited by Björn (2007-01-05 09:21 AM)
_________________________
as long as it works - why fix it?
If it doesn't work - kix-it!

Top
#172431 - 2007-01-05 12:32 PM Re: HELP! use GetFileTime function to add line to log file [Re: Björn]
beejay Offline
Fresh Scripter

Registered: 2007-01-04
Posts: 12
Bjorn

Thanks for the advice earlier - very useful.

I've had a go with the script, but its not working; I've checked that the file is being called from the main

kixtart file (it also calls other kixtart files held in the same folder). For reference, I've entered the

script as used below - just simplified the filename / folder references - but have checked these. Am I missing

something?

Thanks again

Beejay




; * Create the TIMESTAMP
; a TIMESTAMP is not strictly necessary!

IF @MONTHNO < 10
$MONTH= "0" + "@MONTHNO"
ELSE
$MONTH= "@MONTHNO"
ENDIF

IF @MDAYNO < 10
$DAY= "0" + "@MDAYNO"
ELSE
$DAY= "@MDAYNO"
ENDIF

$timestamp = "@YEAR" + "-" + "$MONTH" + "-" + "$DAY" + " " + @TIME
$ComputerName = @WKSTA

; ----------------------------------------------------------------
;Create an antry in I:\foldername\auditfile1.txt &
;I:\foldername\auditfile2.txt relating to
;date held for filename1 & filename2 respectively
; -----------------------------------------------------------------$logfile1 = "I:\foldername\auditfile1.txt"
$logfile2 = "I:\foldername\auditfile2.txt"

$Result1 = GetFileTime("$C:\Program Files\filname1.txt")
$Result2 = GetFileTime("$C:\Program Files\filname2.txt")

$ = Open (3, $logfile1)

if len($Result1)>0
$Result1string = "$TIMESTAMP;$ComputerName;$Result1"
$x = WriteLine( 3 , $Result1string + Chr(13) + Chr(10) )
endif

$ = Close ($logfile1) ; also tried this with 3, $logfile1
$ = Open ($logfile2) ; also tried this with 3, $logfile2

if len($Result2)>0
$Result2string = "$TIMESTAMP;$ComputerName;$Result2"
$x = WriteLine( 3 , $Result2string + Chr(13) + Chr(10) )
endif
$ = Close ($logfile2) ; also tried this with 3, $logfile2
exit

Top
#172436 - 2007-01-05 03:22 PM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
You still didn't open the file for write. YOu need to supply the 3rd parm.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#172437 - 2007-01-05 03:26 PM Re: HELP! use GetFileTime function to add line to log file [Re: Les]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Also, you arre using a zero-length var "$" with an old version of KiX and that will bite you when you embed vars in strings. I suggest you replace them with $RC or $NUL. Better still, don't embed vars in strings.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#172441 - 2007-01-05 06:40 PM Re: HELP! use GetFileTime function to add line to log file [Re: Les]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4459
Loc: USA
The manual will really help you if you use it. ;\) (See below)

Your open()'s and close()'s are not setup right.

Examples:

$RC=open(1,"Filename.txt", 5)
Opens Filename.txt using handle 1, the 5 is equal to 1 + 4 which if you look below 1 means create the file if it does not exist, and 4 open the file for writing. If the file already exists then the value could just be 4.

$RC=close(1) ; to close the file opened above. The 1 is the same handle used above.


Quote:

Open( )

Action: Opens a text file.

Syntax: Open (FileHandle, "filename", mode)

Parameters: FileHandle

A numeric expression indicating the handle number of the file to open. Possible values range from 1 to 10.

Filename

A string expression indicating the path and name of the ASCII file to open.

Mode

Optional parameter that indicates what should happen if the file does not exist. This parameter can have the following values:

0
If the file does not exist, Open( ) fails with return code 2 (default).

1
If the file does not exist, Open( ) will create a new file.

2
Opens the file for read access (default).

4
Opens the file for write access.


Note: These values are cumulative. So if you want to open a file for write access, and create it if it does not yet exist, you should specify 5. Notice, however, that a file can not be opened for read and write access at the same time.


Remarks: Open opens the ASCII file specified by file name, for the internal buffer indicated by file number. KiXtart supports a maximum of ten open files, so file number must be within the range of 1 to 10.

The file-I/O functions in KiXtart (Open, ReadLine, and Close) process small configuration files. They are not intended for larger operations, such as scanning long files. For the sake of simplicity and speed, Open reads an entire ASCII file into memory, and subsequent ReadLine commands read lines stored in memory.

Although this design is memory-intensive, it is also very fast and simple.

Returns: -3 File number already in use
-2 Invalid file number specified
-1 Invalid file name specified
0 File opened successfully
>0 System error


See Also: FreeFileHandle( ), Close( ), ReadLine( ), WriteLine( )

Example: IF Open(3, @LDRIVE + "\CONFIG\SETTINGS.INI") = 0
$x = ReadLine(3)
WHILE @ERROR = 0
? "Line read: [" + $x + "]"
$x = ReadLine(3)
LOOP
ENDIF





Quote:

Close( )

Action: Closes a file previously opened by the Open function.

Syntax: Close (FileHandle)

Parameters: FileHandle
A numeric expression indicating the handle number of the file to close. Possible values range from 1 to 10.

Returns: 0 File Closed
-2 Invalid File Number Specified


See Also: FreeFileHandle( ), Open( ), ReadLine( ), WriteLine( )

Example: If Close(3)
Beep
? "Error closing file!"
EndIf

Top
#172445 - 2007-01-05 07:23 PM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11571
Loc: CA
Hi beejay,

Please review this post to learn how to properly post your CODE so that others can easily read it with a proper layout.

The Post/Reply Formatting Box and How to use it
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=171901

Almost every bulletin board on the Internet uses these types of tags for formatting so it would be worth your time and effort to learn how they're used.

Thanks.

.

Top
#172540 - 2007-01-08 05:11 PM Re: HELP! use GetFileTime function to add line to log file [Re: NTDOC]
beejay Offline
Fresh Scripter

Registered: 2007-01-04
Posts: 12
Can anybody help me??

I've gone through all the comments received and the Online Reference, as well as the "Scripting with Kixtart" book by Bob Kelly, but am banging my head on a brick wall (literally).

I've put my latest attempt to:

- establish the date from filename1 & filename2
- report these dates to logfile1 & logfile2 respectively

sounds simple, but I've tried tweaking the code in places, but have trouble in getting it to work - made worse (for me) as it's difficult to know which part of the code has the error.

I've now upgraded to the latest version of Kixtart.

Thanks to any expert prepared to help
Code:
; Create the TIMESTAMP
; a TIMESTAMP is not strictly necessary!

IF @MONTHNO < 10 
$MONTH= "0" + "@MONTHNO"
ELSE
$MONTH= "@MONTHNO"
ENDIF

IF @MDAYNO < 10 
$DAY= "0" + "@MDAYNO"
ELSE
$DAY= "@MDAYNO"
ENDIF

$timestamp = "@YEAR" + "-" + "$MONTH" + "-" + "$DAY" + " " + @TIME
$ComputerName   = @WKSTA

; ----------------------------------------------------
;Create an entry in I:\foldername\auditfile1.txt & 
;I:\foldername\auditfile2.txt relating to 
;date held for filename1 & filename2 respectively
; ----------------------------------------------------

$logfile1 = "I:\foldername\auditfile1.txt"
$logfile2 = "I:\foldername\auditfile2.txt"

$Result1 = GetFileTime("C:\Program Files\filname1.txt", 0)
$Result2   = GetFileTime("C:\Program Files\filname2.txt", 0)

$ = Open (3, $logfile1, 4)

IF Open(3, $logfile1, 4 ) = 0
  $x = WriteLine( 3 , $Result1string + Chr(13) + Chr(10) )
ELSE
  BEEP
  ? "failed to open file, error code : [" + @ERROR + "]"
ENDIF

If Close(3)
  Beep
  ? "Error closing file!"
EndIf

$ = Open (3, $logfile2, 4)

IF Open(3, $logfile2, 4 ) = 0
  $x = WriteLine( 3 , $Result2string + Chr(13) + Chr(10) )
ELSE
  BEEP
  ? "failed to open file, error code : [" + @ERROR + "]"
ENDIF

If Close(3)
  Beep
  ? "Error closing file!"
EndIf

exit

Top
#172541 - 2007-01-08 05:26 PM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4459
Loc: USA
You are trying to open the same file twice with the following code...

$ = Open (3, $logfile1, 4)
IF Open(3, $logfile1, 4 ) = 0

just remove the first one and use the "if Open..."

You will need to do this for your other file as well.

Top
#172548 - 2007-01-08 08:25 PM Re: HELP! use GetFileTime function to add line to log file [Re: Allen]
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
$Result1 = GetFileTime("C:\Program Files\filname1.txt", 0)
...
WriteLine( 3 , $Result1string + Chr(13) + Chr(10) )

You define $Result1 but then you try to WritLine() something that doesn't exist.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#172566 - 2007-01-09 10:19 AM Re: HELP! use GetFileTime function to add line to log file [Re: Les]
beejay Offline
Fresh Scripter

Registered: 2007-01-04
Posts: 12
Allen / Les

Thanks for spending time looking at the script - I've made the changes you both mention, but it's still not working - is there any way I can validate each piece of code?

Thanks

Top
#172567 - 2007-01-09 12:43 PM Re: HELP! use GetFileTime function to add line to log file [Re: beejay]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22335
Loc: OK
can you post your current code...
_________________________
!

download KiXnet

Top
#172583 - 2007-01-09 05:28 PM Re: HELP! use GetFileTime function to add line to log file [Re: Lonkero]
beejay Offline
Fresh Scripter

Registered: 2007-01-04
Posts: 12
Thanks - I've copied the code below exactly as it appears;

For testing purposes, I had created the 4 text files and holding
folders as named in the scripts, but cant get an entry to appear
in the audit files.
Code:
; * Create the TIMESTAMP 
; a TIMESTAMP is not strictly necessary!

IF @MONTHNO < 10 
$MONTH= "0" + "@MONTHNO"
ELSE
$MONTH= "@MONTHNO"
ENDIF

IF @MDAYNO < 10 
$DAY= "0" + "@MDAYNO"
ELSE
$DAY= "@MDAYNO"
ENDIF

$timestamp = "@YEAR" + "-" + "$MONTH" + "-" + "$DAY" + " " + @TIME
$ComputerName   = @WKSTA

$logfile1 = "I:\foldername\auditfile1.txt"
$logfile2 = "I:\foldername\auditfile2.txt"

$Result1 = GetFileTime("C:\Program Files\filename1.txt", 0)
$Result2 = GetFileTime("C:\Program Files\filename2.txt", 0)

$Result1string = "$TIMESTAMP;$ComputerName;$Result1"
$Result2string = "$TIMESTAMP;$ComputerName;$Result2"        

IF Open(3, $logfile1, 4 ) = 0
  $x = WriteLine( 3 , $Result1string + Chr(13) + Chr(10) )
ELSE
  BEEP
  ? "failed to open file, error code : [" + @ERROR + "]"
ENDIF

If Close(3)
  Beep
  ? "Error closing file!"
EndIf

IF Open(3, $logfile2, 4 ) = 0
  $x = WriteLine( 3 , $Result2string + Chr(13) + Chr(10) )
ELSE
  BEEP
  ? "failed to open file, error code : [" + @ERROR + "]"
ENDIF

If Close(3)
  Beep
  ? "Error closing file!"
EndIf

exit

Top
Page 1 of 2 12>


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

Who's Online
0 registered and 59 anonymous users online.
Newest Members
diefnet, Arogya, gkustra, emnipetro, Hirze
17644 Registered Users

Generated in 0.042 seconds in which 0.011 seconds were spent on a total of 14 queries. Zlib compression enabled.

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