string 如何在 for 循环中对变量执行字符串操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/252286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:19:43  来源:igfitidea点击:

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

stringbatch-filecommand-line

提问by Brent

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

这听起来很愚蠢,但我无法让它发挥作用。我想我只是不明白两者之间的区别%%v, %v% and %v

Here's what I'm trying to do:

这是我想要做的:

for %%v in (*.flv) do ffmpeg.exe -i "%%v" -y -f mjpeg -ss 0.001 -vframes 1 -an "%%v.jpg"

This successfully generates a thumbnail for each of the movies, but the problem is:

这成功地为每部电影生成了一个缩略图,但问题是:

movie.flv -> movie.flv.jpg

So what I would like to do is pull the last 4 characters off %%vand use that for the second variable.

所以我想要做的是将最后 4 个字符拉出%%v并将其用于第二个变量。

I've been trying things like this:

我一直在尝试这样的事情:

%%v:~0,-3%

But it's not working, nor are any of the iterations of that that I could think of.

但它不起作用,我能想到的任何迭代也不起作用。

Any ideas?

有任何想法吗?

采纳答案by WPWoodJr

Use %%~nV to get the filename only.

使用 %%~nV 仅获取文件名。

回答by BlueRaja - Danny Pflughoeft

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

对于发现此线程以寻找如何对 for 循环变量实际执行字符串操作的人(使用延迟扩展):

setlocal enabledelayedexpansion

...

::Replace "12345" with "abcde"
for %%i in (*.txt) do (
    set temp=%%i
    echo !temp:12345=abcde!
)

回答by Jeff Hillman

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

回答by PhiLho

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

我不擅长批处理(我使用 WSH 或其他脚本语言代替),但我可以尝试解释 %%v %v 和 %v%。

The first two forms are used in a forloop. help forexplains 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.

前两种形式在for循环中使用。help for解释了不同之处,第一种形式用于批处理文件,而第二种形式用于直接在命令提示符下键入(粘贴)命令。

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

最后一种形式只是用它的值替换变量名(环境变量):

set FOO=C:\bar\foo
cd %FOO%\gah

回答by Farsee

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.

我更喜欢的另一种方法是创建一个子例程(:processMpeg),我为 For 循环中的每个元素调用它,我将 %%v 变量传递给它。

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