mIRC Home    About    Download    Register    News    Help

Print Thread
count # of extensions in a txt file #186496 23/09/07 05:13 PM
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
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

Re: count # of extensions in a txt file [Re: Fantas] #186498 23/09/07 05:26 PM
Joined: Aug 2005
Posts: 525
S
schaefer31 Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2005
Posts: 525
It would help if you give the structure of the text file's contents.

Re: count # of extensions in a txt file [Re: schaefer31] #186501 23/09/07 05:44 PM
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
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

Re: count # of extensions in a txt file [Re: Fantas] #186505 23/09/07 06:01 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
$lines(file.txt)

Re: count # of extensions in a txt file [Re: hixxy] #186506 23/09/07 06:02 PM
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
yeah but $lines returns all lines not just ones with the extensions

Re: count # of extensions in a txt file [Re: Fantas] #186509 23/09/07 06:15 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
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

Re: count # of extensions in a txt file [Re: Fantas] #186510 23/09/07 06:20 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
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>

Re: count # of extensions in a txt file [Re: SladeKraven] #186511 23/09/07 06:22 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
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) {
...
}

Re: count # of extensions in a txt file [Re: hixxy] #186517 23/09/07 06:31 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Ahh good find, getting a little tired here. Cheers. smile

Re: count # of extensions in a txt file [Re: Fantas] #186520 23/09/07 06:38 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
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.

Re: count # of extensions in a txt file [Re: SladeKraven] #186521 23/09/07 06:40 PM
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
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?

Re: count # of extensions in a txt file [Re: Fantas] #186522 23/09/07 06:41 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
Mine is /filterext <extensions> <file>, Slade's is /ext <file>

Re: count # of extensions in a txt file [Re: Fantas] #186523 23/09/07 06:41 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Nope, type:

/ext <file>

Then it'd look like:

%png 1
%jpg 2
%bmp 30

Or something similar

Re: count # of extensions in a txt file [Re: SladeKraven] #186526 23/09/07 06:50 PM
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
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

Re: count # of extensions in a txt file [Re: Fantas] #186528 23/09/07 06:52 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
How did you call my alias?

Re: count # of extensions in a txt file [Re: hixxy] #186530 23/09/07 06:54 PM
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
/filterext .jpg pics.txt

Re: count # of extensions in a txt file [Re: Fantas] #186535 23/09/07 07:03 PM
Joined: Sep 2005
Posts: 2,881
H
hixxy Offline
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
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
  }
}

Re: count # of extensions in a txt file [Re: hixxy] #186539 23/09/07 07:07 PM
Joined: Dec 2002
Posts: 79
F
Fantas Offline OP
Babel fish
OP Offline
Babel fish
F
Joined: Dec 2002
Posts: 79
that works Hixxy, thanks