What I'm trying to do:
I would like to do a for /f *.mov command to search for all .mov files named as such "Recording" in front then an unspecified number of digits then an underscore then an unspecified number of digits signifying an iteration of the filename. Examples: Recording55555_0.mov or Recording55555_12.mov Recording555_12.mov
The first .mov file the for/f finds sees if there are .flv files in the same directory that have the same file name minus the iteration (anything after the "_" ) and then ONLY if the .mov file has more than one iteration it will do a dir listing of all .flv files that match the filename regardless of iteration, otherwise if there is only one iteration of .mov file skip it. Then, when the loop continues on to the next. .mov file ignore it if the filename has been processed before regardless of iteration.
This is an example directory listing I'm working in:
Recording55555_0.mov
Recording24562_3.mov
Recording24562_4.mov
Recording24562_12.mov
Recording24562_3.flv
Recording24562_4.flv
Recording24562_12.flv
Recording55555_0.flv
so in the example directory above the for loop would find Recording24562_3.mov and then using the "if" command, since there are more than one iteration of the filename it will do a dir listing that would show Recording24562_3.flv, Recording24562_4.flv, Recording24562_12.flv
Then.. when the next loop comes around ignore Recording24562_4.mov and _12.mov (since we already have our flv listing from the first loop) but instead move onto Recording55555_0.mov. But with the if command sees that there is only one iteration of that file so it skips it.
Essentially I'm only concerned with .mov files that have more than one iteration of the same filename. But I don't want to waste time with .mov files with the same filename but different iteration if I've already processed one of them.
I figured isolating the iteration number would be key to this problem so...
Quoted below from http://ss64.com/nt/syntax-replace.html I attempted to at least remove the iterations using the ss64 method recommended below.
:: To remove characters from the right hand side of a string is
:: a two step process and requires the use of a CALL statement
:: e.g.
SET _test=The quick brown fox jumps over the lazy dog
:: To delete everything after the string 'brown'
:: first delete 'brown' and everything before it
SET _endbit=%_test:*brown=%
Echo We dont want: [%_endbit%]
::Now remove this from the original string
CALL SET _result=%%_test:%_endbit%=%%
echo %_result%
Based on that formula I've come up with this code:
Code: Select all
for %%a in (*.mov) do (
call :getfilename %%~na
)
:getfilename
set originalfile=%~1
set iteration=%originalfile:*_=%
call set result=%%originalfile:%iteration%=%%
for /f "usebackq tokens=1 delims= " %%T in (`dir "%result%*.flv" ^| findstr File(s^)`) do (
set /a totaliterations=%%T
)
if %totaliterations% GTR 1 dir "%result%*.flv"
goto :eof
The set iteration line accurately gives me the recording iteration as shown in the example output below.
The problem I'm getting is that the Call set is not removing the iteration from the originalfile variable, instead it is removing ALL characters that match the iteration from the variable
Example BATCH File Output
Code: Select all
D:\edit>for %a in (*.mov) do (
call :getfilename %~na
pause
)
D:\edit>(
call :getfilename Paper24652_2
pause
)
D:\edit>set originalfile=Recording24652_2
D:\edit>set iteration=2
D:\edit>call set result=%originalfile:2=%
D:\edit>for /F "usebackq tokens=1 delims= " %T in (`dir "Recording465_*.fbr" | findstr File(s)`) do (set /
a totaliterations=%T )
So as you can see by the time Recording24652_2 gets added to the for /f command at the bottom it has been changed to Recording4605_ . So instead of simply removing the iteration from the originalfile variable (which we get from the set iteration command and in this case is the number 2) the call set command is replacing every character with the iteration (which is not what the SS64 method says it should do).
The code above even if someone helps me figure out how to fix it, is still missing the code to skip anymore .mov files the for loop finds that match the same filename regardless of iteration.
All of that being said.. if someone has a smoother method that doesn't require calling and can all be done in a for loop. Am trying to avoid delayed expansion but I don't have to if a better method can be recommended.
edit: replaced variable names with clearer named variable names.
Thanks!!