Windows批处理文件,通过子文件夹循环 [英] Windows batch file with loop through subfolders

查看:112
本文介绍了Windows批处理文件,通过子文件夹循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有成功编写适当的批处理文件,希望获得一些帮助.

I didnt succeed writing an approriate batch file and would appreciate some help.

假设我在D:\ Test \中有一个名为script.exe的exe. 要执行此script.exe,它需要一个.bin文件的附加参数(例如bin01.bin,bin02.bin).

Let's say I have an exe in D:\Test\ called script.exe. To execute this script.exe it requires an additional argument of a .bin files (e.g. bin01.bin, bin02.bin).

因此它将是:script.exe -i bin01.bin,script.exe -i bin01

Therefore it would be like: script.exe -i bin01.bin, script.exe -i bin01

我希望脚本与所有子文件夹中的所有.bin文件一起执行

I want the script to execute with all .bin files from all subfolders

D:\ Test \ script.exe
D:\ Test \ Folder01 \ bin01.bin
D:\ Test \ Folder01 \ bin02.bin
D:\ Test \ Folder02 \ bin01.bin
D:\ Test \ Folder03 \ bin01.bin

D:\Test\script.exe
D:\Test\Folder01\bin01.bin
D:\Test\Folder01\bin02.bin
D:\Test\Folder02\bin01.bin
D:\Test\Folder03\bin01.bin

有人可以在这里帮助我吗? 提前非常感谢.

anyone could help me here? Thanks a lot in advance.

推荐答案

从命令提示符窗口直接执行:

For direct execution from within a command prompt window:

for /R "D:\Test" %I in (*.bin) do @D:\Test\script.exe -i "%I"

在批处理文件中使用相同的命令行:

And the same command line for usage in a batch file:

@for /R "D:\Test" %%I in (*.bin) do @D:\Test\script.exe -i "%%I"

命令 FOR 在目录D:\Test及其所有子目录中的递归中搜索与通配符模式*.bin相匹配的文件.

The command FOR searches recursive in directory D:\Test and all subdirectories for files matching the wildcard pattern *.bin.

为每个找到的文件的名称分配一个区分大小写的循环变量I的完整路径.

The name of each found file is assigned with full path to case-sensitive loop variable I.

FOR 为每个文件执行可执行文件D:\Test\script.exe,其中第一个参数-i和第二个参数是找到的文件的名称,全路径用双引号引起来,适用于任何* .bin文件甚至为包含空格或以下字符之一的名称命名:&()[]{}^=;!'+,`~.

FOR executes for each file the executable D:\Test\script.exe with first argument -i and second argument being the name of found file with full path enclosed in double quotes to work for any *.bin file name even those containing a space or one of these characters: &()[]{}^=;!'+,`~.

@只是告诉Windows命令解释器cmd.exe在预处理后不要回显命令行,默认情况下将其执行到控制台窗口.

@ at beginning of entire FOR command line just tells Windows command interpreter cmd.exe not to echo the command line after preprocessing before execution to console window as by default.

@可以避免在循环的每次迭代中将此命令输出到控制台.

@ at beginning of D:\Test\script.exe avoids the output of this command to console executed on each iteration of the loop.

在批处理文件的开头,通常使用@echo off关闭执行前的命令行回显,如下所示.

Most often the echo of a command line before execution is turned off at beginning of the batch file with @echo off as it can be seen below.

@echo off
for /R "D:\Test" %%I in (*.bin) do D:\Test\script.exe -i "%%I"

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • echo /?
  • for /?

这篇关于Windows批处理文件,通过子文件夹循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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