在dos中设置dir命令的递归深度 [英] Set Recursive-Depth for dir command in dos

查看:1540
本文介绍了在dos中设置dir命令的递归深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下命令列出一些目录:

I'm currently using the following command to list some directories:

dir /b /s /AD > c:\temp\dir_list.txt

这给了我几乎所需的列表。但是这是太多的数据,因为一些文件夹有很多子文件夹,我不想在我的列表中看到。

This gives me almost the list that I need. But it is way too much data, because some folders have very many subfolders, that I don't want to see in my listing.

是否可以限制命令的递归深度说明 - 3?

Is it possible, to limit the recursion depth of the command to -lets say- 3?

c:\dir_1\dir_2\dir_3\dir_foo

所以如果我在上面的例子中执行命令c:>我不想看到dir_foo目录,但只是dir_n ...

So if I execute the command in the above example in c:> I don't want to see the dir_foo directory, but just the dir_n ones...

可能没有批处理/ vb脚本?

Maybe without a batch/vb-script?

推荐答案

我确定可以编写一个复杂的命令,列出n级目录。但是很难记住语法和错误。每次您想要更改级别数量时,还需要更改。

I'm sure it is possible to write a complex command that would list n levels of directories. But it would be hard to remember the syntax and error prone. It would also need to change each time you want to change the number of levels.

更好地使用简单的脚本。

Much better to use a simple script.

5年后编辑 - 实际上,Vista有一个简单的一个班轮。 查看我的新ROBOCOPY解决方案。

这里是一个执行深度优先列表的批处理解决方案。 DIR / S命令执行广泛的第一个列表,但我更喜欢这种深度第一格式。

Here is a batch solution that performs a depth first listing. The DIR /S command performs a breadth first listing, but I prefer this depth first format.

@echo off
setlocal
set currentLevel=0
set maxLevel=%2
if not defined maxLevel set maxLevel=1

:procFolder
pushd %1 2>nul || exit /b
if %currentLevel% lss %maxLevel% (
  for /d %%F in (*) do (
    echo %%~fF
    set /a currentLevel+=1
    call :procFolder "%%F"
    set /a currentLevel-=1
  )
)
popd

广度优先版本几乎相同,除了需要额外的FOR循环。

The breadth first version is nearly the same, except it requires an extra FOR loop.

@echo off
setlocal
set currentLevel=0
set maxLevel=%2
if not defined maxLevel set maxLevel=1

:procFolder
pushd %1 2>nul || exit /b
if %currentLevel% lss %maxLevel% (
  for /d %%F in (*) do echo %%~fF
  for /d %%F in (*) do (
    set /a currentLevel+=1
    call :procFolder "%%F"
    set /a currentLevel-=1
  )
)
popd

两个脚本都有两个参数:

Both scripts expect two arguments:

arg1 =要列出的根目录的路径

arg1 = the path of the root directory to be listed

arg2 =要列出的级别数。

arg2 = the number of levels to list.

所以要列出当前目录的3个级别,可以使用

So to list 3 levels of the current directory, you could use

listDirs.bat . 3

要列出不同目录的5个级别,您可以使用

To list 5 levels of a different directory, you could use

listDirs.bat "d:\my folder\" 5

这篇关于在dos中设置dir命令的递归深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆