#172407 - 2007-01-04 10:16 PM
HELP! use GetFileTime function to add line to log file
|
beejay
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
|
|
|
|
#172413 - 2007-01-04 10:45 PM
Re: HELP! use GetFileTime function to add line to log file
[Re: Les]
|
beejay
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
|
|
|
|
#172422 - 2007-01-05 09:08 AM
Re: HELP! use GetFileTime function to add line to log file
[Re: Witto]
|
beejay
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
|
|
|
|
#172431 - 2007-01-05 12:32 PM
Re: HELP! use GetFileTime function to add line to log file
[Re: Björn]
|
beejay
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
|
|
|
|
#172441 - 2007-01-05 06:40 PM
Re: HELP! use GetFileTime function to add line to log file
[Re: Les]
|
Allen
KiX Supporter
Registered: 2003-04-19
Posts: 4549
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.
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
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
Registered: 2000-07-28
Posts: 11624
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
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
; 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
|
|
|
|
#172566 - 2007-01-09 10:19 AM
Re: HELP! use GetFileTime function to add line to log file
[Re: Les]
|
beejay
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
|
|
|
|
#172583 - 2007-01-09 05:28 PM
Re: HELP! use GetFileTime function to add line to log file
[Re: Lonkero]
|
beejay
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.
; * 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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 918 anonymous users online.
|
|
|