Recursively Delete All Files While Maintaining Directory Structure
The following command gets each file in $path
and executes the delete method on each one.
Get-ChildItem –Path "C:\Backup" -Recurse -File | Remove-Item
In the end, the directory structure is left intact.
We can achieve the same result with Command Prompt by modifying “Delete All Files in a Directory Except Specified Ones” script we had seen earlier:
DEL . /S /Q
/S
– is used to perform the delete operation recursively in sub folders
/Q
– Quiet attribute is used to delete files with prompts
/F
– Force attribute is used to delete read-only files
This would recursively delete all files in the current directory while leaving the folders intact.
That’s it. Hope this little code snippet was helpful in someway to you.