1

I have one directory with around 100 subdirectories, which include images. I then have a .txt list of images I need to delete from these folders. Since there are a few thousands, I'm looking for a way to batch delete these using bat file. But the problem is that my files are in subfolders and subfolders and also filenames include spaces.

Example:

MainFolder/Subfolder One/Image Sunshine.jpg<br> MainFolder/Subfolder One/Image Cloudy.jpg 

I've tried multiple options of del, also by putting all paths in txt file inside double quotes. But nothing deletes them. Any ideas how to delete only the selected ones from all subfolders? Renaming or deleting spaces is not an option, since I would have to reupload the remaining images back in the same form as current.

7
  • 3
    We need to see an example of your .txt list content together with the multiple batch file attempts in order to help you to fix one or other of those attempts! Commented Aug 9, 2018 at 13:37
  • del "MainFolder/Subfolder One/Image Sunshine.jpg" Commented Aug 9, 2018 at 13:37
  • 4
    @mamatata, please read: How to create a Minimal, Complete, and Verifiable example and then come back and update your question. Commented Aug 9, 2018 at 13:49
  • 2
    This question is not about programming. Commented Aug 9, 2018 at 13:56
  • 1
    Welcome to Stack Overflow! Unfortunately questions like these are off-topic for Stack Overflow, and therefore should be asked here: SuperUser. Commented Aug 9, 2018 at 14:01

1 Answer 1

0

An easy way to do this is by using an FOR statement to read each line of the text document as a string.

For the removal of the files we can use the DEL command along with the /s switch to search all sub-directories.

  • /P - Prompts for confirmation before deleting each file.
  • /F - Force deleting of read-only files.
  • /S - Delete specified files from all subdirectories.
  • /Q - Quiet mode, do not ask if ok to delete on global wildcard
  • /A - Selects files to delete based on attributes

The following script will remove all file.extension's from the list.txt file from all sub-directories in the directory tree including the execution directory. This will also remove them if they have spaces in the name as "%%G" is quoted.

Batch File:

for /f "delims== tokens=1,2" %%G in (list.txt) do (del /s /q /f "%%G") 

Command Prompt: (Be sure to CD to the main directory!)

for /f "delims== tokens=1,2" %G in (list.txt) do (del /s /q /f "%G") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.