o_ren
o_ren

Reputation: 812

Windows Batch - Change the beginning of a path but keep the rest

I'm running FFMPEG for video encoding.
I have a batch file which will encode files you drop on it, keeping the file's original name and copy the encoded files to a specific directory.
I would like that script to "know" the original's file path and copy the encoded file to a path relative to it, meaning: original file dropped on batch file is in C:\some\folder\show\season\episode\file.mov
encoded file should be encoded to D:\different\directory\show\season\episode\file.mp4
The part of the path up until \show is fixed.

This is what I have so far:

@echo on

set count=0
for %%a in (%*) do (<br>
  if exist %%a (

        md \\specific\path\"%%~na"

        %MYFILES%\ffmpeg.exe -i "%%~a" -vcodec libx264 -preset medium -vprofile baseline -level 3.0 -b 500k -vf scale=640:-1 -acodec aac -strict -2 -ac 2 -ab 64k -ar 48000 "%%~na_500.mp4"
        copy "%%~na_500.mp4" "\\specific\path\"%%~na"\%%~na_500.mp4"
        copy "%%~na_500.mp4" "\\specific\path\"%%~na"\%%~na_500.mp4"
        del "%%~na_500.mp4"

set /a count+=1

) else ( echo Skipping non-existent %%~a

Thank you,
Oren

Upvotes: 0

Views: 117

Answers (1)

Magoo
Magoo

Reputation: 80023

@ECHO OFF
SETLOCAL
SET "MYFILES=MY_files"
SET "fixed_path=u:\destdir"
FOR %%a IN ("%*") DO IF EXIST "%%a" (

 FOR /f "tokens=3*delims=\" %%b IN ("%%~dpa") DO (
  ECHO(MD "%fixed_path%\%%c"
  ECHO(%MYFILES%\ffmpeg.exe -i "%%~a" -vcodec libx264 -preset medium -vprofile baseline -level 3.0 -b 500k -vf scale=640:-1 -acodec aac -strict -2 -ac 2 -ab 64k -ar 48000 "%%~na_500.mp4"
  ECHO(copy "%%~na_500.mp4" "%fixed_path%\%%c%%~na_500.mp4"
  ECHO(del "%%~na_500.mp4"
 )
 ) ELSE (ECHO(%%~a does NOT EXIST
)


GOTO :EOF

The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)

Similarly, the DEL and COPY commands are merely echoed.

I set myfiles to a constant - no doubt yours will be different and I set fixed _path to a constant for convenience of editing.

No idea whether the ffmpeg command is correct - just edited yours, and I'm puzzled by your use of two apparently identical copy commands in succession, but maybe my eyes are playing tricks...

Upvotes: 0

Related Questions