0

When I try to copy files in R, the dir names with space come across to me. For example, I have a string variable filname with its value c:/Datalogger Folder/Bdev.txt and I want to copy this file to A.txt. I know that I should pass filname into system function:

   system(paste("cp",filname,"A.txt",sep=" "))

While for the reason that there is a space in c:/Datalogger Folder/Bdev.txt, the R complained that "c:/Datalogger no such file or directory". Please guide me how to resolve this problem. Thanks for any advise.

5
  • You need to escape the string with a \ for cp to understand it: c:/Datalogger\ Folder/Bdev.txt Commented Sep 10, 2018 at 23:36
  • how to process variable filname? I cannot add \ into this variable. Commented Sep 10, 2018 at 23:47
  • I'd just delete ` Folder` and keep only Datalogger. I think having spaces in folders or filenames is not good practice and will only produce trouble Commented Sep 10, 2018 at 23:49
  • @Tung, Actually the files to be processed are transferred to me, I cannot always manually rename directory as I need. Commented Sep 10, 2018 at 23:55
  • You can write a simple shell script to do folder name and file name cleanup before doing anything in R unix.stackexchange.com/questions/223182/… Commented Sep 11, 2018 at 0:06

1 Answer 1

1

You can add escape character \ before spaces in filname using gsub(). For example:

filname <- gsub(" ", "\ ", filname)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.