mIRC Homepage
Posted By: dassa $findfile and /remove - 24/04/08 11:54 AM
Hi
I am trying to cut files from the root mIRC dir to another dir. Problem is i have to run the alias a few times to get the desired results which is annoying as using /echo it echoes all of them. I cannot work out what the error is. I paste the code below and any suggestions will be very much appreciated.

Code:
alias cut.files {
  var %x = 1
  while ($findfile($mircdir,irc*,%x,1)) { 
    var %file = $qt($v1)
  if (%file != irc-companion) { copy -o %file $scriptdir($gettok(%file,-1,92)) | remove %file | inc %x } }
}
Posted By: Horstl Re: $findfile and /remove - 24/04/08 12:10 PM
Every time you remove a file and inc the file-count %x, $findfile(dir,N) will not point at the next file but the file after the next file. The next file would be the file with the same number, as the "current" file with this number had been removed smile
To fix this, e.g. change your while loop to inc the number only if your (file != something)-condition is not met and the file has not been removed.

Code:
alias cut.files {
  var %x = 1
  while ($findfile($mircdir,test*,%x,1)) { 
    var %file = $qt($v1)
    if (%file != $qt($+($mircdir,testing.txt))) {
      copy -o %file $+($scriptdir,$nopath(%file))
      remove %file 
    }
    else { inc %x }
  }
}

Edit: Note that - now - this should pass all files correctly if you execute the copy/remove command, but will result in an infinite loop if you echo the command only.
Posted By: genius_at_work Re: $findfile and /remove - 24/04/08 01:50 PM
To solve the problem mentioned by Horstl, you can use a decreasing loop instead of an increasing one.

Code:

alias cut.files {
  var %x = $calc($findfile($mircdir,irc*,0,1) + 1), %xx = 1
  while (%x > %xx) {
    dec %x
    var %file = $qt($findfile($mircdir,irc*,%x,1))
    if (%file != irc-companion) { 
      copy -o %file $scriptdir($gettok(%file,-1,92)) 
      remove %file
    } 
  }
}


(untested)

-genius_at_work
Posted By: qwerty Re: $findfile and /remove - 24/04/08 02:08 PM
Quite a few problems with that code.
  • /inc %x should be inside an /else, corresponding to your /if: if the file is moved, %x should not be incremented, for the reason explained by Horstl. Otherwise, %x should be incremented.
  • %file will never equal "irc-companion" because it is the full filename, including the path (eg it would look like "C:\Program files\mIRC\irc-companion")
  • $scriptdir does not accept parameters. You just need $+ to concatenate $scriptdir with the filename.
However, this method of using $findfile is very inefficient and will cause noticeable delay if you have relatively many files in $mircdir. Here's a simpler and faster way, that also shows how to use /rename to move files directly. This has the advantage that if $scriptdir is on the same partition as $mircdir, no copying of data is performed: the files are moved directly, which is much faster and doesn't require extra disk space.

Code:
alias cut.files {
  return $findfile($mircdir,irc*,0,1,if ($nopath($1-) != irc-companion) rename $qt($1-) $qt($scriptdir $+ $v1)))
}

If you don't want to see the * Renamed blahblah message, prefix the /rename command with a dot.

There is one potential problem with /rename: I noticed you're using the -o switch in /copy to force an overwrite if the destination exists. /rename does not offer this functionality, so if this is important, you'll have to first delete the destination file, if it exists. To do that, you'd have to create an alias that contains a /remove and then a /rename and use that inside $findfile.
Posted By: RoCk Re: $findfile and /remove - 24/04/08 02:28 PM

Well here's my contribution, which I made before seeing qwerty's method (his is much better).

Code:

alias cut.files {
  if (%cut.files) {
    if ($nopath($1) != irc-companion) {
      if ($isfile($+($scriptdir,$nopath($1)))) .remove $qt($+($scriptdir,$nopath($1)))
      .rename $qt($1) $qt($+($scriptdir,$nopath($1)))
    }
    return
  }
  set %cut.files $true
  window -hl @cut.files
  noop $findfile($mircdir,irc*,0,0,@cut.files)
  filter -wk @cut.files cut.files *
  unset %cut.files
  window -c @cut.files
}



~ Edit ~
Imagining using the if statement in the command section of $findfile would have made all the difference for me lol. Ahh well. Very nicely done as usual qwerty.
smile

~ Edit ~
Added the
/remove per qwerty's suggestion so no second alias is necessary.
Posted By: dassa Re: $findfile and /remove - 25/04/08 09:54 AM
wow thanks for all the replies. qwerty i like you approach alot and thanks for that.
© mIRC Discussion Forums