英文:
Please, I need a batch to delete all folders, except specific folders, and specific sub-folders
问题
我有一个带有多个分区的SSD。
我需要在J:\
上工作,我需要删除J:\
内的所有内容,除了以下文件夹:
J:\System Volume Information
J:$RECYCLE.BIN
J:\New
J:\Old Stuff
以及以下子文件夹:
J:\Papers\Folder
J:\Room\Main Documents
J:\Year\Origin\Part
J:\Year\Origin\Part Two
阅读了很多后,我写了以下代码:
@ECHO OFF
SETLOCAL
SET "sourcedir=J:\"
SET "keepdir=System Volume Information"
SET "keepdir=$RECYCLE.BIN"
SET "keepdir=New"
SET "keepdir=Old Stuff"
SET "keepdir=Papers\Folder"
SET "keepdir=Room\Main Documents"
SET "keepdir=Year\Origin\Part"
SET "keepdir=Year\Origin\Part Two"
FOR /d %%a IN ("%sourcedir%\*") DO IF /i NOT "%%~nxa"=="%keepdir%" RD /S /Q "%%a"
GOTO :EOF
不幸的是,上面的代码删除了J:\
内的所有内容。
英文:
I have a SSD with several partitions.
I need to work at J:\
and I need to delete everything inside J:\
, except the following folders:
J:\System Volume Information
J:$RECYCLE.BIN
J:\New
J:\Old Stuff
And the following sub-folders:
J:\Papers\Folder
J:\Room\Main Documents
J:\Year\Origin\Part
J:\Year\Origin\Part Two
After reading a lot, I wrote:
@ECHO OFF
SETLOCAL
SET "sourcedir=J:\"
SET "keepdir=System Volume Information"
SET "keepdir=$RECYCLE.BIN"
SET "keepdir=New"
SET "keepdir=Old Stuff"
SET "keepdir=Papers\Folder"
SET "keepdir=Room\Main Documents"
SET "keepdir=Year\Origin\Part"
SET "keepdir=Year\Origin\Part Two"
FOR /d %%a IN ("%sourcedir%\*") DO IF /i NOT "%%~nxa"=="%keepdir%" RD /S /Q "%%a"
GOTO :EOF
Unfortunately, the code above deletes everything inside J:\
.
答案1
得分: 1
一种简单的解决方案是使用 Windows 的 robocopy 工具:
md J:\empty
robocopy /mir J:\empty J:\ /xd keep /xd keep2
这将将一个原本为空的目录镜像到目标目录,从而删除所有内容。除了在排除项(/xd 路径)中列出的路径。
英文:
One simple solution would be to use robocopy which is part of Windows:
md J:\empty
robocopy /mir J:\empty J:\ /xd keep /xd keep2
This will mirror an otherwise empty directory into the target, thus deleting everything. EXCEPT for those paths listed as exclusions (/xd path).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论