需要掌握 Windows 批处理语法

发布于 2024-10-26 01:28:38 字数 747 浏览 6 评论 0原文

我在 Windows 上编译了一个 C++ 程序,我需要它来处理大量的数据文件。例如,这些文件被命名为“x0000y”至“x9999y”。

C++ 程序一次仅获取一个文件,创建每个文件的输出,保存在某处,然后终止。我不想对程序进行硬编码,因为我的数据集并不总是具有相同数量的文件 - 仅仅为此而不断重新编译程序并不酷。所以我正在寻找一种快速的方法来做到这一点:批处理。

麻烦来了:我在尝试使批处理语法正确且有效时遇到困难。那么有人可以向我展示以下批处理版本中的伪代码吗?:

for (int i = 0; i < lastFile; i++){

    String filename;

    /*
    Because the files are named "x0000y", "x0034y", etc. 
    We need to put in all the extra 0s in the string if i is less than 1000.
    */
    String numberedString = convertNumToFourDigit(i);

    filename = "myFileName" + numberedString + "Footer";       

    /*
    execute the program with the respective filename.
    */
    execute("MyProgram.exe " + filename);

}

I have compiled a C++ program on Windows, and I need it to process a huge number of my data files. The files are named, for instance, "x0000y" to "x9999y".

The C++ program only takes one file at a time, create the output of each respective file, save somewhere, and terminate. I do not want to hard code the program, as my data set does not always have the same number of files - and keep recompiling the program just for this is not cool. So I am looking for a quick way of doing this: batch processing.

Here comes the trouble: I am having trouble trying to get the batch syntax correct and valid. So could somebody show me the following pseudocode in batch processing version?:

for (int i = 0; i < lastFile; i++){

    String filename;

    /*
    Because the files are named "x0000y", "x0034y", etc. 
    We need to put in all the extra 0s in the string if i is less than 1000.
    */
    String numberedString = convertNumToFourDigit(i);

    filename = "myFileName" + numberedString + "Footer";       

    /*
    execute the program with the respective filename.
    */
    execute("MyProgram.exe " + filename);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-11-02 01:28:38

这就是 .bat 文件中所需的全部内容,以便针对当前目录中的所有文件运行程序。

for %%I IN (*) DO ( MyProgram.exe %%I )

如果数据文件位于具有扩展名的子目录中,则这里有一个示例。

for %%I IN (subdir\*.dat) DO ( MyProgram.exe %%I )

如果您只需要 x0000y 形式的文件,那么这就可以了。

for %%I IN (x????y) DO ( MyProgram.exe %%I )

它们将以文件系统提供名称的任何顺序处理数据文件。

This is all you'll need in your .bat file to run your program against all of the files in the current directory.

for %%I IN (*) DO ( MyProgram.exe %%I )

If the data files are in a subdirectory with an extension, here's an example.

for %%I IN (subdir\*.dat) DO ( MyProgram.exe %%I )

If you only want files in the form of x0000y, then this will do the trick.

for %%I IN (x????y) DO ( MyProgram.exe %%I )

These will process the data files in whatever order the filesystem provides their names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文