1

Im trying to read a directory from a txt-file. In the txt file the syntax is like follows:

dirScript;"C:/User/Folder_1/R/Script-Folder 1/"
"empty line"

The information that I want is to get var equal to a string with the directory like this:

var <- "C:/User/Folder_1/R/Script-Folder 1/"
setwd(var)

And my R code looks like this:

tempString           <- str_c(str_extract_all(textInTxtFile, regex("(?<=;).*"), simplify=TRUE), sep="", collapse="") 

# Real variable, gsub expression deletes extra backslashes added by functions above
var                  <- gsub("[^A-Za-z0-9/.:-_; ]", "", tempString)
# Tempstring: "\""C:/User/Folder 1/R/Script-Folder 1/"\""
# Var: "C:/User/Folder 1/R/ScriptFolder 1/"

# *UPDATE* Or like this, seems to work, Safe enough?
var                  <- gsub('"', "", tempString)

So my "-" disappears and also the "_". I don't understand why, I think that my regex should search anything after a semicolon ";", is this wrong? Maybe I shouldn't use str_c ? (but does it make a difference?)

Also how do i fix my gsub to not take away "-" and (?) "_"? Or how fix my regex so that gsub isn't necessary, my regex or other functions there add some backslashes and citation-signs.

And lastly is there a way to check my directory after I've found my correct directory?

Is this a good idea to use?

dir.create(file.path(mainDir, subDir), showWarnings = FALSE)
setwd(file.path(mainDir, subDir))

from: Check existence of directory and create if doesn't exist

Extra question: Is there a way to easy find if the direction is written with backslashes (windows standard), if so I would just use gsub("[\]", "/", text). I was thinking of just searching for a number of backslashes and if its bigger than say 3 I use this, but it's not too safe. Aslo it will complain in the regex I think since R will interpret backslash as escape character.

So the "extra question" is, how find and fix this to a good path:

dirScript;"C:\User\Folder_1\R\Script-Folder 1\"
"empty line"
Community
  • 1
  • 1
Smrow
  • 89
  • 7
  • 1
    `:-_` is a range from `:` to `_`, if this is not intended, move the `-` to the end of the character class – Sebastian Proske Jul 08 '16 at 06:46
  • So are you trying to extract a directory path from a string? – Tim Biegeleisen Jul 08 '16 at 06:51
  • Well yes, but the directory path is a string, so more or less i just want to copy the string with the directory path from the txt-file... – Smrow Jul 08 '16 at 06:54
  • This regex `(C:\/.*?[^\s]\/)(\s|$)` works well in [Regex101](https://regex101.com/r/sS8fE4/1), but it didn't work in R for some reason. – Tim Biegeleisen Jul 08 '16 at 07:08
  • Maybe if I add some extra backslashes, I'll try it. Thanks! – Smrow Jul 08 '16 at 07:18
  • Sorry, in R you would use: `(C:/.*?[^\\s]/)(\\s|$)` – Tim Biegeleisen Jul 08 '16 at 07:19
  • Thanks, I tried it and don't get it to work. I also tried your first regex in Regex101 but it doesn't find anything with for example this string: `someStuff;"C:/User/Files and Stuff/R/Program-Copy/Program/temp"` – Smrow Jul 08 '16 at 07:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116765/discussion-between-smrow-and-tim-biegeleisen). – Smrow Jul 08 '16 at 07:31

1 Answers1

0

I fixed it. Thanks to the helped I received.

I think my main problem was in the gsub-function. It works with this now:

var                  <- gsub('"', "", tempString)

Also my regex-expression with the str_c and all might add some weird stuff that give me an extra-string, but thats deleted by the new gsub.

Also, I tried it with a path with backslashes "\" and it works.

I don't know if leave this question up, maybe not? Comment what you think.

Thanks for the help.

Smrow
  • 89
  • 7