Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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 %%v and use that for the second variable.

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?

like image 520
Brent Avatar asked Oct 31 '08 00:10

Brent


3 Answers

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!
)
like image 164
BlueRaja - Danny Pflughoeft Avatar answered Oct 15 '22 11:10

BlueRaja - Danny Pflughoeft


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
like image 9
Jeff Hillman Avatar answered Oct 15 '22 12:10

Jeff Hillman


Use %%~nV to get the filename only.

like image 8
WPWoodJr Avatar answered Oct 15 '22 12:10

WPWoodJr