如何在Windows命令行中进行for循环? [英] How to do a for loop in windows command line?
问题描述
我想知道这是否可能?我对使用Windows命令行不熟悉,但是我必须将其用于我正在处理的项目.我有许多文件,每个文件都需要执行一个功能.我曾经使用过python,但是显然这有点不同,所以我希望获得一些帮助.
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.
基本上,我需要for循环遍历一个文件夹中的17个文件,对每个文件执行一个功能(即使用我在此处具有该项目的特定软件),然后输出一个具有唯一名称的文件(该功能通常需要我声明输出文件名)我会把它吸起来,然后手动对17个文件进行处理,但基本上,它是在创建文件数据库,然后将其与17个文件进行比较.被迭代了数百遍.使用for循环可以节省我很多时间.
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构造,您可以从命令提示符或批处理文件中使用它.
The commandline interpreter does indeed have a FOR construct that you can use from the command prompt or from within a batch file.
出于您的目的,您可能想要类似的东西:
For your purpose, you probably want something like:
FOR %i IN (*.ext) DO my-function %i
这将导致当前目录中每个扩展名为* .ext的文件的名称传递给my-function(例如,可能是另一个.bat文件).
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)
部分是"filespec",在指定文件集方面非常灵活.例如,您可以这样做:
The (*.ext)
part is the "filespec", and is pretty flexible with how you specify sets of files. 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.
一般来说,filespec和FOR有很多选项.见
There are scores of options for the filespec and FOR in general. See
HELP FOR
从命令提示符处获取更多信息.
from the command prompt for more information.
这篇关于如何在Windows命令行中进行for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!