How to do a for loop in windows command line?

前端 未结 3 508
借酒劲吻你
借酒劲吻你 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:11

    You might also consider adding ".

    For example for %i in (*.wav) do opusenc "%~ni.wav" "%~ni.opus" is very good idea.

    0 讨论(0)
  • 2021-01-30 14:12

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题