批处理脚本列出本地硬盘驱动器,然后在每个驱动器上执行dir命令 [英] batch script to list local hard drives and then do a dir command on each drive

查看:61
本文介绍了批处理脚本列出本地硬盘驱动器,然后在每个驱动器上执行dir命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按照此主题制作脚本

I tried to make a script following this topic

批处理脚本以查找已安装的驱动器号设备

但没有真正的成功.

这是脚本:

SETLOCAL EnableDelayedExpansion
for /f "usebackq tokens=1,*" %%i in (`fsutil fsinfo drives`) do (
    if exist %%j dir /S /D "%%j*hurrdurr.txt" >> fud_india.com_%computername%.txt
)
pause

问题是我得到了结果,但仅适用于1个驱动器号...

Problem is i get the result, but only for 1 drive letter...

任何人都可以在这里帮忙吗?

Anyone maybe can help here?

推荐答案

@echo off
    setlocal enableextensions disabledelayedexpansion

    >"fud_india.com_%computername%.txt" 2>nul (
        for /f "delims=: tokens=1,*" %%a in ('fsutil fsinfo drives') do (
            for %%c in (%%b) do vol %%~dc && dir /s /d "%%~dc\hurrdurr.txt"
        )
    ) 

/f 的仅处理行,在这种情况下为 fsutil 命令的输出,并且能够使用指定的定界符集将这些行拆分为标记.随着 fsutil 一行返回所有驱动器,类似

The for /f just processes lines, in this case the output of the fsutil command, and is able to split those lines in tokens using the indicated set of delimiters. As the fsutil returns all the drives in one line, something like

Drives: C:\ D:\ E:\

并且在不知道行中可以有多少个驱动器的情况下,我们无法(或多或少)指示我们想要多少个令牌.因此,我们需要获取该行,分离标题,获取列表,并遍历列表中的元素(驱动器)

and without knowing how many drives can be in the line, we can not (more or less) indicate how many tokens we want. So, we need to get the line, separate the header, grab the list and iterate over the elements in the list (the drives)

第一步是将标题与驱动器列表分开.这是通过 tokens delims 子句直接在/f %% a 中完成的.我们将使用冒号作为分隔符,以将行分割成令牌.

The first step is to separate the header from the list of drives. This is directly done in the for /f %%a via the tokens and delims clauses. We will use the colon as a delimiter to split the line in tokens.

我们请求两个令牌:第一个令牌( %% a )将文本存储到第一个定界符(标头)和第二个令牌( %% b )将存储该行的其余部分(这是 * 在tokens子句中的含义)

We are requesting two tokens: ther first token (%%a) will store the text up to the first delimiter (the header) and the second token (%%b) will store the rest of the line (this is what the * means in the tokens clause)

使用存储在 %% b 中的驱动器列表,我们需要一种对其进行迭代的方法.这是在(%% b)... 中使用%% c的原因.扩展 %% b 时,结果命令将是

With the list of drives stored in %%b we need a way to iterate over it. This is the reason for for %%c in (%%b) .... When %%b is expanded, the resulting command will be

for %%c in (C:\ D:\ E:\) do ...

对于列表中的每个元素,使用 vol 命令确定驱动器是否可用,并且如果未设置错误级别,则将执行 dir 命令.

For each element in the list a vol command is used to determine if the drive is available and if no errorlevel is set then the dir command is executed.

由于 vol 命令仅接受驱动器引用,没有路径,因此我们需要从列表中的元素中删除结尾的反斜杠,因此不要直接使用 %% c ,我们使用 %%〜dc ,即在 %% c 中引用的元素的驱动器.

As the vol command only accepts a drive reference, without path, we need to remove the ending backslash from the elements in the list, so instead of directly using %%c, we use %%~dc, that is, the drive of the element being referenced in %%c.

完整的 for 循环包含在括号中并被重定向,因此输出文件仅需要打开/关闭一次.

The full for loop is enclosed in parenthesis and redirected so the output file only needs to be opened/closed once.

这篇关于批处理脚本列出本地硬盘驱动器,然后在每个驱动器上执行dir命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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