How do I perform string operations on variables in a for loop?

前端 未结 5 797
遇见更好的自我
遇见更好的自我 2021-02-06 06:57

This sounds dumb, but I can\'t get it to work. I think i just dont\' understand the difference between %%v, %v% and %v

Here\'s what I\'m trying to do:

相关标签:
5条回答
  • 2021-02-06 07:34

    Yet another way that I prefer is to create a sub-routine (:processMpeg) that I call for each element in the For loop, to which I pass the %%v variable.

    for %%v in (*.flv) do call :processMpeg "%%v"
    goto :eof
    
    :processMpeg
      set fileName=%~n1
      echo P1=%1  fileName=%fileName%  fullpath=%~dpnx1
      ffmpeg.exe -i "%~1" -y -f mjpeg -ss 0.001 -vframes 1 -an "%filename%.jpg"
      goto :eof
    
    0 讨论(0)
  • 2021-02-06 07:43

    For people who found this thread looking for how to actually perform string operations on for-loop variables (uses delayed expansion):

    setlocal enabledelayedexpansion
    
    ...
    
    ::Replace "12345" with "abcde"
    for %%i in (*.txt) do (
        set temp=%%i
        echo !temp:12345=abcde!
    )
    
    0 讨论(0)
  • 2021-02-06 07:51
    for %%v in (*.flv) do ffmpeg.exe -i "%%v" -y -f mjpeg -ss 0.001 -vframes 1 -an "%%~nv.jpg"
    

    From "help for":

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
    
    0 讨论(0)
  • 2021-02-06 07:51

    I am not as good at batch as the above (I use WSH or other script languages instead) but I can try and explain %%v %v and %v%.

    The first two forms are used in a for loop. help for explains the difference, the first form is used in a batch file while the second one is used when typing (pasting) the command directly at the command prompt.

    The last form just replaces the variable name (environment variable) with its value:

    set FOO=C:\bar\foo
    cd %FOO%\gah
    
    0 讨论(0)
  • 2021-02-06 07:52

    Use %%~nV to get the filename only.

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