mIRC Homepage

count # of extensions in a txt file

Posted By: Fantas

count # of extensions in a txt file - 23/09/07 05:13 PM

i'm looking for a way to count extensions in a text file like 1.jpg 2.gif etc
i know i would have to $read it and inc but i'm lost
Posted By: schaefer31

Re: count # of extensions in a txt file - 23/09/07 05:26 PM

It would help if you give the structure of the text file's contents.
Posted By: Fantas

Re: count # of extensions in a txt file - 23/09/07 05:44 PM

ok
1.jpg
3.gif
1.png
2.mov

what i'm looking for is a way to set a variable then inc that variable
Posted By: hixxy

Re: count # of extensions in a txt file - 23/09/07 06:01 PM

$lines(file.txt)
Posted By: Fantas

Re: count # of extensions in a txt file - 23/09/07 06:02 PM

yeah but $lines returns all lines not just ones with the extensions
Posted By: hixxy

Re: count # of extensions in a txt file - 23/09/07 06:15 PM

Code:
alias filterext {
  if ($isfile($2-)) {
    var %re = $+(/^\Q,$replacexcs($1,;,\E|\Q,\E,\E\\E\Q),\E$/)
    filter -ffg $qt($2-) nul %re
    echo -a * Found: $filtered
  }
}


/filterext <extensions> <file>

<extensions> should be a list of extensions you want to match like *.mp3;*.mpg;*.etc
Posted By: SladeKraven

Re: count # of extensions in a txt file - 23/09/07 06:20 PM

Code:
alias ext {
  var %x = 1, %f = $$1
  while (%x <= $lines%f)) {
    if ($gettok($read(%f,%x),2,46)) inc $+(%,$v1)
    inc %x
  }
}


/ext <file>
Posted By: hixxy

Re: count # of extensions in a txt file - 23/09/07 06:22 PM

There's a few problems with this script.

1) Setting %f to $$1 will not take into account files that have spaces in the name. Use $$1- instead.

2) $lines%f should be $lines(%f)

3) Calling $read without the 'n' flag will evaluate identifiers in the file.

Also, that loop could be greatly sped up by setting $lines(%f) to a variable instead of calling it in each iteration of the loop:

Code:
var %x = 1, %f = $$1-, %l = $lines(%f)
while (%x <= %l) {
...
}
Posted By: SladeKraven

Re: count # of extensions in a txt file - 23/09/07 06:31 PM

Ahh good find, getting a little tired here. Cheers. smile
Posted By: SladeKraven

Re: count # of extensions in a txt file - 23/09/07 06:38 PM

Code:
alias ext {
  var %n = ext
  .fopen %n $$1-
  if ($ferr) { .fclose %n | return }
  if ($fopen(%n)) {
    while (!$feof) {
      if ($gettok($fread(%n),2,46)) { inc $+(%,$v1) }
    }
    .fclose %n
  }
}


I think this might be a little faster than my $read post.
Posted By: Fantas

Re: count # of extensions in a txt file - 23/09/07 06:40 PM

ok not quite sure i follow ya here %f is my text file?
alias ext {
var %x = 1, %f = $$1-, %l = $lines(%f)
while (%x <= %l) {
if ($gettok($read(%f,%x),2,46)) inc $+(%,$v1)
inc %x
}
}
so i type /ext .jpg pics.txt?
Posted By: hixxy

Re: count # of extensions in a txt file - 23/09/07 06:41 PM

Mine is /filterext <extensions> <file>, Slade's is /ext <file>
Posted By: SladeKraven

Re: count # of extensions in a txt file - 23/09/07 06:41 PM

Nope, type:

/ext <file>

Then it'd look like:

%png 1
%jpg 2
%bmp 30

Or something similar
Posted By: Fantas

Re: count # of extensions in a txt file - 23/09/07 06:50 PM

Hixxys keeps returning 0 and /ext does nothing
i made a file called it pics.txt in the file i put
1.jpg
2.gif
1.png
3.mov
1.wmf

none worked
Posted By: hixxy

Re: count # of extensions in a txt file - 23/09/07 06:52 PM

How did you call my alias?
Posted By: Fantas

Re: count # of extensions in a txt file - 23/09/07 06:54 PM

/filterext .jpg pics.txt
Posted By: hixxy

Re: count # of extensions in a txt file - 23/09/07 07:03 PM

Silly mistake.. just remove the "^" character from my alias:

Code:
alias filterext {
  if ($isfile($2-)) {
    var %re = $+(/\Q,$replacexcs($1,;,\E|\Q,\E,\E\\E\Q),\E$/)
    filter -ffg $qt($2-) nul %re
    echo -a * Found: $filtered
  }
}
Posted By: Fantas

Re: count # of extensions in a txt file - 23/09/07 07:07 PM

that works Hixxy, thanks
© 2021 mIRC Discussion Forums