<p>I want to escape a DOS filename so I can use it with sed. I have a DOS batch file something like this:</p>

<pre><code>set FILENAME=%~f1

sed 's/Some Pattern/%FILENAME%/' inputfile
</code></pre>

<p>(Note: <code>%~f1</code> - expands <code>%1</code> to a Fully qualified path name - <code>C:\utils\MyFile.txt</code>)</p>

<p>I found that the backslashes in <code>%FILENAME%</code> are just escaping the next letter.</p>

<p>How can I double them up so that they are escaped?</p>

<p>(I have cygwin installed so feel free to use any other *nix commands)</p>

<h1>Solution</h1>

<p>Combining Jeremy and Alexandru Nedelcu's suggestions, and using | for the delimiter in the sed command I have</p>

<pre><code>set FILENAME=%~f1
cygpath "s|Some Pattern|%FILENAME%|" &gt;sedcmd.tmp
sed -f sedcmd.tmp inputfile
del /q sedcmd.tmp
</code></pre>

This will work.  It's messy because in BAT files you can't use set var=`cmd` like you can in unix.
The fact that echo doesn't understand quotes is also messy, and could lead to trouble if
```
Some Pattern
```
contains shell meta characters.
```
```
set FILENAME=%~f1
echo s/Some Pattern/%FILENAME%/ | sed -e "s/\\/\\\\/g" >sedcmd.tmp
sed -f sedcmd.tmp inputfile
del /q sedcmd.tmp
```
```
[Edited]: I am suprised that it didn't work for you.  I just tested it, and it worked on my machine.  I am using sed from
http://sourceforge.net/projects/unxutils
and using cmd.exe to run those commands in a bat file.