RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Manage text files with event commands

0 Members and 1 Guest are viewing this topic.

**
Rep: +0/-0Level 68
RMRK Junior
This script gives a little control to create and modifiy text files to eventers.
You can create a file, erase it, append text, add or remove a specific line.
Also you can configure the files extensions and paths.

You can ask for bugs, suggerences, compatibilitzations and any other things.

You can get the code in this simple web. Im going to use it on now because it saves alot of time updating posts and other things.

http://usuarios.multimania.es/kisap/english_list.html

Code: [Select]
#==============================================================================
# Manage text files with event commands
# By gerkrt/gerrtunk
# Version: 1
# License: GPL, credits
# Date: 05/01/2011
# IMPORTANT NOTE: to acces the more actualitzed or corrected version of this
# script check here: http://usuarios.multimania.es/kisap/english_list.html
#==============================================================================

=begin

-------INTRODUCTION-----------

This script gives a little control to create and modifiy text files to eventers.
You can create a file, erase it, append text, add or remove a specific line.
Also you can configure the files extensions and paths.

----------INSTRUCTIONS----------------

1. First of all you have to create the text file. You do this with the
script command newTxt. That will create a new text file called at your will.

2. Then you can erase it using delTxt or modify it and continue to append contents
using appTxt, that add new text after the existent one.

You can also check for the rest of the functions to do other things:

newTxt(name, text). This command create a new text file named 'name' and adds
the text 'text.

delTxt(name). This deletes the text file of the specified name.

  newTxt("test", "Initial text") --> Creates a test.txt file with  Initial text inside.
  delTxt("test") --> Erases it

appTxt(name, text). Appends at the end of the file a new text. If you make
a newTxt over a file that already exist it will be overwritten.

  With the file called "test":
  "start of file and line 1
  line 2
  line3
  end of file and line 4
  "
  If you do a appTxt("test", "Append \n me \n))  it will result in:
 
  "start of file and line 1
  line 2
  line3
  end of file and line 4
  Append
  me
 
  "

addLine(name, text, line). Insert a line of text in the specified position of
the file. A line means a piece of text thats beyond two \n (or the end of start
of the file). The insert moves all the lines after the one you are pushing, so:

  With the file called "test":
  "start of file and line 1
  line 2
  line3
  end of file and line 4
  "
  If you do a addLine("test", "test line \n", 2) it will be inserted where the
  actual line 2 is, so it will result in:
 
  "start of file and line 1
  test line
  line 2
  line3
  end of file and line 4
  "


delLines(name, lines). Deletes a list of lines from the file named 'name'.
Works in a similar way that the add line but deleting them. It also
accepts more lines to erase:


  With the file called "test":
  "start of file and line 1
  line 2
  line3
  end of file and line 4
  "
  If you do a delLine("test", [2,4])  it will result in:
 
  "start of file and line 1
  line3
  "

-------SPECIAL CARACTERS------------

You can add new lines marks and tabulations marks using these codes:

\t (tab)
\n (newline)

This works internally for the system, but the reader doesent see ethese symbols
but their true meaning. So if you write:

text = " testing \n \ttabulating it"

It will read as:

testing
  tabulating it

------CONFIGURATION-------------

Texts_file_path: Ifor you put this to empty "", it wont create a folder.
Also you have to add the \\ at the end of the folder name.

Texts_file_extension: You can changue the extension of all the text files.


=end

# Configuration
module Wep
  Texts_file_path = "Text Files\\"
  Texts_file_extension = ".txt"
end

 





# Create Folder for text files
if Wep::Texts_file_path  != ''
    Dir.mkdir(Wep::Texts_file_path) if !FileTest.directory?(Wep::Texts_file_path)
end
 
class Interpreter
  #--------------------------------------------------------------------------
  # * newTxt
  # creates a new text file
  # name: file name, text: text to write in the file
  #--------------------------------------------------------------------------
  def newTxt(name, text='')
    file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'w')
    file.write text
    file.close
  end
 
  #--------------------------------------------------------------------------
  # * delTxt
  # deletes a  text file
  # name: file name
  #--------------------------------------------------------------------------
  def delTxt(name)
    File.delete Wep::Texts_file_path + name + Wep::Texts_file_extension
  end
 
  #--------------------------------------------------------------------------
  # * addLine
  # insert line in text file
  # name: file name, text: text to write in the file, line: line number(-1 internal)
  #--------------------------------------------------------------------------
  def addLine(name, text, line)
    # Extract a array of lines from the file and insert text
    line_arr = File.readlines(Wep::Texts_file_path + name + Wep::Texts_file_extension)
    line_arr.insert(line-1, text)
    # Save it
    file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'w')
    file.write line_arr
    file.close
  end
 
  #--------------------------------------------------------------------------
  # * delLines
  # deletes lines in text file
  # name: file name, lines: array of line numbers to erase(-1 internal)
  #--------------------------------------------------------------------------
  def delLines (name, lines)
    # Extract a array of lines from the file and insert text
    line_arr = File.readlines(Wep::Texts_file_path + name + Wep::Texts_file_extension)
    # Delete specified lines
    lines.each do |index|
      line_arr.delete_at(index-1)
    end
    # Save it
    File.open(Wep::Texts_file_path+name+Wep::Texts_file_extension, "w") do |f|
      line_arr.each{|line| f.puts(line)}
    end
  end
 
  #--------------------------------------------------------------------------
  # * appTxt
  # appends text to a  text file
  # name: file name text: text to append
  #--------------------------------------------------------------------------
  def appTxt (name, text)
    file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'a')
    file.write text
    file.close
  end
 
end