tces2005 發表於 2020-7-28 15:43

如何用批次檔或命令刪除磁碟機中的目錄及檔案

本帖最後由 tces2005 於 2020-7-28 22:25 編輯

狀況描述:如下圖:
在一個磯碟機有許多的目錄及檔案,但只要保留子目錄 123 及檔案 BOOTICEx86.exe,其它的均要刪除,請問一下各位先進,
有沒有使用批次檔或命令的方式來達到這個目的。謝謝。
https://i.imgur.com/aQF6yXM.png


maodou 發表於 2020-7-28 19:06

本帖最後由 maodou 於 2020-7-28 22:11 編輯

用PowerShell執行 (注意權限)Get-ChildItem -Path G:\ | Where-Object { $_.Mode -Match 'd' -and $_.Name -NotMatch '123' } | Remove-Item -Recurse -Force
Get-ChildItem -Path G:\ | Where-Object { $_.Mode -NotMatch 'd' -and $_.Name -NotMatch 'BOOTICEx86.exe' } | Remove-Item -Force

tces2005 發表於 2020-7-28 19:51

本帖最後由 tces2005 於 2020-7-28 19:59 編輯

maodou 發表於 2020-7-28 19:06
在命令提示字元內執行, 或是存成cmd檔再執行 (注意權限)
謝謝您的說明,不過我用上面的指令無法執行成功,改用下面的才行,多加上 -Recurse,在 PowerShell 及命令提示字元之下都可以了!Get-ChildItem g:\ -Recurse -Exclude BOOTICEx86.exe,123 | Remove-Item -Recurse -Force
頁: [1]
查看完整版本: 如何用批次檔或命令刪除磁碟機中的目錄及檔案