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.
FileNameJoin@{"C:", "Users", "Pi33", "Desktop", "FreeAlarmClockPortable"}
or use\\
to manually escape the\
$\endgroup$FileNames[All, FileNameJoin[{$HomeDirectory, "MyFavoriteDirectory"}]]
$\endgroup$