Batch command to delete everything (sub folders and files) from a folder except one file
<p>First of all, similar questions have been answered in past but not exactly the one I have. In some other solutions, hiding folders/files and changing attributes were suggested and I do not want this, unless there is no simpler way available. Also, I have tried the solution suggested here (and couple of others): <a href="https://superuser.com/questions/112136/ms-dos-command-to-delete-all-files-except-one">MS-DOS command to delete all files except one</a>.</p>

<p>But did not work for my need due to two reasons:</p>

<ol>
<li>This also deleted the file, I did not want to.</li>
<li>This did not delete the sub-folders but only the files.</li>
</ol>

<p>So, I have folder <code>c:/users/data</code> and in there, I have 5 folders and 6 files I want to delete everything except one which is <code>web.config</code> and to do that I run the batch file like this:</p>

<pre><code>@echo off
for %%j in (C:\Users\data\*) do if not %%j == Web.config del %%j
pause
</code></pre>

<p>But when I run the batch file, this deletes all the files including <code>web.config</code> and also, does not delete any of the sub-folders and if I use the <code>/d</code> switch then it only deletes folders not files. How can I delete both files and folders?</p>