Dir命令无法正常运行

时间:2018-09-10 15:57:25

标签: batch-file directory

我应该写一个批处理文件,该文件将找到以字母a开头并以扩展名.dll结尾的文件。

这是我想出的:

@echo off
dir /b *.dll C:\windows\system32\ > dll.txt
dir /b a* C:\windows\system32\ > a.txt

我的脚本无法按照我想要的方式工作,因为它列出了system32目录中的所有文件。

有人能帮我按应有的方式解决它吗?

3 个答案:

答案 0 :(得分:2)

dir /b *.dll C:\windows\system32\列出与*.dll匹配的所有文件,然后列出与C:\windows\system32\匹配的所有文件-即该文件夹中的所有文件。您要执行的操作的正确语法是:

dir /b "C:\windows\system32\*.dll"

与另一行相同:

dir /b "C:\windows\system32\a*"

答案 1 :(得分:1)

您可以尝试使用此批次:

// Classes 'A' and 'MyClass' are 3rd party, I do not want to edit them
public class A {
    private MyClass myClass;

    A(){
        myClass = new MyClass();

        // Changes I want to keep doing
        doSomething();
    }

    public void doSomething(){
        // Do some calculations to myClass and modify it accordingly 
    }

    public MyClass getMyClass(){
        return myClass
    }
}


// Classes 'B' and 'MyCustomClass' are 1st party, I can do anything I want with them :)
public class B extends A{
    private MyCustomClass myCustomClass;        

    B(){
        super();

        myCustomClass = ?????????;


        // Extremely important message, this should keep working
        System.out.println(myCustomClass.verySpecialString);

        // Build further on made modifications, this should keep working too
        useModificationsByClassA();
    }

    public void useModificationsByClassA(){
        // Use result of class A's calculations in order to continue
    }
}


public class MyCustomClass extends MyClass{
    private String verySpecialString;

    MyCustomClass(){
        super();

        verySpecialString = "Hey!";
    }
}

答案 2 :(得分:0)

命令 DIR 支持用于目录列表的多个参数。

dir /b *.dll C:\windows\system32\ > dll.txt

此命令行导致写入文件dll.txt

  1. 当前目录中所有匹配的非隐藏文件和文件夹名称通配符模式*.dll
  2. 目录C:\windows\system32\中所有与默认通配符模式*匹配的非隐藏文件和文件夹名称。

下一个命令行是:

dir /b a* C:\windows\system32\ > a.txt

此命令行导致写入文件a.txt

  1. 当前目录中所有匹配的非隐藏文件和文件夹名称通配符模式a*
  2. 目录C:\windows\system32\中所有与默认通配符模式*匹配的非隐藏文件和文件夹名称。

但是最可能需要的是将dll.txt目录中的所有{.1。}文件和C:\windows\system32\目录a.txt中的所有a*文件写入C:\windows\system32\次,还包括具有隐藏属性集的匹配文件。因此,要使用的命令行为:

dir %SystemRoot%\System32\*.dll /A-D /B >dll.txt
dir %SystemRoot%\System32\a* /A-D /B >a.txt

选项/A-D禁用偶然匹配通配符模式的目录列表(属性而非目录),并启用隐藏文件列表。

要获取有关命令 DIR 的帮助,请在命令提示符窗口dir /?中运行。

顺便说一句:应该考虑Windows file system redirector。如果批处理文件由*.dll中的32位a*执行,则两个命令行将目录%SystemRoot%\SysWOW64中的cmd.exe%SystemRoot%\SysWOW64\文件输出到文本文件在64位Windows上,因为批处理文件是从32位应用程序中启动的。

下面的批处理文件可用于确保根据Windows架构获取Windows系统目录的列表,这在64位Windows上有所不同。

set "SystemFolder=%SystemRoot%\System32"
if exist "%SystemRoot%\Sysnative\cmd.exe" set "SystemFolder=%SystemRoot%\Sysnative"
dir %SystemFolder%\*.dll /A-D /B >dll.txt
dir %SystemFolder%\a* /A-D /B >a.txt

非常特殊的%SystemRoot%\Sysnative重定向器对于64位应用程序不存在,因此对于目录cmd.exe中由64位%SystemRoot%\System32\执行的批处理文件也不存在。请注意,Sysnative是目录还是符号链接。只能检查%SystemRoot%\Sysnative中是否存在文件,而不能检查%SystemRoot%\Sysnative本身是否存在。

相关问题