From WikiChip
Difference between revisions of "mirc/text files"
< mirc

m (Reverted edits by 187.60.173.6 (talk) to last revision by PatrolBot)
(Replaced content with "Hey, thanks for the post.Really thank you! Really Cool. fadbffgeefgdggkc")
Line 1: Line 1:
{{mirc title|Text Files}}
+
Hey, thanks for the post.Really thank you! Really Cool. fadbffgeefgdggkc
'''Plain text files''' are files you can edit via a basic editor like notepad and has no special structure. Below are a few handy commands and identifiers to work with plain text files.
 
 
 
== File Info ==
 
To determine if a file exists we can use the $isfile() identifier.
 
 
 
<syntaxhighlight lang="mirc">$isfile(file.txt)</syntaxhighlight>
 
 
 
In many cases you'd want to check the number of lines in the file. $lines() will help you there.
 
 
 
<syntaxhighlight lang="mirc">$lines(file.txt)</syntaxhighlight>
 
 
 
== Reading From a Text File ==
 
The $read() identifier is a very powerful command that can be used to read from a text file in a variety of ways.
 
 
 
=== n Switch ===
 
By default, $read will evaluate the text it reads as if it was mSL code. To prevent this behavior you must use the n switch. Throughout this article we will ALWAYS use that switch. Improper use of the $read() identifier without the 'n' switch could {{mIRC|mSL Injection|leave your script highly vulnerable}}.
 
 
 
=== Reading a Random Line ===
 
The most basic functionality $read() offers is the ability to read a random line from a particular file. The syntax is:
 
 
 
<syntaxhighlight lang="mirc">; read a random line from file.txt
 
$read(file.txt, n)</syntaxhighlight>
 
 
 
=== Reading a Specific Line ===
 
To read a specific line from a file you can specify the line number as the third argument.
 
 
 
<syntaxhighlight lang="mirc">$read(file.txt, n, line)</syntaxhighlight>
 
 
 
=== Searching the File ===
 
$read() offers three methods for searching a file:
 
 
 
* Scanner
 
* Wildcard Pattern
 
* Regular Expression Pattern
 
 
 
==== Scanner ====
 
The scanner is the most primitive search of the three. It will go through each line comparing the pattern provided to the first part of the line. If a match is found, mIRC will return the text that followed the pattern.
 
 
 
Consider the following abbr.txt:
 
 
 
<pre>lol laughing out loud
 
lmao Laughing my ass off
 
btw by the way
 
brb be right back</pre>
 
 
 
We can use the following alias to get the abbreviation we are looking for.
 
 
 
<syntaxhighlight lang="mirc">alias abbr return $read(abbr.txt, ns, $1)</syntaxhighlight>
 
 
 
Executing the following code:
 
 
 
<syntaxhighlight lang="mirc">//echo -a $abbr(lol)
 
//echo -a $abbr(brb)</syntaxhighlight>
 
 
 
Will produce the following output:
 
 
 
<pre>laughing out loud
 
be right back</pre>
 
 
 
==== Wildcard And RegEx Patterns ====
 
Both the wildcard pattern matching and the regex pattern matching works by searching for the first matching line and returning the entire line. It follows the same syntax as the scanner:
 
 
 
<syntaxhighlight lang="mirc">; A wildcard pattern match:
 
$read(file.txt, nw, *wildmatch*)
 
 
 
; A regex pattern match:
 
$read(file.txt, nr, /pattern/)</syntaxhighlight>
 
 
 
==== Starting Line ====
 
If you specify a line number after the pattern, that line will be used as the first line to start searching from.
 
 
 
For Example:
 
 
 
<syntaxhighlight lang="mirc">; Start searching from line 400:
 
$read(file.txt, nw, *hello*, 400)</syntaxhighlight>
 
 
 
== Iterating Over Matches ==
 
$readn is an identifier that returns the line that $read() matched. We can use that to start searching for our pattern on the next line.
 
 
 
For example, to search all the line containing the word 'test' in a file, we can construct a loop like this:
 
 
 
<syntaxhighlight lang="mirc">//while ($read(file.txt, nw, *test*, $calc($readn + 1))) echo -a $v1</syntaxhighlight>
 
 
 
In the code above, $readn starts at 0. We use $calc() to start at line 1. Every match $read() will start searching on the next line. When no more matches are after the line specified $read will return $null - terminating the loop.
 
 
 
== Writing to a Plain Text File ==
 
The /write command can be used to manipulate a text file in a variety of ways.
 
 
 
=== Appending a Line ===
 
/write's simplest operation is the append operation. By default, /write will write a text line to the end of the file.
 
 
 
<syntaxhighlight lang="mirc">/write text.txt <string></syntaxhighlight>
 
 
 
=== Inserting a Line ===
 
To insert text at specific line we have the following syntax:
 
 
 
<syntaxhighlight lang="mirc">/write -il<line> file.txt <text></syntaxhighlight>
 
 
 
For example, the following line will write "Hello There!" at line 2.
 
 
 
<syntaxhighlight lang="mirc">/write -il2 file.txt Hello There!</syntaxhighlight>
 
 
 
=== Deleting a Line ===
 
The /write command provides the ability to delete a specific line from a file.
 
 
 
<syntaxhighlight lang="mirc">; Delete line <line> from a file:
 
/write -dl<line> file.txt</syntaxhighlight>
 
 
 
== Clearing A File ==
 
The -c switch on /write can be combined to clear the file before writing to it.
 
 
 
<syntaxhighlight lang="mirc">; clear the file
 
/write -c file.txt</syntaxhighlight>
 
 
 
== Deleting a File ==
 
The delete a file, you can use the /remove command:
 
 
 
<syntaxhighlight lang="mirc">/remove file.txt
 
; send to the recycle bin
 
/remove -b file.txt</syntaxhighlight>
 
 
 
[[Category:mIRC]]
 

Revision as of 09:31, 6 March 2017

Hey, thanks for the post.Really thank you! Really Cool. fadbffgeefgdggkc