How can I delete "D:\Folder\" without deleting "D:\Folder\file.txt"?

Solution 1:

Just redirect the file as input stream to lock it so it can not be deleted

<"d:\folder\file.txt" rmdir /s /q "d:\folder"

Solution 2:

You can do it in Windows PowerShell with the following command:

Get-ChildItem "D:\Folder\" | Where-Object { $_.FullName -ne "D:\Folder\file.txt" } | Remove-Item -Recurse -Force