I am using a relative file path in one of the cs file to get a location to save an image.
Is there any difference in using ../ and ..// for getting the path.
I don't know if your slashes are actually backslashes, but in c#, you have to escape backslashes.
var path = "..\\file.txt";
path's value is actually ..\file.txt, because the "\" is actually one (escaped) backslash.
However, if it is:
var path = #"..\file.txt";
then it is the same. The # means you want the string as-is, without any escaping, so both "path" variables are the same.
On Unix, and I think MS-DOS and hence Windows follows Unix closely enough here that it is not a difference between the systems, then you can have any number of consecutive slashes at any point in a pathname and it is equivalent to a single slash. Consequently, your two examples are equivalent.
Note that on Windows, a double slash at the start of a path name indicates a UNC path - a machine name followed by a path on that machine.
Related
What is the difference between "C:\Android\sdk" and "C:\Android\sdk\" when adding path in environment variables or any other place?
There is no difference. A trailing backslash (or slash in the case of UNIX) is ignored by the operating system. There are only two uses that I know of. First, dealing with broken programs that incorrectly assume a trailing slash/backslash is present when concatenating the value to another relative path. Second, a very small number of commands (e.g., scp) might assign special meaning to a trailing path delimiter.
Certainly an old issue, but I was not able to find a solution (maybe there are none). On Unix it is straight forward to use the R function file.path to obtain the path to some file. How can the same thing be done under Windows when spaces in paths return with ~ .
If I need to write, say the path to Rscript.exe to a file, this would work on unix:
x <- list.files(R.home("bin"), full.names = T, pattern = "Rscript")
writeLines(x, con = "path_to_rscript.txt")
On Windows the result is:
C:/PROGRA~1/R/R-35~1.1/bin/x64/Rscript.exe
Where I would have wanted something like:
C:/Program Files/R-3.5.1/bin/x64/Rscript.exe
Is there a way to circumvent this behavior (and what is it with the capitalized PROGRA ?).
Indeed, checkout normalizePath:
normalizePath(path, winslash = "\\", mustWork = NA)
which states explicitly:
On Windows it converts relative paths to absolute paths, converts
short names for path elements to long names and ensures the separator
is that specified by winslash. It will match paths case-insensitively
and return the canonical case. UTF-8-encoded paths not valid in the
current locale can be used.
I am looking for an efficient way to convert back slash to forward slash in R. Sometime I copy the link of the directory in Windows and I get something like this:
C:\Users\jd\Documents\folder\file.txt
How can I quickly change this to C:/Users/jd/Documents/folder/file.txt ? I cannot even read the above expression as character. It throws an error
"\u used without hex digits in character string starting ""C:\u".
I know TAB function in R helps to find the location fast, but was just wondering if there was any other work around. I could change the working directory to the location of folder also. I was just playing around and tried to convert backslash to forward slash and was not straight forward so asked this just because of curiosity.
In R, you've to escape the \ with \\ So, your path should be:
x <- "C:\\Users\\jd\\Documents\\folder\\file.txt"
To get that, you can do:
x <- readline()
then, at the prompt, paste your unmodified path (CTRL+V then ENTER)
Finally, to change \\ to / everywhere, you could use gsub, once again by escaping the \, but twice, as follows:
gsub("\\\\", "/", x)
# [1] "C:/Users/jd/Documents/folder/file.txt"
If you want the least number of keystrokes to convert backslashes when pasting paths, use an RStudio snippet defined as follows:
snippet pp
"`r gsub('"', "", gsub("\\\\", "/", readClipboard()))`"
Remember to preface the second line with a tab, not multiple spaces for the snippet to work.
Then type pp, TAB, ENTER and the text on your clipboard is pasted, backslashes replaced with forward slashes and surrounded by quotes.
Here is the steps I usually take to copy file paths to RStudio once the above snippet has been defined:
Navigate to file path in explorer.
If copying a file path then: Shift + Right click on file, then click Copy as path.
If copying a folder path then: Alt + d, Ctrl + c.
Change window to RStudio and focus in R script where you want to paste the path.
pp, TAB, ENTER to paste into RStudio and convert backslashes to forward slashes.
I use Path Copy Copy, which is a plug-in to Windows that allows you to create custom copy commands when you right-click a file/folder in Windows. So my right-click menu has "Copy Full Path with Forward Slash" as an option, which copies the file/folder with forward slashes. I am guessing it saves me days every year from manually changing slashes to R's format.
I like to use the RStudio add-in snippetsaddin which has the function 'Convert slash':
It will reverse all slashes either in the selected block(s) of code, or if there is no selection (or only whitespace is selected), it will reverse all slashes in the clipboard and paste it to the current cursor(s) position(s).
Addins are isntalled like a package. To install this one, do this:
devtools::install_github("sfr/RStudio-Addin-Snippets", type = "source")
If I understand correctly, you do want to get rid of the string editing. In order to be able to use gsub you would have to change all the \ to \\ manually first. So, why not just change \ to / in first place?
If you have the string in the clipboard you can use
x=scan("clipboard",what="string")
This gives
"C:\\Users\\jd\\Documents\\folder\\file.txt"
That is, it converts all \ to \\ automatically.
I know - not very handy, but the only way I know to get around the editing.
Here is a one step method of converting the address from the clipboard
x <- gsub ( "\\\\", "/", readClipboard () )
autohotkey program:
^+v::
StringReplace, clipboard, clipboard, \,/,All
send %clipboard%
after control+c the file path, use control + shift + v to paste
A solution without a snippet defintion is
writeClipboard(gsub("\\\\", "/", readClipboard()))
R has the inbuilt r"(C:\myfolder\)" command which converts backslashes in a string to double backslashes.
x <- r"(C:\myfolder\)"
print(x)
"C:\\myfolder\\"
Only problem is that it can't take a variable and can only take a typed string
I think the best way to get rid of the hassle is to find the file in Rstudio in the right panel. And then click "more" and click "Set as Working Directory". Then you will see in the console "setwd(...)". You can copy this to your code.
My method is the same as what #pluke stated.
This is how I set my working directory:
x <- r"(C:\myfolder\)"
setwd(x)
I am looking for an efficient way to convert back slash to forward slash in R. Sometime I copy the link of the directory in Windows and I get something like this:
C:\Users\jd\Documents\folder\file.txt
How can I quickly change this to C:/Users/jd/Documents/folder/file.txt ? I cannot even read the above expression as character. It throws an error
"\u used without hex digits in character string starting ""C:\u".
I know TAB function in R helps to find the location fast, but was just wondering if there was any other work around. I could change the working directory to the location of folder also. I was just playing around and tried to convert backslash to forward slash and was not straight forward so asked this just because of curiosity.
In R, you've to escape the \ with \\ So, your path should be:
x <- "C:\\Users\\jd\\Documents\\folder\\file.txt"
To get that, you can do:
x <- readline()
then, at the prompt, paste your unmodified path (CTRL+V then ENTER)
Finally, to change \\ to / everywhere, you could use gsub, once again by escaping the \, but twice, as follows:
gsub("\\\\", "/", x)
# [1] "C:/Users/jd/Documents/folder/file.txt"
If you want the least number of keystrokes to convert backslashes when pasting paths, use an RStudio snippet defined as follows:
snippet pp
"`r gsub('"', "", gsub("\\\\", "/", readClipboard()))`"
Remember to preface the second line with a tab, not multiple spaces for the snippet to work.
Then type pp, TAB, ENTER and the text on your clipboard is pasted, backslashes replaced with forward slashes and surrounded by quotes.
Here is the steps I usually take to copy file paths to RStudio once the above snippet has been defined:
Navigate to file path in explorer.
If copying a file path then: Shift + Right click on file, then click Copy as path.
If copying a folder path then: Alt + d, Ctrl + c.
Change window to RStudio and focus in R script where you want to paste the path.
pp, TAB, ENTER to paste into RStudio and convert backslashes to forward slashes.
I use Path Copy Copy, which is a plug-in to Windows that allows you to create custom copy commands when you right-click a file/folder in Windows. So my right-click menu has "Copy Full Path with Forward Slash" as an option, which copies the file/folder with forward slashes. I am guessing it saves me days every year from manually changing slashes to R's format.
I like to use the RStudio add-in snippetsaddin which has the function 'Convert slash':
It will reverse all slashes either in the selected block(s) of code, or if there is no selection (or only whitespace is selected), it will reverse all slashes in the clipboard and paste it to the current cursor(s) position(s).
Addins are isntalled like a package. To install this one, do this:
devtools::install_github("sfr/RStudio-Addin-Snippets", type = "source")
If I understand correctly, you do want to get rid of the string editing. In order to be able to use gsub you would have to change all the \ to \\ manually first. So, why not just change \ to / in first place?
If you have the string in the clipboard you can use
x=scan("clipboard",what="string")
This gives
"C:\\Users\\jd\\Documents\\folder\\file.txt"
That is, it converts all \ to \\ automatically.
I know - not very handy, but the only way I know to get around the editing.
Here is a one step method of converting the address from the clipboard
x <- gsub ( "\\\\", "/", readClipboard () )
autohotkey program:
^+v::
StringReplace, clipboard, clipboard, \,/,All
send %clipboard%
after control+c the file path, use control + shift + v to paste
A solution without a snippet defintion is
writeClipboard(gsub("\\\\", "/", readClipboard()))
R has the inbuilt r"(C:\myfolder\)" command which converts backslashes in a string to double backslashes.
x <- r"(C:\myfolder\)"
print(x)
"C:\\myfolder\\"
Only problem is that it can't take a variable and can only take a typed string
I think the best way to get rid of the hassle is to find the file in Rstudio in the right panel. And then click "more" and click "Set as Working Directory". Then you will see in the console "setwd(...)". You can copy this to your code.
My method is the same as what #pluke stated.
This is how I set my working directory:
x <- r"(C:\myfolder\)"
setwd(x)
It seems that the output are the same when I echoed it.
I also tested other commands such as open, but the results from both are the same.
In traditional sh-style pattern matching, * matches zero or more characters in a component of the file name, so there is no difference between *, **, and ***, either on its own or as part of a larger pattern.
However, there are globbing syntaxes that assign a distinct meaning to **. Pattern matching implemented by the Z shell, for example, expands x/**/y to all file names beginning with x/ and ending in /y regardless of how many directories are in between, thus matching all of x/y, x/subdir/y, x/subdir1/subdir2/y, etc. This syntax was later implemented by bash, although only enabled when the globstar configuration option is set by the user.