This question already has an answer here:

This code works fine. it deletes files from a file list:

For /f %%a in (file.lst) do del %%a

But why is the below one not working? It deletes the .lst file itself:

if exist file.lst (   
  set filelist="file.lst"    
) else (    
  set filelist="default.lst"    
for /f %%b in (%filelist%) do del %%b    

Also this is not working too:

for /f %%b in ("%filelist%") do del %%b        

file.lst and default.lst contain something like this:


marked as duplicate by Ƭᴇcʜιᴇ007, mpy, Carl B, Tog, Simon Sheehan Sep 16 '13 at 16:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


for /f interprets content of brackets like this:

  • file - the file is processed line by line.
  • "text" - only the text is processed.
  • 'command' - output of the command is processed line by line.

This is how you should edit your batch:

if exist file.lst (
    set "filelist=file.lst"
) else (
    set "filelist=default.lst"
for /f %%b in (%filelist%) do del "%%b"

What's the need for for while deleting the list file? Just use something like:

if exist filename del filename
  • BTW, your first if-else code block works fine. The second for loop will also work, although if you have set filelist="file.lst" then "%filelist%" will cause the quotes to be doubled. – Karan Jul 4 '13 at 9:22
  • It doesnt delete list file when i mention the list file name. but it deletes the list file when i put the list file name in a variable. – Meisam Moradi Jul 4 '13 at 14:41
  • Thanks Karan, i removed the quotes and it works: set filelist=file.lst – Meisam Moradi Jul 4 '13 at 14:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.