Board logo

标题: 请教批处理查找并打开文件夹 [打印本页]

作者: jiudun     时间: 2007-11-28 15:14    标题: 请教批处理查找并打开文件夹

比如在D盘查找aaa文件夹,找到后打开.

  如果D盘有多个名为aaa的文件夹,如何挑选出一个占用空间最大或者文件夹内文件数量最多的aaa文件夹,并将其打开?
作者: jiudun     时间: 2007-11-28 21:32
没人知道吗?还是不可能实现?
作者: fastslz     时间: 2007-11-29 00:30
测试D:\TEST目录结构

├─a
│  └─aaa
├─aaa
├─b
│  ├─aaa
│  └─s_aaa
└─c
    ├─aaa
    └─s_aaa

rem 以文件数代码
@echo off
for /f "delims=" %%a in ('dir /s /b /ad D:\*aaa*^|find /i "\aaa"') do call :slz "%%a"
echo explorer.exe "%folder%"
pause

:slz
set xxx=%~1
if not defined xxx goto:eof
if not defined folder set "folder=%~1"
for /f "tokens=1* delims= " %%i in ('dir /-c "%~1"^|find "个文件"') do set most=%%i
if not defined most1 set most1=0
if %most% gtr %most1% set "most1=%most%" & set "folder=%~1"
echo "%~1" 有%most%个文件!
goto:eof

rem 以总文件大小代码
@echo off
for /f "delims=" %%a in ('dir /s /b /ad D:\*aaa*^|find /i "\aaa"') do call :slz "%%a"
echo explorer.exe "%folder%"
pause

:slz
set xxx=%~1
if not defined xxx goto:eof
if not defined folder set "folder=%~1"
for /f "tokens=3* delims= " %%i in ('dir /-c "%~1"^|find "个文件"') do set most=%%i
if not defined most1 set most1=0
if %most% gtr %most1% set "most1=%most%" & set "folder=%~1"
echo "%~1" 大小为 %most%!
goto:eof


  Quote:
最后结果
"D:\test\aaa" 有1个文件!
"D:\test\a\aaa" 有1个文件!
"D:\test\b\aaa" 有3个文件!
"D:\test\c\aaa" 有2个文件!
explorer.exe "D:\test\b\aaa"

[ Last edited by fastslz on 2007-11-29 at 10:50 PM ]
作者: jiudun     时间: 2007-11-29 21:13
高人,谢谢.
作者: jiudun     时间: 2007-11-29 21:20
另外我想知道,如果在不知道文件夹名字的情况下,也就是说打开D盘下文件夹内文件最多的一个文件夹,但是前提是不知道这个文件夹的名字,应该怎么做.
作者: fastslz     时间: 2007-11-29 21:39

@echo off
for /f "delims=" %%a in ('dir /s /b /ad D:\*') do call :slz "%%a"
echo explorer.exe "%folder%"
pause

:slz
set xxx=%~1
if not defined xxx goto:eof
if not defined folder set "folder=%~1"
for /f "tokens=1* delims= " %%i in ('dir /-c "%~1"^|find "个文件"') do set most=%%i
if not defined most1 set most1=0
if %most% gtr %most1% set "most1=%most%" & set "folder=%~1"
echo "%~1" 有%most%个文件!
goto:eof
[ Last edited by fastslz on 2007-11-29 at 10:51 PM ]
作者: jiudun     时间: 2007-11-29 21:46
谢谢"一根葱"大哥,一会儿去测试效果.
作者: fastslz     时间: 2007-11-29 22:55
3楼6楼代码修正过了(原来的处理不了首个数量最多的),重新测试~
作者: jiudun     时间: 2007-11-30 23:19
6楼的可以正确检测,但是"请按任意键继续"之后没动作了.
作者: fastslz     时间: 2007-12-1 00:59
@echo off
for /f "delims=" %%a in ('dir /s /b /ad D:\*') do call :slz "%%a"
echo explorer.exe "%folder%"
pause

:slz
set xxx=%~1
if not defined xxx goto:eof
if not defined folder set "folder=%~1"
for /f "tokens=1* delims= " %%i in ('dir /-c "%~1"^|find "个文件"') do set most=%%i
if not defined most1 set most1=0
if %most% gtr %most1% set "most1=%most%" & set "folder=%~1"
echo "%~1" 有%most%个文件!
goto:eof

我服了你!居然不知道echo和explorer.exe是什么东东,该多看看置顶教程了!去掉红色部分试试

作者: jiudun     时间: 2007-12-1 02:12
老大,我很菜,去掉后测试成功,万分感谢!
如果不想"按任意键继续"直接关闭命令提示符窗口,应该怎么弄啊?
作者: fastslz     时间: 2007-12-1 02:23
不你是老大
pause换成exit
作者: jiudun     时间: 2007-12-1 02:30
惭愧,不是我笨啊,我以前没接触过批处理,正在努力学习,真是万分感谢!