The online racing simulator
Files - Create/Read/Edit/Delete

<?php 
######################################################################################
#Tutorial: Create/Edit/Read/Delete files and delete lines in a file.
#In this tutorial i show you some code examples. You may use these in your scripts aswell. 
#
#We use the OnMSO Playerevent , which registers the input of the message box ('T' keypress)
######################################################################################
Event OnMSO$userName$text # Player event
    
$idxOfFirtsSpace indexOf$text" ");

    IF( 
$idxOfFirtsSpace == -THEN
      $command 
$text;
      
$argv "";
    ELSE
      
$command subStr$text,0,$idxOfFirtsSpace );
      
$argv trimsubStr$text,$idxOfFirtsSpace ) );
    ENDIF
        
    SWITCH( 
$command )

            
#Create a file somewhere on your pc.

            
CASE "!createfile":
                IF (
UserIsAdmin($userName) == 1THEN
                    
#Action if file already exist
                    #0 = no action
                    #1 = overwrite file
                    #2 = overwrite file + create backup file.
                    
                    
$Filename "TestFile";
                    
$Folder "C:\Users\User\Desktop";
                    
$Action 2;

                    
CreateFile($Filename,$Folder,$Action);
                ENDIF
            BREAK;

            
#Read File

            
CASE "!readfile":
                IF (
UserIsAdmin($userName) == 1THEN
                    $Filename 
"TestFile";
                    
$Folder "C:\Users\User\Desktop";
                    
$Extension ".txt";

                    
$LinesofFile ReadFile($Filename,$Folder,$Extension);
                
                    
#Get Information from the file.
                    
$NrOfLines $LinesofFile["NumberOfLines"];
                    
$FileCreationDate $LinesofFile["TimeOfCreation"];
                    
$FileModificationDate $LinesofFile["TimeOfModification"];
                    
$FileSize $LinesofFile["FileSize"];
                    
                    
#Display info in the chat about the file 
                    
privmsg("Reading file: " $Filename ".txt");
                    
privmsg("NumberOfLines: " $NrOfLines "");
                    
privmsg("Size of file: " $FileSize " Bytes");
                    
privmsg("Creation time: " $FileCreationDate "");
                    
privmsg("Modify time: " $FileModificationDate "");
                    
                    
#Create a FOR loop to display each line from the file in the chat.

                    
FOR ( $i 0$i <= $NrOfLines-$i $i 1)
                        
$linenr $LinesofFile[$i,"LineNumber"];    #LineNr
                        
$line $LinesofFile[$i,"Line"];             #Text
                        
privmsg("[".$linenr."]: ".$line);            #Display LineNr and Text
                    
ENDFOR    
                ENDIF            
            BREAK;
            
            
#Edit File
            #You can edit a line or you can create a new one.
            #IF $LineToEdit = -1, a new line will be added in your created file. 
   
            
CASE "!editfile":
                IF (
UserIsAdmin($userName) == 1THEN
                    $Filename 
"TestFile";
                    
$Folder "C:\Users\User\Desktop";
                    
$NewText "NewText ";
                    
$LineToEdit 5;  # -1 to create a extra line
                    
$Extension ".txt";
 
                    
EditFile($Filename,$Folder,$NewText,$LineToEdit,$Extension);
                ENDIF
            BREAK;
            
            
#Delete File
            #By deleting a file, you can choose if you want to make a backup before deleting.
            #Keep value of '$CopyFolder' empty if you dont want to copy the file before deleting.
            #Otherwise enter the foldername where the backup need to be placed.

            
CASE "!deletefile":
                IF (
UserIsAdmin($userName) == 1THEN
                    $Filename 
"TestFile";
                    
$Folder "C:\Users\User\Desktop";
                    
$CopyFolder "";                    #$CopyFolder is empty , no backup will be created.
                    
$Extension ".txt";
                    
                    
DeleteFile($Filename,$Folder,$CopyFolder,$Extension);
                ENDIF
            BREAK;
            
            
#Delete a single line , based on a LineNr

            
CASE "!deleteline":
                IF (
UserIsAdmin($userName) == 1THEN
                
                    
#Enter -1 to $NewText to enable the "deleteline" function
                    #set the line @ $LineToEdit, which line that need to be deleted.
                    
                    
$Filename "TestFile";
                    
$Folder "C:\Users\User\Desktop";
                    
$NewText = -1;    # -1 = deleteline function.
                    
$LineToDelete 5;  
                    
$Extension ".txt";
 
                    
EditFile($Filename,$Folder,$NewText,$LineToDelete,$Extension);
                ENDIF
            BREAK;            
    ENDSWITCH
EndEvent
?>


Thx for info

FGED GREDG RDFGDR GSFDG