2008-10-31 38 views
8

这听起来很愚蠢,但我无法让它工作。我想我只是不”明白%%v, %v% and %v如何在for循环中对变量执行字符串操作?

这里的区别就是我想要做的事:

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

这成功地生成每个电影的缩略图,但问题是:

movie.flv -> movie.flv.jpg 

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

我一直想这样的事情:

%%v:~0,-3% 

但它不工作,也没有任何的是,我能想到的迭代。

任何想法?

+0

如果在一行中“设置”和“回声”FOR循环:nslookup -vc -type = TXT twitter.com 8.8.8.8 |找到“”“”| for/F“tokens = *”%f in('findstr $')do @(echo | set/P str =%f) – diyism 2013-06-09 13:35:50

回答

8

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

+0

“n”表示什么? – UltimateBrent 2008-10-31 01:02:49

+0

工作正常!我不明白为什么...... – UltimateBrent 2008-10-31 01:05:10

+1

请参阅“help for”: 此外,FOR变量引用的替换已得到增强。 您现在可以使用下列选项语法: %〜I - 将%I扩充删除任何引号(“) ... %〜镍 - 将%I扩充到一个文件名只 ... – WPWoodJr 2008-10-31 01:08:25

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

从“帮助”:

%~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 
2

我不批如上述(我用WSH或其他脚本语言,而不是),但我可以尝试解释%% v%的好v和%v%。

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

最后一种形式只是取代了变量名(环境变量)与它的价值:

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

对于谁发现了这个线索寻找如何真正对循环变量进行字符串操作人(使用delayed expansion) :

setlocal enabledelayedexpansion 

... 

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

然而我更喜欢另一种方式是创建一个子程序(: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