Simple help with batch file variables

vonsworld

Distinguished
Nov 30, 2012
26
0
18,540
Hi

I'd like to edit an exisitng variable within a batch file to create a new variable...

For example if the variable %input% has previously been set to a value "Test Video.mp4" how can I create a variable %output% that contains "Test Video.Stereo.mp4"

In other words I would like a batch command that takes the variable %input% and inserts .stereo four characters from the left, then saves the result as variable %output%

Thanks for your advice


at a
 

vonsworld

Distinguished
Nov 30, 2012
26
0
18,540
Thanks that works fine.

I'd like to perform this action for every .mp4 file in a particular folder using a loop, so with your suggestion I have written the following:

for %%x in (*.mp4) do set "input=%%x"
set output=%input:~0,-4%.Stereo%input:~-4%
echo %output%

(In the final version I would add a command to do something useful like an audio conversion on each file)

However in the above example the loop only occurs in the first line. The batch file finds the last .mp4 file in the folder (which ends the loop) and then performs the SET Output and ECHO commands on that only.

How can I alter the syntax please so that all three lines are performed within the loop for every .mp4 in the folder?

Thanks :)
 

Sylvvester

Distinguished
Nov 22, 2010
160
0
18,760
You use brackets ( ) to execute multiple commands and you need "setlocal enabledelayedexpansion" and !var! to use SET in a for loop.
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for %%x in (*.mp4) do (
set "input=%%x"
set output=!input:~0,-4!.Stereo!input:~-4!
echo !output!
)