2014-02-06 56 views
0

我试图编写一个批处理文件来计算当前目录中有多少个文件和多少个目录。计算某个目录内的文件和文件夹

for /r %%i in (dir) do (
    if exist %%i\* (
     set /a directories=directories+1 
    ) else (
     set /a files=files+1 
    ) 
) 
echo directories 
echo files 

这是一个目录的,我尝试运行这个批处理文件结构:

---directory 
    ---file1 
---file2 

而且这始终返回“2个文件”和“0目录”。

+0

你想包含子目录中的文件和direcotries? – Monacraft

回答

0

试试这个:

@echo off 
set total=0 
set dir=0 
set files=0 
for /f %a in ('dir /b') do (set /a total+=1) 
for /f %a in ('dir /b /a:d') do (set /a dir+=1) 
set /a files=%total%-%dir% 

Echo There are %dir% direcotries and %files% files in the current directory alone 

这将不计算子目录,以及,你会用for /rfor /r /d

希望这对蒙娜有所帮助。

0

for /r将递归搜索目录(默认情况下它将搜索当前目录) - *将返回目录树中的所有文件,.将返回树中的所有目录。

@echo off 

set files= 
set directories= 

for /r %%a in (*) do set /a files+=1 
for /r %%b in (.) do set /a directories+=1 

echo files:  %files% 
echo directories: %directories% 

看看for命令帮助页面 -

h:\>for /? 

FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters] 

    Walks the directory tree rooted at [drive:]path, executing the FOR 
    statement in each directory of the tree. If no directory 
    specification is specified after /R then the current directory is 
    assumed. If set is just a single period (.) character then it 
    will just enumerate the directory tree. 
+0

我不确定他是否想要子文件夹中的目录,因为他已经避免提及它。 – Monacraft

+0

@Monacraft,我以为他确实想要子目录,因为他正在收集'子文件'(想不到更好的单词)。 – unclemeat

相关问题