none
How do I do a recursive delete of a file in a .bat file but exclude a directory RRS feed

  • Question

  • This script works perfectly, however it deletes too much, How do it get it to exclude a directory when deleting?

    FOR /R %%a IN (Setup File.msi) DO DEL "%%~dpaSetup File.msi"

    The "Setup File.msi" could be in the root of where I'm running the batch file from as well as sub folders can be any name and any number of levels deep, hence the need for /R . I did find other possibilities to help answer this however they were deleting a file in 1 directory down and that's it, not what I need.

    I'm basically trying to centralise messy install sets, I have Copied/Moved the latest files that I need to one folder but would like to delete any other traces of them else where apart from where I have copied them to. I don't want to just delete any file though which is why I need to name a file to delete, but exclude the "Master" directory

    p.s. I have only been learning this for 2 weeks so please explain clearly how your script works so I can learn

    • Edited by Adam Lacey Tuesday, August 5, 2014 12:04 PM Just so more gramer
    Tuesday, August 5, 2014 10:55 AM

Answers

  • Here is the batch solution.

    FOR /F "delims=*" %%G IN ('dir /b /s "C:\Admin\testfolder" ^| findstr /v "\Master" ^| findstr "Setup File.msi"') DO del "%%G"

    Above is adapted from LomM's response and playing with strings with findstring.

    Ah, that's ugly. Here is the PowerShell line to do the same. Remove the -WhatIf to have it actually delete files.

    Get-ChildItem -Recurse | Where-Object {$_.Name -eq "Setup File.msi" -and $_.DirectoryName -ne "C:\Admin\testfolder\Master"
    } | Remove-Item -WhatIf



    • Marked as answer by Adam Lacey Wednesday, August 6, 2014 4:17 PM
    Tuesday, August 5, 2014 9:53 PM

All replies

  • Hello,

    I'm not 100% sure of what are you trying to do so correct me if I'm wrong but:

    1. you want to find setup.msi (or similar) from the group of folders and subfolders
    2. you want to delete those files but not the other files that are inside the same location
     @echo off
     ::type the folder where you want to find the setup.msi -file like c:\admin\testfolder
     SET WhereTolook=C:\Admin\testfolder
     ::then we will search the folder and all it's sub folders and pipe | that output to "FIND" command that will single out only the setup.msi -files. after that the loop will delete all those lines
     FOR /F "delims=*" %%G IN ('dir "%WhereTolook%" /B /S ^| FIND "setup.msi"') DO del "%%G" /q


    • Proposed as answer by Shaun Vermaak Tuesday, August 5, 2014 12:00 PM
    • Unproposed as answer by Shaun Vermaak Tuesday, August 5, 2014 12:03 PM
    Tuesday, August 5, 2014 11:36 AM
  • del "Setup File.msi" /s
    Tuesday, August 5, 2014 12:03 PM
  • Thanks but "C:\Admin\testfolder" Would be the folder I'm looking in for e.g, however the "Master" Folder I don't want to delete anything from will also be in "testfolder". As I will be running this script on hundreds of sites, all different structures I've figured out the most efficient/best solution would be to use a script to create the "master" folder, including it's files/msi's/exe's, within this "testfolder" before any deleting occurs. Worst comes to worst I could look at scripting to move the "Master" folder out, run my original line on the "testfolder" folder and then move the "Master" it back in to that location (where it needs to be by the end) but I would like to avoid this due to it being inefficient and having to create folders on other peoples servers to achieve this if possible, so trying to contain it all in one location.
    Tuesday, August 5, 2014 12:22 PM
  • del "Setup File.msi" /s

    Yes this is a really simple way of deleting "Setup File.msi" in sub folders but how does this exclude a folder where I don't want to delete things from where they may also exist?

    Tuesday, August 5, 2014 1:37 PM
  • I found this which doesn't search unlimited number if sub folders does it? And I need it to. But allows excluding a folder
    @echo off
    set root=d:/document and settings 
    
    for /F "usebackq" %%i in (`dir /b "%root%"`) do ( set flag=0 
    
    REM here you must make a block for every folder you dont want to change
    REM START OF EXCLUSION LIST 
    if /i "%%i"=="Appdata" (set flag=1) 
    if /i "%%i"=="default" (set flag=1) 
    if /i "%%i"=="public" (set flag=1)
    REM END OF EXCLUSION LIST
    
    call :delete %%i ) 
    
    pause 
    exit /b 0 
    
    :delete 
    if "%flag%"=="0" ( echo Processing: %1 
    REM here you can delete whatever you want. 
    REM the current folder is: %root%\%1 ) 
    
    goto :eof

    Tuesday, August 5, 2014 6:47 PM
  • Here is the batch solution.

    FOR /F "delims=*" %%G IN ('dir /b /s "C:\Admin\testfolder" ^| findstr /v "\Master" ^| findstr "Setup File.msi"') DO del "%%G"

    Above is adapted from LomM's response and playing with strings with findstring.

    Ah, that's ugly. Here is the PowerShell line to do the same. Remove the -WhatIf to have it actually delete files.

    Get-ChildItem -Recurse | Where-Object {$_.Name -eq "Setup File.msi" -and $_.DirectoryName -ne "C:\Admin\testfolder\Master"
    } | Remove-Item -WhatIf



    • Marked as answer by Adam Lacey Wednesday, August 6, 2014 4:17 PM
    Tuesday, August 5, 2014 9:53 PM
  • Hello,

    Maybe you already got your answer from Mark but I'm still not sure about the folder structure you are working with. So I still made a few modifications based on this model:

    • you have parent folder (in this example "C:\Admin\testfolder") that contains many folders
    • C:\admin\testfolder
    • C:\admin\testfolder\Folder_I_want_to_delete1
    • C:\admin\testfolder\Folder_I_want_to_delete2
    • C:\admin\testfolder\Folder_I_want_to_delete2\Subfolder_I_want_to_delete1
    • C:\admin\testfolder\Folder_I_want_to_delete3
    • C:\admin\testfolder\MASTER
    • etc.
    • you have some way to differentiate the "Master folder" (in this example its located "C:\admin\testfolder\MASTER)
    • you don't want to delete single file inside of all of the folders BUT you want to delete all the files and folders under the parent folder EXCEPT the "Master folder".

    Is this the scenario you are working with if so I would use CMD script like this (and yes CMD script is dirty;)

     @echo off
     ::type the root folder where the "find and delete folder" will run
     SET WhereTolook=C:\Admin\testfolder
     ::What to exclude?
     SET ExcludeWhat=%WhereTolook%\MASTER
     ::then we will search the folder and all its subfolders and pipe | that output to "FINDSTR" command that find everything except the folder you want to exclude then we RD(remove folders) and sub folders
     FOR /F "delims=*" %%G IN ('dir "%WhereTolook%" /B /S /AD ^| findstr /v "%ExcludeWhat%"') DO RD "%%G" /Q /S
     ::If there are any files in the root directory you want to remove
     del %WhereTolook% /q


    • Edited by LomM Wednesday, August 6, 2014 10:51 AM
    Wednesday, August 6, 2014 10:49 AM