milowas
milowas

Reputation: 23

batch file: assign decimal value to variable

This script is run from within a directory of OGG files.

cd c:\dirWithOGG
for %%f in (*.ogg) do (
sox %%f %%f pad 0 "soxi -D %%f"
sox %%f %%f repeat 10
)

First time I've had to create a batch script, I thought it's going to easy.

soxi -D %%f

On it's own, the above returns a decimal value. For some reason, it's doesn't execute from within the script. sox and soxi are in a location added to PATH.

Any help appreciated, thanks.

Upvotes: 2

Views: 826

Answers (3)

Andriy M
Andriy M

Reputation: 77717

If you want to use the output of soxi -D %%f as a command line parameter for sox, here's how you can do that:

FOR %%f IN (*.ogg) DO CALL :runsox "%%f"
GOTO :EOF

:runsox
soxi -D %1>tmpfile
SET /P decvalue=<tmpfile
sox %1 %1 pad 0 %decvalue%
sox %1 %1 repeat 10

As you can see, the commands inside the loop have been moved to a subroutine that the loop is calling now. Other than that, the output of soxi is redirected to a file, which then is read into a variable, which in turn is used to pass the value to sox.

It's actually because of the variable that I had to move the commands outside the loop. It would work incorrectly inside the loop unless delayed expansion was enabled and a different syntax was used to address the variable. I preferred it implemented as above, though.

Upvotes: 1

D.Shawley
D.Shawley

Reputation: 59613

Try expanding the FOR in your head (or on paper):

FOR %%F IN (*.ogg) DO (
  sox %%F %%F pad 0 "soxi -D %%F"
  sox %%F %%F repeat 10
)

If you have one .ogg file, say foo.ogg, then the FOR loop would cause the following two statements to be executed.

sox foo.ogg foo.ogg pad 0 "soxi -D foo.ogg"
sox foo.ogg foo.ogg repeat 10

I don't think that the first line is going to do what you expect. It will run sox and pass the remainder of the line as parameters. It will not execute soxi -D foo.ogg and replace it with its output. DOS doesn't work this way at all. Try something like:

SETLOCAL EnableExtensions
FOR %%F IN (*.ogg) DO (
  FOR /F %%N IN ('soxi -D %%F') DO (
    sox %%F %%F pad 0 "%%N"
    sox %%F %%F repeat 10
  )
)
ENDLOCAL

That might work. I don't have access to a DOS box so I might have the inner FOR syntax wrong. You may have to specify the subcommand with backticks or double quotes. The enhanced version of the FOR loop requires command extensions to be enabled IIRC. Try typing FOR /? at the command prompt or give http://www.computerhope.com/forhlp.htm a read for more information.

Upvotes: 0

Eli Acherkan
Eli Acherkan

Reputation: 6411

If I remember correctly, for requires the action to be specified on the same line. Try the following:

for %%f in (*.ogg) do sox %%f %%f pad 0 "soxi -D %%f" && sox %%f %%f repeat 10

Upvotes: 0

Related Questions