1

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
0

2 Answers 2

1

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"
0

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

if exist filename del filename
3
  • 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, 2013 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. Jul 4, 2013 at 14:41
  • Thanks Karan, i removed the quotes and it works: set filelist=file.lst Jul 4, 2013 at 14:42

Not the answer you're looking for? Browse other questions tagged .