|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
Oh, and you are missing the file handles too. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
The board is also full of examples opening files in write mode http://www.kixtart.org/forums/ubbthreads...true#Post168511 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
You still didn't open the file for write. YOu need to supply the 3rd parm. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. . |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
$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. |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
can you post your current code... |
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
k, one more... change: Code: IF Open(3, $logfile1, 4 ) = 0 $x = WriteLine( 3 , $Result1string + Chr(13) + Chr(10) ) ELSE BEEP ? "failed to open file, error code : [" + @ERROR + "]" ENDIF to: Code: ? IF Open(3, $logfile1, 4 ) = 0 $x = WriteLine( 3 , $Result1string + Chr(13) + Chr(10) ) if @error beep "Failed to write to the darn file. " @error ? else "write succeeded." ? endif ELSE BEEP ? "failed to open file, error code : [" + @ERROR + "]" ENDIF sleep 2 |
||||||||
|
|
|||||||
If the target file does not exist, the mode should be 5 (4 + 1)? Open(3, $logfile1, 5 ) |
||||||||
|
|
|||||||
he said he made the file. thus, there are at least 4 possibilities with his posted code: - he has no rights to write to the file - the drive is full - he is not actually running the posted code - the code is part of larger file and it exits before it or jumps over the part but, what the actual reason why the write fails will be shown with the error showing code I modified for him. |
||||||||
|
|
|||||||
Okay beejay, since you've been attempting to learn how to do it I'll give you an A for effort and I'll provide you with some working code. As with most scripts it can be accomplished many ways and my example is just one of many. Code: ;Set options If Not @LogonMode Break On Else Break Off EndIf Dim $SO $SO=SetOption('Explicit','On') $SO=SetOption('NoVarsInStrings','On') $SO=SetOption('NoMacrosInStrings','On') $SO=SetOption('WrapAtEOL','On') ;Declare our variables Dim $TimeStamp, $LogFile1, $LogFile2, $Handle, $WL, $OpenFile, $CloseFile Dim $Result1, $Result2 Dim $Result1string, $Result2string ;Use Split and Join to put the date and time into a variable $TimeStamp = Trim(Join(Split(@DATE,'/'),'-'))+'_'+Trim(Join(Split(@TIME,':'),'')) ;Place our Logfile locations into variables $LogFile1 = 'I:\TEST\auditfile1.txt' $logfile2 = 'I:\TEST\auditfile2.txt' ;Place the files we want to get the file time from into variables $Result1 = GetFileTime('C:\Program Files\filename1.txt',0) $Result2 = GetFileTime('C:\Program Files\filename2.txt',0) ;Place the full results into variables including a comma for csv style format $Result1string = $TIMESTAMP + ',' + @WKSTA + ',' + $Result1 $Result2string = $TIMESTAMP + ',' + @WKSTA + ',' + $Result2 ;Use FreeFileHandle to find an available file handle to use $Handle = FreeFileHandle() If $Handle > 0 $OpenFile = Open($Handle,$LogFile1,5) If @ERROR 'Error opening LogFile1: ' + @ERROR + ' - ' + @SERROR ? Else $WL = WriteLine($Handle, $Result1string) If @ERROR 'Error updating LogFile1: ' + @ERROR + ' - ' + @SERROR ? EndIf EndIf $CloseFile = Close($Handle) EndIf ;We close the file and move on to the next file ;If we did not close the file we could not use the same variable for the Handle $Handle = FreeFileHandle() If $Handle > 0 $OpenFile = Open($Handle,$LogFile2,5) If @ERROR 'Error opening LogFile2: ' + @ERROR + ' - ' + @SERROR ? Else $WL = WriteLine($Handle, $Result1string) If @ERROR 'Error updating LogFile2: ' + @ERROR + ' - ' + @SERROR ? EndIf EndIf $CloseFile = Close($Handle) EndIf . |
||||||||
|
|
|||||||
Here's my revised code - but It's still not doing the trick. - it's not beeping either 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) ) if @error beep "Failed to write to the darn file. " @error ? else "write succeeded." ? endif ELSE BEEP ? "failed to open file, error code : [" + @ERROR + "]" ENDIF sleep 2 If Close(3) Beep ? "Error closing file!" EndIf ? IF Open(3, $logfile2, 4 ) = 0 $x = WriteLine( 3 , $Result2string + Chr(13) + Chr(10) ) if @error beep "Failed to write to the darn file. " @error ? else "write succeeded." ? endif ELSE BEEP ? "failed to open file, error code : [" + @ERROR + "]" ENDIF sleep 2 exit |
||||||||
|
|
|||||||
Well sorry but I gave you working code already. I don't have time to keep debugging your old code tonight. Please adapt the code I provided you or wait for one of the other guys to assist you as it's time for me to hit the sack. . |
||||||||
|
|
|||||||
NTDOC: OK - Thanks & goodnight - I'll try adapting it & let you / colleagues know if I manage to overcome this little b****** Cheers |
||||||||
|
|
|||||||
Hi all I've tried a few tweaks and to simplify the code - the latest effort is below, but I still can't get the little b***** to play ball & do the job. getting very frustrated with this one Thanks from one kixtart novice 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) IF Open( 3 , "I:\foldername\auditfile1.txt" , 5 ) = 0 $x = WriteLine( 3 , $TIMESTAMP;$ComputerName;$Result1 + 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 , "I:\foldername\auditfile2.txt" , 5 ) = 0 $x = WriteLine( 3 , $TIMESTAMP;$ComputerName;$Result2 + Chr(13) + Chr(10) ) ELSE BEEP ? "failed to open file, error code : [" + @ERROR + "]" ENDIF If Close(3) Beep ? "Error closing file!" EndIf exit |
||||||||
|
|
|||||||
NTDOC: I've used your script that you did last night - thanks for spending time on it; on logging on, however, the screen displays an error message for a fraction of a second: 000Script error : expected expression !. $SO=SetOption('Explicit','On') it doesn't add any data to the log files as requested. Does this error mean anything to you? (sorry but I don't know) As ever, entirely grateful. |
||||||||
|
|
|||||||
yes, you have old kixtart version. however, your old code should have worked and if it didn't beep, you should check again that it actually ran at all. the code was written in such a manner that it always outputs something and sleeps a bit. so if you can't get anything to the console with it, you are not actually running it. anyway, you could start by upgrading your kixtart to at least 4.22 |
||||||||
|
|
|||||||
Quote: you could start by upgrading your kixtart to at least 4.22 And if you're going to upgrade to 4.22 then you might as well upgrade to the latest release version 4.53 As Jooel was saying though even a very old KiXtart 3.63 should give output with the code he was helping you with. The code I gave you is a bit newer and requires the 4.x version of KiXtart. You should also be running/testing this as a stand-alone script and not as part of your main or logon script until you get it working. . |
||||||||
|
|
|||||||
I've updated to 4.53 version replacing ALL 6 kixtart files (before I had only replaced kix32.exe (windows 2003 server) as in the guidance - and EUREKA!!!. Before I sign off (until next time) a big THANK YOU to all involved, for your patience and help, especially NTDOC for writing the script now in use. Au revoir Beejay |
||||||||
|
|
|||||||
6 files? Unless you have WIntendos, you need only the KiX32.exe and WKiX32.exe files. |