简体   繁体   中英

sed / Batch / Windows: Prevent changig Backslash to slash

I have a variable with a path, like this:

SET "somevar=D:\tree\path\nonsens\oink.txt"

And I have a file, where somethink like this is written

VAR=moresonsense

Now I want to replace the word morenonsense to D:\\tree\\path\\nonsens\\oink.txt . This should be the result

VAR=D:\tree\path\nonsens\oink.txt

For this, I am using the tool sed for windows. But using sed in windows gives me the following:

VAR=D:    ree/path/nonsens/oink.txt

The spaces between the colon and ree is a tab. I thought, I could fix it with the following line before calling sed:

SET "somevar=%somevar:\\=\\\\%"

But no, this line is not working. So I have some questions:

  1. Is there a possibility, to prevent sed from changing \\t to a tab and prevent changing two backslashed \\ to a slash /?

  2. Is there another easy way to replace a string with another string within a file with BATCH?

  3. Does someone has another idea how to resolve this problem?

You should not \\ -escape the \\ instances in the variable expansion ; use the following:

SET "somevar=%somevar:\=\\%"

I don't know whether that solves all your problems, but SET "somevar=%somevar:\\\\=\\\\\\\\%" definitely does not work as intended, because it'll only match two consecutive \\ chars in the input, resulting in a no-op with your input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM