# escaping the backslash inside \immediate\write18 with del command

I pasted the following line into the DOS PROMPT/DOS Command Line:

del D:\myTempFolder\myTempFile.txt


It worked well -- I was able to delete the file myTempFile.txt'' inside the foldermyTempFolder''. I did not use the forward slash since it has a different meaning (switches) when associated with the del command.

I was wondering if I can implement the preceding routine inside a LaTeX file, so i tried the following:

\documentclass{article}%
\newcommand*{\myFolderName}{myTempFolder}
\newcommand*{\myFileName}{myTempFile.txt}
\begin{document}
\immediate\write18{del D:\textbackslash\myFolderName\textbackslash\myFileName}%
\end{document}


The file myTempFile.txt'' was not deleted. May I know where my mistake is? I think the problem here is escaping the backslash inside \immediate\write18 with the del command.

Thank you for any help.

• by the way, i compiled using pdflatex with shell-escape. it has worked with my other \immediate\write18 lines, but not with the del command. Apr 10, 2020 at 16:21
• I answered but I noticed this is a duplicate of my answer that is listed in the "Related" list at the right: tex.stackexchange.com/a/525430/1090 Apr 10, 2020 at 16:28

you can use

\@backslashchar as in

\makeatletter
\immediate\write18{del D:\@backslashchar myFolderName\@backslashchar myFileName}%
\makeatother


or more simply avoid expanding the undefined tokens \myFolderName by using \string.

\immediate\write18{del D:\string\myFolderName\string\myFileName}%

• trying it now sir david... Apr 10, 2020 at 16:47
• Not really: the OP wants expansion of the two commands. Apr 10, 2020 at 17:02
• hi, the solution using \@backslashchar works. however, the solution using \string did not work for me. i dont know where my error is, if i missed a package or something. just the same, thank you sirs Apr 10, 2020 at 17:06
• @beethovengg14 in both forms here I assumed the folder and file are literally myFolderName and myFileName if you meant them to be macros that expanded to the name they need slight adjustment Apr 10, 2020 at 21:34
• @beethovengg14 if the file is d:\beethoven\thesis.tex you could use {del d:\string\beethoven\string\thesis.tex} but if you mean you have \def\MyFolder{beethoven}\def\myFile{thesis.tex} then you need {del d:\expandafter\string\myFolder\expandafter\string\myFile} Apr 11, 2020 at 10:10

I like the solution found here, in this case it will need to add this:

\makeatletter
\newcommand\dosystem{%
\@ifstar{\@tempswafalse\do@system}{\@tempswatrue\do@system}%
}
\edef\@hashmark{\string#}\edef\@lbrace{\string{}\edef\@rbrace{\string}}
\newcommand\do@system[1]{%
\begingroup
\let\\\@backslashchar
\let\%\@percentchar
\let\#\@hashmark
\let\{\@lbrace
\let\}\@rbrace
\if@tempswa\expandafter\immediate\fi
\write18{#1}%
\endgroup
}


and then something like

\dosystem{del D:\\myFolderName\\myFileName}