我正在尝试编写一个批处理文件,该文件将计算当前目录中有多少个文件和多少个目录。
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 :(得分: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.
答案 1 :(得分: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 /r
和for /r /d
。
希望这有帮助,莫娜。