Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 122375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:07:07+00:00 2026-05-11T04:07:07+00:00

I need to delete files of a certain type (.zip files, say) from a

  • 0

I need to delete files of a certain type (.zip files, say) from a folder, and all of its sub-folders, using the command line. Ideally I am looking for something that can be run as a .bat file in Windows.

I know there is a /S switch for the DEL command to look in sub-folders, but to add to the challenge I need to exclude files of a certain name (‘Backup.zip’ as an example).

Is there a way to delete files recursively but exclude files of a certain name. It will not be practical in my situation to explicitly list all the file names I want to delete, only the files of the matching type I don’t want to delete.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:07:08+00:00Added an answer on May 11, 2026 at 4:07 am

    A nice trick: make the files you want to exclude read-only!

    DEL /S will not delete read-only file.

    The following script does not do exactly what you want (see my remarks below) but shows you how read-only files can be used to avoid deletion.

    @ECHO OFF :: This example supposes your first parameter is :: the relative path to the folder to be deleted :: and the second is the relative path (from the :: the target folder) of the one to be excluded :: Notice that this will only work if the folders :: are in the working drive, if not, you ll :: have to specify full paths  IF '%1'=='' GOTO ERROR IF '%2'=='' GOTO ERROR IF NOT EXIST %1\NUL GOTO ERROR CD %1 IF NOT EXIST %2\NUL GOTO ERROR ECHO Starting up the deletion process ECHO. * Setting attributes attrib %1\*.mp3 -r -s -h > NUL attrib %2\*.mp3 +r > NUL ECHO. * Deleting files del /s %1\*.mp3 ECHO. * Reseting attributes attrib %2\*.mp3 -r > NUL ECHO. ECHO Operation completed! ECHO. GOTO END  :ERROR ECHO Parameters: ECHO. Param1 -> target folder ECHO. Param2 -> folder to be ignored ECHO. GOTO END  :END 

    Note: you can adapt this script in order to ignore not just a sub-folder but all files of given type:

    attrib /S *.xxx +r > NUL 

    will in effect help you to exclude all ‘xxx’ files of the current directory and all sub-directories (hence the /S option).


    Note: the ‘> NUL‘ part is a redirection often used to hide standard output, instead of displaying it on screen.
    It can be dangerous if used too often (in a large loop with different paths involved, for instance) since it is a device, and like all devices (AUX, COMn, LPTn, NUL and PRN):

    • opening a device will claim one file handle. However, unlike files, devices will never be closed until reboot.
    • each device exists in every directory on every drive, so if you used redirection to NUL in, say, C:\ and after that you use it again in C:\TEMP, you’ll lose another file handle.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to recursively delete a folder and its children. Is there
Java is the key here. I need to be able to delete files but
I need to delete a temporary file from my C++ windows application (developed in
I need to delete some Unicode symbols from the string 'بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ'
I am using Qt Dialogs in one of my application. I need to hide/delete
I need to delete my input file securely once I have finished with it,
Whoops, I need some info from a file I deleted, a while ago. In
I need to delete a regsitry key. It has a deny ACL on Set
The array has lots of data and I need to delete two elements. Below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.