Solved delete all files & directories except three files


bonky

Member
Local time
11:32 PM
Posts
24
OS
win10
Hello all.... i have a request for help....

Im trying to delete all files & directories except three files, in my search in google I came across this

(for /f "tokens=*" %f in ('dir /a:-d /b 2^>nul ^| findstr /v /b /e /c:"test1.txt"') do (del /f "%f")) & (for /f "tokens=*" %f in ('dir /a:-d /b 2^>nul ^| findstr /v /b /e /c:"test1.txt"') do (rmdir /q /s "%f"))

it works... BUT.... I have to be already in that directory (CD\ etc etc) is it poss to ajust this script to delete no matter where i am for the "%userprofile%\downloads\test" ?

also to add another two files to this not to be touched (I know I can attrib +r, but would like to have one line command if poss)
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
this works for protecting three files....

for %i in (*) do if not "%~i" == "test.txt" if not "%~i" == "test1.txt" if not "%~i" == "test2.txt" del "%~i"

as above post I have to be already in that directory (CD\ etc etc) is it poss to ajust this script to delete no matter where i am for the "%userprofile%\downloads\test" ?

this command doesnt remove directories in the "%userprofile%\downloads\test" (which I want)
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
Hello!

Ether add pushd your\dir\path && before the script, or add your\dir\path\ in dir argument command and in del filename.

pushd changes dir, and popd come back to previous dir.

Full example:

pushd your\dir\path && for %i in (*) do if not "%~i" == "test.txt" if not "%~i" == "test1.txt" if not "%~i" == "test2.txt" del "%~i" && popd
 

My Computer

System One

  • OS
    Windows 11
oh wow!! I didnt know that command!! thank you...

how would I include a RMDIR command in that ? (delete all folders)
 
Last edited:

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
You probably didn't want an answer in PowerShell, but here's an example of why it's much easier to write this in PS.

Code:
$MyFolderPath = "\path\myfolder"

# Recursively remove files except for an exclusion list
# Separate the excluded filenames by a comma ","
#
Get-ChildItem -Path $MyFolderPath -File -Recurse -Exclude *BitLocker*,*README*,*WhatsMy* | Remove-Item

# Recursively remove all empty folders
Get-ChildItem -Path $MyFolderPath -Directory -Recurse | Where-Object { $_.GetFileSystemInfos().Count -eq 0 } | Remove-Item
 

My Computer

System One

  • OS
    Windows 7
I can run PS from CMD line,

can you do it on all folders... rather than 0k size ?
 

My Computer

System One

  • OS
    win10
    Computer type
    PC/Desktop
The first Get-ChildItem returns every single file below a folder path, except for the excluded list.

The second Get-ChildItem returns a list of subfolders which are empty (no files). We don't want to remove the folders that still contain excluded files, because that would delete the files we wanted to keep.

You can't combine the two actions in one command, since the exclude files doesn't protect against a file's own folder from getting removed. They just didn't design the command that way.
 

My Computer

System One

  • OS
    Windows 7
Just for the sake of picking nits, the removal of empty folders won't work if a folder contains only empty folders. For example, if you have 'My Folder\Something', and Something contains A through D folders that are empty, those A-D folders will get deleted, but Something will not.

You need to do it with a recursive function or something like this...

Powershell:
while ($true) {
    $empty = Get-ChildItem -Path 'D:\A Folder' -Directory -Recurse | Where-Object { -not (Get-ChildItem -Path $_.FullName) }
    if (-not $empty) { break }
    $empty | Remove-Item
}
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
I think you can do a depth-first search with something like:
Code:
Get-ChildItem "\path\folder" -Directory -Recurse).FullName | Sort-Object -Descending | Where-Object { (Get-Item $_).GetFileSystemInfos().Count-eq 0 } | ForEach-Object { Remove-Item $_ }
 

My Computer

System One

  • OS
    Windows 7
I had to move the FullName bit to the right a little, but yep that works.

Powershell:
Get-ChildItem -Path 'D:\Ice Cream' -Directory -Recurse | Sort-Object -Property 'FullName' -Descending | Where-Object { (Get-Item $_).GetFileSystemInfos().Count-eq 0 } | ForEach-Object { Remove-Item $_ }

Edit: I see what you meant... missing a ( at the beginning though.
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2
    Computer type
    PC/Desktop
    Manufacturer/Model
    Intel NUC12WSHi7
    CPU
    12th Gen Core i7-1260P
    Motherboard
    NUC12WSBi7
    Memory
    64 GB Micron PC4-25600
    Graphics Card(s)
    Intel Iris Xe Graphics
    Sound Card
    on-board Realtek HD Audio
    Monitor(s) Displays
    Dell U3219Q
    Screen Resolution
    3840 x 2160
    Hard Drives
    Samsung SSD 990 PRO 1TB
    Crucial MX500 2 TB
    Antivirus
    Microsoft Defender
Updated script:
Code:
$MyFolderPath = "\path\myfolder"

# Recursively remove files except for an exclusion list
# Separate the excluded filenames by a comma ","
#
Get-ChildItem -Path $MyFolderPath -File -Recurse -Exclude *BitLocker*,*README*,*WhatsMy* | Remove-Item

# Recursively remove all empty folders
(Get-ChildItem -Path $MyFolderPath -Directory -Recurse).FullName | Sort-Object -Descending | Where-Object { (Get-Item $_).GetFileSystemInfos().Count-eq 0 } | ForEach-Object { Remove-Item $_ }
 

My Computer

System One

  • OS
    Windows 7

Latest Support Threads

Back
Top Bottom