2

我正在尝试创建两个 bat 文件。一个是 c.bat,另一个是 r.bat

c.bat 仅包含:

cls
javac *.java

这将编译所有 .java 文件。

在 r.bat 我有:

cls
java *

它不会运行类文件。我认为这是因为“java *”行中的 * 转换为“java Class1.class”、“java Class2.class”等等,当它应该是“java Class1”和“java Class2”时。(没有扩展)我该怎么做?我刚开始学习这些东西,我在任何地方都找不到正确的答案。

4

1 回答 1

2

以下循环遍历.java找到的所有文件C:\java\stuff并一个接一个地运行它们。%%~nf正在格式化文件名以不显示文件扩展名,即java 类名。如果您更改java %%~nf为,echo java %%~nf您可以确切地看到发生了什么。

cls
for "C:\java\stuff" %%f in (*.java) do (
    java %%~nf
)

对于:以下选项可用:

Variable with modifier  Description

%~I                     Expands %I which removes any surrounding 
                        quotation marks ("").
%~fI                    Expands %I to a fully qualified path name.
%~dI                    Expands %I to a drive letter only.
%~pI                    Expands %I to a path only.
%~nI                    Expands %I to a file name only.
%~xI                    Expands %I to a file extension only.
%~sI                    Expands path to contain short names only.
%~aI                    Expands %I to the file attributes of file.
%~tI                    Expands %I to the date and time of file.
%~zI                    Expands %I to the size of file.
%~$PATH:I               Searches the directories listed in the PATH environment 
                        variable and expands %I to the fully qualified name of 
                        the first one found. If the environment variable name is 
                        not defined or the file is not found by the search,
                        this modifier expands to the empty string. 
于 2012-10-26T16:19:45.033 回答