0

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.

David Carlisle
  • 757,742
  • 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. – beethovengg14 Apr 10 '20 at 16:21
  • 1
    I answered but I noticed this is a duplicate of my answer that is listed in the "Related" list at the right: https://tex.stackexchange.com/a/525430/1090 – David Carlisle Apr 10 '20 at 16:28

2 Answers2

2

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}%
David Carlisle
  • 757,742
  • trying it now sir david... – beethovengg14 Apr 10 '20 at 16:47
  • 1
    Not really: the OP wants expansion of the two commands. – egreg Apr 10 '20 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 – beethovengg14 Apr 10 '20 at 17:06
  • @egreg oh I suppose so, I thought it was a meta example and the folder was literally myFolderName (in the example) – David Carlisle Apr 10 '20 at 21:32
  • 1
    @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 – David Carlisle Apr 10 '20 at 21:34
  • hi sir david, did you mean

    \immediate\write18{del D:\string\myTempFolder\string\myTempFile}

    instead of

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

    – beethovengg14 Apr 11 '20 at 09:39
  • 1
    @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} – David Carlisle Apr 11 '20 at 10:10
  • i understand sir. i will try it too. thank you sir for being very helpful. in all my questions here in stackexchange, it was not without trying -- i would exhaust all i know about latex, read again and again the package word for word and in between the lines, read the latex manuals, exhaust trials-and-errors, but at some point i would really stumble, and i know its already time to seek help. thank you again. beethovengg14 of the philippines – beethovengg14 Apr 11 '20 at 12:54
1

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}
alexis
  • 481