2

Trying to write a batch file that will loop through all the files in a directory and if the first part of the file is not found in the text file then delete.

In the images directory the files are named like:

1_a.jpg
1_b.jpg
1_c.jpg
2_a.jpg
3_a.jpg
3_b.jpg

in the file the list is

2
3

So at this point I would like to delete

1_a.jpg
1_b.jpg
1_c.jpg

Started by using:

@echo off
wget -N http://www.domain.com/imagelist.txt
FOR /R C:\image-directory\ %%G IN (*.jpg) DO ??????? %%G

And im stuck.

Bali C
  • 30,582
  • 35
  • 123
  • 152
Kevin
  • 57
  • 1
  • 5

1 Answers1

4
@echo off
setlocal
set "folder=c:\somePath"
set "excludeFile=c:\somePath2\someFile.txt"
for /f "eol=: delims=" %%F in ('dir /b /a-d "%folder%" ^| findstr /vibg:"%excludeFile%"') do del "%folder%\%%F"
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • is there a way to send you a specific question to see if you can answer, or do I have to start a whole new thread? – Kevin Apr 27 '12 at 21:18
  • if you experience "file not found" (on windows 10), remove the " (quote) from this part `"%folder%\%%F"` – Developer Marius Žilėnas Jan 13 '17 at 21:00
  • @Willmore - I edited my answer in a different way to address the issue. It is better to move the quote before the variable name in the SET statements, and then add quotes around the paths in the DIR and FINDSTR commands. But thanks for pointing out the issue (it is a general issue with batch, not just Win 10). – dbenham Jan 13 '17 at 22:24