[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 求助:删除除指定文件夹外的所有文件夹及文件

比如我D盘有两个文件夹:a和b
       我想在开机的时候删除D盘除文件夹a和b外的其它所有文件和文件夹,能不能用批处理完成?呵呵。。。
    为谢!

  1. @echo off
  2. for /d %%i in (d:\*) do if /i "%%~ni" neq "a" if /i "%%~ni" neq "b" rd /s /q "%%i"
  3. pause
复制代码
心绪平和,眼藏静谧。

TOP

pusofalse就是强啊!人又好!有问必答!呵呵……
虚心使人进步 细节成就完美

TOP

快速清理桌面

我发一个和楼主的意思差不多,写的代码可能有点臃肿望高人优化
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. if "%cd%" neq "%userprofile%\桌面" echo 请把该文件放到桌面上使用!&pause>nul&exit
  4. for /f "delims=:" %%a in ('findstr /n /c:"[文件夹设置]" %0') do set fdk=%%a
  5. for /f "delims=:" %%a in ('findstr /n /c:"[文件设置]" %0') do set flk=%%a
  6. for /f "delims=" %%i in ('dir /ad /b *.*') do (
  7. call :fd "%%i"
  8. )
  9. for /f "delims=" %%j in ('dir /a-d /b *.*') do (
  10.         call :file "%%j"
  11. )
  12. pause
  13. ::判断文件夹部分
  14. :fd
  15. for /f "skip=%fdk% delims=" %%m in (%~sdpnx0) do (
  16.         if %1 equ "%%m" goto :eof
  17.         set a=%%m
  18.         if "!a:~0,3!"=="***" echo rd  /s /q %1&goto :eof
  19. )
  20. echo rd /s /q %1
  21. goto :eof
  22. ::判断文件部分
  23. :file
  24. for /f "skip=%flk% delims=" %%n in (%~sdpnx0) do (
  25.         if %1 equ "%%n" goto :eof
  26. )
  27. if %1 neq "%~nx0" echo del /f /q %1
  28. goto :eof
  29. *******************************************************************************
  30. [文件夹设置](请在下面输入你要保留的“文件夹”每行只能输入一个)
  31. 新建文件夹
  32. 小游戏
  33. *******************************************************************************
  34. [文件设置](请在下面输入你要保留的“文件”每行只能输入一个)
  35. a.txt
  36. uc.lnk
  37. a.vbs
  38. a.bat
  39. 一键上网.bat
  40. 腾讯QQ.lnk
  41. MvBox.lnk
  42. 132.wma
  43. 快速关机.bat
  44. HTML语言教程.exe
复制代码

TOP

我技术不行。
不过你可以自己发散思维啊!!!
你可以把a和b   copy到其他地方再删除d盘中的全部文件夹在copy回来也一样,,,,
----------------------------呵呵------------------------思路而已--------------------------------------

TOP

回复 5楼 的帖子

如果a和b太大的话,可能会有问题。
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

这种问题好像很多人问。应该是用来搞QQ登陆后生成的那些文件夹吧。

TOP

回复6楼

哦!
那是我没想到大的,你想的真全面啊!!
我有2个思路不知道行不。
1:用有个命令我忘了是怎么写的了------------命令的作用是不让任何人访问好像是cal??
这样他就不能删除了吧。
2:在a和b下建立个..\文件夹呵呵,这样也删除不了。删除到a  b的时候就能跳过了。。
对吧!!!!

TOP

谢谢各位达人!呵呵

TOP

回复 8楼 的帖子

1、设置读写权限,可以用cacls命令,但只能适用于NTFS分区,遇到FAT32分区就不行了。
2、在子目录下面建立以点结尾的文件夹是没用的,你试试2楼的rd /s /q,一样能删除。
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

请问二楼代码
for /d %%i in (d:\*) do if /i "%%~ni" neq "a" if /i "%%~ni" neq "b" rd /s /q "%%i"
里的"%%~ni"一句里     ~ 和n分别代表什么呀?

四楼里(%~sdpnx0)一句每个字符又分别代表什么呀?谢谢!

TOP

回复 11楼 的帖子

尽快学会看帮助
for /?
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表