How to do a for loop in windows command line?

前端 未结 3 542
借酒劲吻你
借酒劲吻你 2021-01-30 13:37

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 whic

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-30 14:21

    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
    

    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).

    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.

    There are scores of options for the filespec and FOR in general. See

    HELP FOR
    

    from the command prompt for more information.

提交回复
热议问题