# Define an operator for FileNameJoin

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?

• it's possible to define new operators using the Notations  package (see other answers for details). But in your case maybe you just want an alias, say FNJ and then you can use x~FNJ~y?The notations all work from box forms, which can cause issues if you embed them in a package. – b3m2a1 May 31 '17 at 16:27
• @MB1965, I prefer </>, because it's like <>, but it can add /(path separator) automatically. Thanks, I will try it with Notations. – UnchartedWorks May 31 '17 at 16:50
• Perhaps you could choose an operator without built-in meanings from the tutorial Operators without Built-in Meanings – Carl Woll May 31 '17 at 17:40
• E.g., TildeTilde = FileNameJoin@*List: i.stack.imgur.com/XT6Jg.png – Michael E2 May 31 '17 at 17:43
• Possibly of interest: (27081), (31375) – Mr.Wizard May 31 '17 at 23:52

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.

• 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
• 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

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:

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

• How to type <>/ ? I tried to use the result of SubscriptBox["<>", "/"] // InputForm, however it doesn't work. – UnchartedWorks Jun 1 '17 at 8:01
• @UnchartedWorks I added the exact key presses needed in Windows. – Mr.Wizard Jun 1 '17 at 19:59