5

When I join file names, it's inconvenient to use FileNameJoin. The workaround is to use <> instead, however <> is not exactly like FileNameJoin.

I tried to define an operator . It just doesn't work. If I can define it, I can use it to construct file name like this, "dir1" "dir2" <> "file".

In:

x_ < /> y_ := FileNameJoin[{x, y}]

Out:

Syntax::sntxf: "x_<" cannot be followed by "/>y_".

This is what I do, however I got an error. How can I fix it?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
webcpu
  • 3,182
  • 12
  • 17

2 Answers2

5

Suppose you were to choose TildeTilde as Michael E2 suggests. Then you don't need the Symbol Package since has interpretation as an operator already built-in.

TildeTilde[names___] := FileNameJoin[{names}]

Then

TildeTilde[$HomeDirectory, "Desktop", "my_file.dat"]

gives me

"/Users/oldmg/Desktop/my_file.dat"

and more to the point,

$HomeDirectory ≈ "Desktop" ≈ "my_file.dat"

"/Users/oldmg/Desktop/my_file.dat"

There is an input alias for . You can type Esc+~+~+Esc to get . Input aliases seem be defined for most (maybe all?) of the operators with no built-in meaning.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • 1
    No need to add an alias for \[TildeTilde], it already works. – Carl Woll May 31 '17 at 22:10
  • @CarlWoll. +1 I should have checked the docs. It looks like all the unassigned operator symbols have input aliases. I'll update my answer. – m_goldberg May 31 '17 at 22:32
  • 2
    Note that you can use WolframLanguageData (or EntityValue) to find out what the keyboard shortcuts are, e.g., WolframLanguageData["TildeTilde", "KeyboardShortcuts"] – Carl Woll May 31 '17 at 23:06
  • @CarlWoll. That seems like more trouble than consulting the Documentation Center. – m_goldberg May 31 '17 at 23:11
  • However, if you wanted to verify that all operators in a list have an input alias, it would be a lot easier to use WolframLanguageData than the Documentation Center. – Carl Woll May 31 '17 at 23:15
3

I favor the method m_goldberg shows but an alternative is Rojo's SubscriptBox method from Is it possible to define custom compound assignment operators like ⊕= similar to built-ins +=, *= etc?

MakeExpression[
  RowBox[arg : {_, PatternSequence[SubscriptBox["<>", "/"], _] ..}], 
  StandardForm
] := 
  MakeExpression @ RowBox[{"FileNameJoin", "[", "{", ##, "}", "]"}] & @@ 
    Riffle[arg[[;; ;; 2]], ","]

Now:

enter image description here

To enter this operator in Windows type < > (Ctrl+-) /

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371