Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

d:\1.txt    
d:\2.txt    
d:\abcd.bmp
share|improve this question

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.

2 Answers 2

up vote 1 down vote accepted

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"
share|improve this answer

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

if exist filename del filename
share|improve this answer
    
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.