batch script to delete certain files based upon one file (format vary sometimes) exist in all sub folders

Batch script to delete certain files based upon one file (format vary sometimes) exist in all sub folders

To create a batch script that deletes certain files based on the existence of a specific file (with varying formats) in all subfolders, you can use a combination of for loops, conditional checks, and deletion commands (del). Here's a script that demonstrates this process:

Batch Script Explanation

The following script assumes you want to delete files (*.txt in this example) if a specific trigger file (trigger.txt in this example) exists in any subfolder. It recursively searches through all subfolders starting from the current directory.

@echo off setlocal enabledelayedexpansion rem Define the trigger file name (can vary) set "trigger_file=trigger.txt" rem Loop through all directories and subdirectories for /r %%d in (.) do ( rem Check if the trigger file exists in the current directory if exist "%%d\%trigger_file%" ( rem Display a message indicating the trigger file exists echo Trigger file found in "%%d" rem Loop through all files in the current directory for %%f in ("%%d\*.txt") do ( rem Delete the files matching the specified pattern (*.txt) echo Deleting "%%f" del "%%f" ) ) ) echo Done. pause 

Script Breakdown:

  1. @echo off: Disables echoing of commands to the console, making the output cleaner.
  2. setlocal enabledelayedexpansion: Enables delayed expansion to handle variables within loops.
  3. set "trigger_file=trigger.txt": Defines the name of the trigger file that indicates which files to delete.
  4. for /r %%d in (.) do ...: Recursively iterates through all subdirectories (/r) starting from the current directory (.).
  5. if exist "%%d\%trigger_file%": Checks if the trigger file exists in the current directory (%%d).
  6. for %%f in ("%%d\*.txt") do ...: Iterates through all .txt files (*.txt) in the current directory (%%d).
  7. del "%%f": Deletes each .txt file found (%%f).

Notes:

  • Adjust trigger_file to match the actual filename pattern or format that might vary.
  • This script assumes you want to delete all .txt files found in directories containing trigger.txt. Modify *.txt to match the file pattern you want to delete.
  • Ensure you test this script in a safe environment before running it in a production setting, especially when using commands that delete files.

This batch script provides a basic framework to accomplish your task. Customize it further based on specific requirements or additional conditions for file deletion.

Examples

  1. Search Query: Batch script to delete files based on a specific file extension in all subfolders

    • Description: Deleting files with a specific file extension (.txt, .log, etc.) from all subdirectories using a batch script.
    • Code Example:
      @echo off for /r %%i in (*.txt) do ( del "%%i" echo Deleted file: "%%i" ) echo All .txt files deleted. pause 
  2. Search Query: Batch script to delete files if a certain file exists in all subdirectories

    • Description: Deleting files if a specific marker file exists in all subfolders using a batch script.
    • Code Example:
      @echo off for /r %%i in (*) do ( if exist "%%~dpi\marker.txt" ( del "%%i" echo Deleted file: "%%i" ) ) echo Files deleted where marker.txt exists. pause 
  3. Search Query: Batch script to delete files based on filename patterns in subfolders

    • Description: Deleting files based on specific filename patterns or wildcard matches in all subdirectories using a batch script.
    • Code Example:
      @echo off for /r %%i in (*pattern*.*) do ( del "%%i" echo Deleted file: "%%i" ) echo Files matching pattern deleted. pause 
  4. Search Query: Batch script to delete files older than a certain date in subfolders

    • Description: Deleting files older than a specified date from all subdirectories using a batch script.
    • Code Example:
      @echo off forfiles /s /m *.* /d -30 /c "cmd /c del @path" echo Files older than 30 days deleted. pause 
  5. Search Query: Batch script to delete empty directories after deleting files

    • Description: Deleting empty directories recursively after deleting files within them using a batch script.
    • Code Example:
      @echo off for /f "delims=" %%d in ('dir /ad /s /b ^| sort /r') do ( rd "%%d" echo Deleted directory: "%%d" ) echo Empty directories deleted. pause 
  6. Search Query: Batch script to delete files of a specific size in all folders

    • Description: Deleting files larger than a certain size or within a specific size range from all directories using a batch script.
    • Code Example:
      @echo off for /r %%i in (*.*) do ( for %%F in ("%%i") do ( set size=%%~zF if %%size%% GTR 1000000 ( del "%%i" echo Deleted file: "%%i" ) ) ) echo Files larger than 1MB deleted. pause 
  7. Search Query: Batch script to delete files based on file attributes in subdirectories

    • Description: Deleting files based on specific file attributes (read-only, hidden, system) from all subdirectories using a batch script.
    • Code Example:
      @echo off for /r %%i in (*) do ( attrib "%%i" | find "H" > nul && del "%%i" attrib "%%i" | find "S" > nul && del "%%i" attrib "%%i" | find "R" > nul && del "%%i" ) echo Files with specific attributes deleted. pause 
  8. Search Query: Batch script to delete specific files based on a list of filenames in subfolders

    • Description: Deleting files matching specific filenames listed in a text file across all subdirectories using a batch script.
    • Code Example:
      @echo off set "listfile=list.txt" for /f "usebackq delims=" %%i in ("%listfile%") do ( for /r %%j in ("%%i") do ( del "%%j" echo Deleted file: "%%j" ) ) echo Files listed in list.txt deleted. pause 
  9. Search Query: Batch script to delete files based on file creation date in all subdirectories

    • Description: Deleting files created before or after a specified date from all subdirectories using a batch script.
    • Code Example:
      @echo off set "cutoff_date=2023-01-01" for /r %%i in (*) do ( for /f "tokens=1-3 delims=/-" %%a in ("%%~ti") do ( set "file_date=%%c-%%a-%%b" if "%file_date%" LSS "%cutoff_date%" ( del "%%i" echo Deleted file: "%%i" ) ) ) echo Files created before %cutoff_date% deleted. pause 
  10. Search Query: Batch script to delete files based on file size and type in subdirectories

    • Description: Deleting files of a specific type (.jpg, .pdf) and larger than a certain size from all subdirectories using a batch script.
    • Code Example:
      @echo off set "file_type=.pdf" set "min_size=1000000" for /r %%i in (*%file_type%) do ( for %%F in ("%%i") do ( set size=%%~zF if %%size%% GTR %min_size% ( del "%%i" echo Deleted file: "%%i" ) ) ) echo %file_type% files larger than %min_size% bytes deleted. pause 

More Tags

grand-central-dispatch cqrs sweetalert2 bootstrap-daterangepicker npm-link nested-lists ed srand spring-data-redis moment-timezone

More Programming Questions

More Cat Calculators

More Trees & Forestry Calculators

More Biology Calculators

More Statistics Calculators