Please, I need a batch to delete all folders, except specific folders, and specific sub-folders

huangapple go评论50阅读模式
英文:

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).

huangapple
  • 本文由 发表于 2023年6月16日 03:34:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定