user3106266
user3106266

Reputation: 15

Windows Batch Script - FOR DO one command at a time

I made a batch script to find .flv and .mp4 files in subdirectories and create a template .srt file with the same name in that subfolder.

The problem is, I use a FOR DO loop and it seems to spit the commands out too fast for it to be reliable..

Heres the code:

for /R %%f in (*.flv) do (
cd  %%~nf
echo.1>> "%%~nf".srt
echo.00:00:00,500 --^> 00:00:03,500>> "%%~nf".srt
echo.%%~nf>> "%%~nf".srt
echo.>> "%%~nf".srt
echo.2>> "%%~nf".srt
echo.00:00:00,000 --^> 00:00:00,000>> "%%~nf".srt
echo.%%~nf>> "%%~nf".srt
cd..
)
for /R %%f in (*.mp4) do (
cd  %%~nf
echo.1>> "%%~nf".srt
echo.00:00:00,500 --^> 00:00:03,500>> "%%~nf".srt
echo.%%~nf>> "%%~nf".srt
echo.>> "%%~nf".srt
echo.2>> "%%~nf".srt
echo.00:00:00,000 --^> 00:00:00,000>> "%%~nf".srt
echo.%%~nf>> "%%~nf".srt
cd..
)

If all goes well.. for ./FILE1/FILE1.mp4 it would make ./FILE1/FILE1.srt that contains:

1
00:00:00,500 --> 00:00:03,500
FILE1

2
00:00:00,000 --> 00:00:00,000
FILE1

which it does but only for the first few files, then the process seems to go too fast and I get doubles and the wrong titles in the wrong srt files.

It seems like the do ( ... ) loop is spitting out the commands all at the same time. I've even tried to slow it down using PING commands and even tried GOTO commands to break up the work into steps but no luck.

How can I get it to execute one command at a time while still being able to use %%~nf to create and label the .srt files??

Any help is greatly appreciated!

Upvotes: 1

Views: 489

Answers (2)

MC ND
MC ND

Reputation: 70923

"Probably" you are having some kind of problems with nested subdirectories or spaces in directories names and your cd %~nf cd .. logic. Change to cd "%~dpf" to change to the directory where the file resides. Or better, don't change directories and use full paths in every file access.

for /R %%f in (*.flv *.mp4) do (
    echo(1
    echo(00:00:00,500 --^> 00:00:03,500
    echo(%%~nf
    echo(
    echo(2
    echo(00:00:00,000 --^> 00:00:00,000
    echo(%%~nf
) > "%%~dpnf.srt"

Upvotes: 2

Infinite Recursion
Infinite Recursion

Reputation: 6557

This should work:

@echo off
for /F "delims==" %%f in ('dir /s /b /o:gn *.flv *.mp4') DO ( 
echo.1> "%%~dpnf.srt"
echo.00:00:00,500 --^> 00:00:03,500>> "%%~dpnf.srt"
echo.%%~nf>> "%%~dpnf.srt"
echo.>> "%%~dpnf.srt"
echo.2>> "%%~dpnf.srt"
echo.00:00:00,000 --^> 00:00:00,000>> "%%~dpnf.srt"
echo.%%~nf>> "%%~dpnf.srt"
)

Upvotes: 0

Related Questions