bbk
bbk

Reputation: 229

RF: Replacing Escape Chars

I am trying to replace \ with / but due to escape chars Robot Framework is not doing what I was expecting. I am getting the following result:

${location1} =    Replace String    C:\Users\bnduch\Downloads    \    /

Result:

/C/:/U/s/e/r/s/b/n/d/u/c/h/D/o/w/n/l/o/a/d/s/

How to avoid escape chars here?

Edit: To get other alternatives i am adding source of my directory string.

In Order to work with downloaded files in IE I have to get default downloaded location. (reason being we cant modify IE downloaded location)

def get_ie_download_path():
    reg = Reg()
    path = 'HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders'
    downloadregconfig = reg.read_value(path, '{374DE290-123F-4565-9164-39C4925E467B}')
    downloadlocation = downloadregconfig["data"]
    return downloadlocation

Using above function i am getting IE Default download location i.e. C:\Users\bnduch\Downloads, But cant use it since it contains '\'. so i am forced to update '\' to '/'. So that i can REMOVE/Delete the file. This is the code i am using

${DownloadDir}=  get_ie_download_path
${DownloadDir}=  Catenate  ${DownloadDir}/Daily Reads Status Report.xlsx
Remove File  ${DownloadDir}

Please suggest

Upvotes: 0

Views: 3925

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

You have a couple problems, all related to the fact that the backslash is an escape character.

First, you need to escape the backslashes in C:\Users\bnduch\Downloads, ie: C:\\Users\\bnduch\\Downloads

Next, you need to do the same with the other two arguments. To get a single backslash use two; to get two, use four: \ needs to be \\ and \\ needs to be \\\\.

Your complete statement should look like this:

${location1} =    Replace String    C:\\Users\\bnduch\\Downloads    \\    \\\\

With that, ${location1} will be set to the 28-byte string C:\\Users\\bnduch\\Downloads

All of that being said, this looks like a code smell. It's very rare that you need to replace backslashes with double backslashes in a file path. If you could show us why you think you need to do this, we can possibly offer better solutions.

Upvotes: 1

Related Questions