简体   繁体   English

Windows批处理文件,通过子文件夹循环

[英]Windows batch file with loop through subfolders

I didnt succeed writing an approriate batch file and would appreciate some help. 我没有成功编写适当的批处理文件,将不胜感激。

Let's say I have an exe in D:\\Test\\ called script.exe. 假设我在D:\\ Test \\中有一个称为script.exe的exe。 To execute this script.exe it requires an additional argument of a .bin files (eg bin01.bin, bin02.bin). 要执行此script.exe,它需要一个.bin文件的附加参数(例如bin01.bin,bin02.bin)。

Therefore it would be like: script.exe -i bin01.bin, script.exe -i bin01 因此,它将类似于:script.exe -i bin01.bin,script.exe -i bin01

I want the script to execute with all .bin files from all subfolders 我希望脚本与所有子文件夹中的所有.bin文件一起执行

D:\\Test\\script.exe D:\\ Test \\ script.exe
D:\\Test\\Folder01\\bin01.bin D:\\ Test \\ Folder01 \\ bin01.bin
D:\\Test\\Folder01\\bin02.bin D:\\ Test \\ Folder01 \\ bin02.bin
D:\\Test\\Folder02\\bin01.bin D:\\ Test \\ Folder02 \\ bin01.bin
D:\\Test\\Folder03\\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"

The command FOR searches recursive in directory D:\\Test and all subdirectories for files matching the wildcard pattern *.bin . 命令FOR在目录D:\\Test和所有子目录中递归搜索与通配符模式*.bin匹配的文件。

The name of each found file is assigned with full path to case-sensitive loop variable I . 找到的每个文件的名称均带有区分大小写的循环变量I完整路径。

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

@ 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. 整个FOR命令行开头的@只是告诉Windows命令解释程序cmd.exe在预处理后默认执行之前不回显命令行,然后执行控制台窗口。

@ at beginning of D:\\Test\\script.exe avoids the output of this command to console executed on each iteration of the loop. D:\\Test\\script.exe开头的@避免在循环的每次迭代中将此命令输出到控制台。

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执行前的命令行回显,如下所示。

@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 /?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM