mIRC Homepage
Posted By: Thels /fopen additional option - 08/09/10 09:48 AM
Quote:
File Handling Commands

/fopen [-no] <name> <filename>

Opens the specified file and assigns a name to it for later reference. The open fails if the file does not exist. The -n switch creates the file if it does not already exist, fails if the file does exist. The -o switch creates a new file, overwrites if it exists.


It would be lovely, if there was also an option that would create the file if it doesn't exist, and open it if it already exists, in case you just want to append to files.
Posted By: RusselB Re: /fopen additional option - 09/09/10 04:56 AM
Makes sense to me, and, imo, using a (for append) is an obvious choice. Of course, the first problem I see, is if you get someone specifying multiple options. Eg: /fopen -ao
Which should be used? or should the order set precedence?
Posted By: FroggieDaFrog Re: /fopen additional option - 09/09/10 07:54 AM
You could just rewrite /fopen, here is an example:

Code:
Opens the specified file and assigns a name to it for later reference
-n switch creates the file if it does not already exist, fails if the file does exist
-o switch creates a new file, overwriting it if it exists
-a switch opens a file if it exists, creates it if it doesn't

alias fopen {

  ;set up /fopen command to use mIRC's internal command
  ;also check to see if fopen has been called with a preceeding "." to
  ;not show it's results
  ;%fopen looks something like: !.fopen  
  var %fopen = $+(!,$iif(!$show,.),fopen)

  ;check to see if any switches have been specified, if so make sure they are valid.  
  if (-* iswm $1 && !$regex($1,/^(?:n|o|a)$/i)) {
    ;handle the syntax error how u feel
  }
  
  ;if the switches are valid, and the switch is -a
  elseif ($1 == -a) {
  
    ;check to see if the file exists, and if so, open it with /fopen
    if ($isfile($3-)) {
      %fopen $2-
    }
    
    ;if the file doesn't exist, open it with "/fopen -n" to create it.
    else {
      %fopen -n $2-
    }
  }
  
  ;if /fopen was called without any of the improvements we are trying to make, use the sent command as is.
  else {
    %fopen $1- 
  }
}

I haven't tested it or anything but the concept is pretty easy to follow
Posted By: hixxy Re: /fopen additional option - 09/09/10 08:25 AM
" var %fopen = $+(!,$iif(!$show,.),fopen)"

This line is unnecessary as mIRC automatically prefixes all commands called within an alias with a "." if the alias itself was called with a "."

Try this:

Code:
alias test echo -q hello


/test echoes "hello"

/.test does nothing.

Quite a handy feature smile
Posted By: Thels Re: /fopen additional option - 09/09/10 08:50 AM
Originally Posted By: RusselB
Makes sense to me, and, imo, using a (for append) is an obvious choice. Of course, the first problem I see, is if you get someone specifying multiple options. Eg: /fopen -ao
Which should be used? or should the order set precedence?


Afaik, when conflicting switches are given, one specific switch takes priority, regardless of the order of switches.

Wouldn't -n and -o already conflict?

And yes, I know it's fairly easy to script. I just found it odd that with the various switches, there's no "append" switch, while that is the one that feels the most natural to me.
Posted By: Knoeki Re: /fopen additional option - 09/09/10 10:04 AM
I second this motion. Right now I need to check if a file already exists, create the file if it doesn't, then open it.
Posted By: Thels Re: /fopen additional option - 09/09/10 10:37 AM
Originally Posted By: Knoeki
I second this motion. Right now I need to check if a file already exists, create the file if it doesn't, then open it.


Create it separately?

I use -n if it doesn't exist, and no switches if it does.

Still, it's something lile:

Code:
fopen $iif(!$exists(%filename), -n) file %filename


Where this would be a lot nicer:

Code:
fopen -a file %filename
Posted By: Knoeki Re: /fopen additional option - 09/09/10 11:39 AM
Originally Posted By: Thels
Originally Posted By: Knoeki
I second this motion. Right now I need to check if a file already exists, create the file if it doesn't, then open it.


Create it separately?

I use -n if it doesn't exist, and no switches if it does.


That's pretty much what I'm doing. Sorry if it wasn't clear from what I posted ;_)
© mIRC Discussion Forums