批处理文件删除除指定列表以外的所有文件夹
答
这里是一个黑客,围绕= d
如果你的文件夹路径中说列表folders.txt列为如此:
- C:\ Documents和Settings \ Mechaflash
- Ç :\ Documents和Settings \妈妈
- C:\ Documents和Settings \爸爸
等你可以做的是暂时茶将它们设为隐藏文件夹,然后在所有非隐藏文件夹上设置RMDIR。
CD "C:\Documents and Settings\"
FOR /F "tokens=*" %%A IN (folders.txt) DO (
ATTRIB +H "%%A" /S /D
)
FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /B /A:-HD "C:\Documents and Settings\"`) DO (
RMDIR /S /Q %%A
)
FOR /F "tokens=*" %%A IN (folders.txt) DO (
ATTRIB -H "%%A" /S /D
)
答
rem the last space character is deliberate
set yourKeepList="abc def "
for /f %%f in ('dir /b/ad "C:\Documents and Settings"') do (
(echo %yourKeepList% | findstr /v /i "%%f " 1>nul) && rd /q/s %%f
)
答
一个解决方案使用ROBOCOPY:
cd /d "C:\Documents and Settings"
md tmp
robocopy . tmp /E /MOVE /XD folderToKeep1 folderToKeep2 ...
rd /s /q tmp
我不能让这一个正常工作;我确实包括了这个空间。 –
你有什么错误吗?还是它不会删除您预期的目录? – Arun
C:\ Documents and Settings>((echo“keepList.txt”| findstr/v/i“test”1> nul)&& rd/q/s f) 系统找不到指定的文件。 –