如何在 Windows 命令行中执行 for 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11192039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do a for loop in windows command line?
提问by TheFoxx
I was wondering if this was possible? I'm not familiar with using windows command line, but I have to use it for a project I'm working on. I have a a number of files, for which I need to perform a function for each. I'm used to working with python, but obviously this is a bit different, so I was hoping for some help.
我想知道这是否可能?我不熟悉使用 windows 命令行,但我必须将它用于我正在处理的项目。我有许多文件,我需要为每个文件执行一个功能。我习惯使用 python,但显然这有点不同,所以我希望得到一些帮助。
Basically I need the for loop to iterate through 17 files in a folder, perform a function on each (that's using the specific software I have here for the project) and then that will output a file with a unique name (the function normally requires me to state the output file name) I would suck it up and just do it by hand for each of the 17, but basically it's creating a database of a file, and then comparing it to each of the 17. It needs to be iterated through several hundred times though. Using a for loop could save me days of work.
基本上我需要 for 循环来遍历文件夹中的 17 个文件,对每个文件执行一个函数(使用我在这里为项目提供的特定软件),然后将输出一个具有唯一名称的文件(该函数通常需要我来说明输出文件名)我会把它搞定,然后为 17 个中的每一个手动完成,但基本上它正在创建一个文件的数据库,然后将它与 17 个中的每一个进行比较。它需要迭代虽然几百次。使用 for 循环可以节省我几天的工作时间。
Suggestions?
建议?
回答by Myk Willis
The commandline interpreter does indeed have a FOR construct that you can use from the command prompt or from within a batch file.
命令行解释器确实有一个 FOR 构造,您可以从命令提示符或批处理文件中使用它。
For your purpose, you probably want something like:
为了您的目的,您可能想要这样的东西:
FOR %i IN (*.ext) DO my-function %i
Which will result in the name of each file with extension *.ext in the current directory being passed to my-function (which could, for example, be another .bat file).
这将导致当前目录中每个扩展名为 *.ext 的文件的名称被传递给 my-function(例如,它可能是另一个 .bat 文件)。
The (*.ext)
part is the "filespec", and is pretty flexible with how you specify sets of files. For example, you could do:
该(*.ext)
部分是“filespec”,并且在如何指定文件集方面非常灵活。例如,你可以这样做:
FOR %i IN (C:\Some\Other\Dir\*.ext) DO my-function %i
To perform an operation in a different directory.
在不同的目录中执行操作。
There are scores of options for the filespec and FOR in general. See
一般而言,filespec 和 FOR 有很多选项。看
HELP FOR
from the command prompt for more information.
从命令提示符获取更多信息。
回答by Josh Girouard
This may help you find what you're looking for... Batch script loop
这可能会帮助您找到您要查找的内容... 批处理脚本循环
My answer is as follows:
我的回答如下:
@echo off
:start
set /a var+=1
if %var% EQU 100 goto end
:: Code you want to run goes here
goto start
:end
echo var has reached %var%.
pause
exit
The first set of commands under the start label loops until a variable, %var% reaches 100. Once this happens it will notify you and allow you to exit. This code can be adapted to your needs by changing the 100 to 17 and putting your code or using a call command followed by the batch file's path (Shift+Right Click on file and select "Copy as Path") where the comment is placed.
开始标签下的第一组命令循环直到变量 %var% 达到 100。一旦发生这种情况,它会通知您并允许您退出。此代码可以通过将 100 更改为 17 并放置您的代码或使用调用命令后跟批处理文件的路径(Shift+右键单击文件并选择“复制为路径”)来适应您的需要,其中放置了注释。
回答by Christian Gosch
For doing loops independently of files or directories see e. g. Batch script loop,. Btw. MS help should help also (but who digs there)
回答by Alice Vixie
You might also consider adding "
.
您也可以考虑添加"
.
For example for %i in (*.wav) do opusenc "%~ni.wav" "%~ni.opus"
is very good idea.
例如,这for %i in (*.wav) do opusenc "%~ni.wav" "%~ni.opus"
是一个很好的主意。