6
$\begingroup$

Shift+RightClick a file on windows explorer and select 'copy as path', then ctrl+v paste to mathematica notebook, then apply FileExistsQ :

ex1)

FileExistsQ["C:\Users\Pi33\Desktop\FreeAlarmClockPortable"]
True

But the same method produces False sometimes :

ex2)

FileExistsQ["C:\Users\Pi33\Desktop\nircmd-x64"]
False

because in the string "\nircmd-x64", the character "\n" is interpreted as LF.

In fact for ex1), we got True with some error messages. The reason for the error message is
\Users
\Pi33
\Desktop
\FreeAlarmClockPortable
The use of "\U" or "\P" or "\D" or "\F" are not allowed, because backslash is a special character that needs one more backslash in front of it, to be part of a string. I mean, they should be "\\U", "\\P", "\\D", "\\F". I think that mathematica showed automatic correction ability for ex1).

But in ex2), mathematica didn't/couldn't show such ability for "\n" in "\nircmd-x64", becuase "\n" is just LF.

So when we do 'file/folder path copy and paste job' in mathematica, we need
pre-processing
(processing the content of clipboard then paste)
or
post-processing
(some process in mathematica after pasting the clipboard content to a mathematica notebook).

At present, I am using a post-processing method, probably easiest but the most inefficient : searching problematic backslashes (like \n, \r,..) with eyes, and typing one more backslash before such backslashes with fingers.

What kind of method do you use?

The method should be automatic and work for multiple files and folders.

$\endgroup$
5
  • $\begingroup$ Use FileNameJoin@{"C:", "Users", "Pi33", "Desktop", "FreeAlarmClockPortable"} or use \\ to manually escape the \ $\endgroup$
    – b3m2a1
    Commented Apr 29, 2022 at 22:31
  • $\begingroup$ Thank you, I want an automated method. What is the next step after copying path of hundres of files/folders into the clipboard? $\endgroup$
    – imida k
    Commented Apr 29, 2022 at 22:39
  • $\begingroup$ Just checking something...If you're wanting an automated solution, then can you just avoid the copy-paste altogether? So, if you have a root directory to start at, you might try something like FileNames[All, FileNameJoin[{$HomeDirectory, "MyFavoriteDirectory"}]] $\endgroup$
    – lericr
    Commented Apr 29, 2022 at 22:56
  • $\begingroup$ With FileNames, you can choose a depth to search as well as filename patterns to match. $\endgroup$
    – lericr
    Commented Apr 29, 2022 at 22:58
  • $\begingroup$ None of the answers helps if using a different Front-End, like Wolfram Cloud, Jupyter, or no front-end like the text kernel. $\endgroup$
    – rhermans
    Commented Sep 30, 2022 at 15:05

2 Answers 2

7
$\begingroup$

Instead of Shift + Right click > Copy Path, just select the files and press Ctrl+C. Paste will add the proper escaping as needed. You could also just drag and drop them using Ctrl + Shift (the drag icon should say Link instead of Copy).

$\endgroup$
1
  • $\begingroup$ Wow, I've never had such a daring thought. The Columbus's Egg! $\endgroup$
    – imida k
    Commented Apr 29, 2022 at 23:21
4
$\begingroup$

Here's a post-processed solution.

First copy-paste the file path to a Text cell, then run this in the following cell

escapeSlashes[cell_] :=
  CellPrint[
   ExpressionCell[FE`makePlainText@NotebookRead@cell, "Input"]
   ];
escapeSlashes[] :=
  escapeSlashes[PreviousCell[]];

enter image description here

Instead of the CellPrint[ExpressionCell[...]] you can also use CopyToClipboard[FE`makePlainText@NotebookRead@cell] to copy the escaped path so you can paste it elsewhere.

Obviously you could do a lot more with this but this fixes the basic problem I think.

$\endgroup$
1
  • $\begingroup$ Thank you, I even didn't know there is a feature like Text cell. A great knowledge! $\endgroup$
    – imida k
    Commented Apr 29, 2022 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.