简体   繁体   English

如何在Windows命令行中执行for循环?

[英]How to do a for loop in windows command line?

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. 我不熟悉使用Windows命令行,但我必须将它用于我正在进行的项目。 I have aa 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. 我习惯使用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. 基本上我需要for循环遍历文件夹中的17个文件,在每个文件上执行一个函数(使用我在这里为项目使用的特定软件),然后输出一个具有唯一名称的文件(该函数通常需要我说出输出文件的名称)我会把它搞砸,然后手工为17中的每一个做,但基本上它是创建一个文件的数据库,然后将它与17中的每一个进行比较。它需要迭代通过几百次。 Using a for loop could save me days of work. 使用for循环可以节省我几天的工作量。

Suggestions? 建议?

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. (*.ext)部分是“filespec”,并且对指定文件集的方式非常灵活。 For example, you could do: 例如,您可以这样做:

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. 一般来说,filespec和FOR有很多选项。 See 看到

HELP FOR

from the command prompt for more information. 从命令提示符获取更多信息。

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. start标签下的第一组命令循环,直到变量%var%达到100.一旦发生这种情况,它将通知您并允许您退出。 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. 此代码可以根据您的需要进行调整,方法是更改​​100到17并调整代码或使用调用命令,然后使用批处理文件的路径(Shift +右键单击文件并选择“复制为路径”)放置注释。

For doing loops independently of files or directories see eg Batch script loop ,. 对于独立于文件或目录的循环 ,请参见例如批处理脚本循环 ,。 Btw. 顺便说一句。 MS help should help also (but who digs there) MS帮助也应该有帮助(但谁在那里挖掘)

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"是非常好的主意。

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

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