Cycling sounds - please check my syntax

D

David Trimboli

I'm trying to create a batch file that will cycle sound files - for
instance, changing the e-mail sound every time someone logs in.

Here's the script so far:


:-------------------------
REM @echo off
setlocal enableextensions
set sounddir=c:\sounds
set soundchosen=no

for /f "tokens=*" %%i in (%sounddir%\soundindex.txt) do (
if %soundchosen% equ no (
copy /y %sounddir%\%%i %sounddir%\mysound.wav
set lastsound=%%i
set soundchosen=yes
) else (

copy /y soundindex.tmp soundindex.txt
del soundindex.tmp
:-------------------------


In %sounddir% there are a series of .wav files. soundindex.txt contains a
list of all the .wav files in %sounddir%. The script is supposed to take
the first name on the list, copy the corresponding sound to
%sounddir%\mysound.wav, then rewrite the list, putting the new sound at the
bottom of the list. %sounddir%\mysound.wav will be the file the event is
set to. Here's the output I get when I run the script:


:-------------------------
C:\Sounds>sounds

C:\Sounds>REM @echo off

C:\Sounds>setlocal enableextensions

C:\Sounds>set sounddir=c:\sounds

C:\Sounds>set soundchosen=no

C:\Sounds>
C:\Sounds>
:-------------------------

Here are some sample files and the index file that cause this to happen:


-------------------------
C:\sounds>dir
Volume in drive C is System
Volume Serial Number is 103F-22AB

Directory of C:\sounds

05/24/2004 10:11a <DIR> .
05/24/2004 10:11a <DIR> ..
06/20/2003 08:00a 15,906 ir_begin.wav
06/20/2003 08:00a 42,728 ir_end.wav
06/20/2003 08:00a 75,508 ir_inter.wav
05/24/2004 10:11a 38 soundindex.txt
4 File(s) 134,180 bytes
2 Dir(s) 31,126,949,888 bytes free

C:\sounds>type soundindex.txt
ir_begin.wav
ir_end.wav
ir_inter.wav
C:\sounds>
-------------------------


Can someone tell me why this script stops at the FOR statement?

David
Stardate 4395.8
 
D

David Trimboli

Oh. Heh. I missed the second ")" after the ELSE clause.

David
Stardate 4396.1
 
D

David Trimboli

Oy. That first script was a real mess, hmm? Okay, I've got it working, and
here it is for anyone who's interested (or who would like to show me an
easier way to do this):


@echo off
setlocal enableextensions enabledelayedexpansion
set sounddir=c:\sounds
set soundchosen=no

for /f "usebackq tokens=*" %%i in ("%sounddir%\soundindex.txt") do (
if !soundchosen! equ no (
copy /y "%sounddir%\%%i" "%sounddir%\mysound.wav"
set lastsound=%%i
set soundchosen=yes
) else (

copy /y "%sounddir%\soundindex.tmp" "%sounddir%\soundindex.txt"
del "%sounddir%\soundindex.tmp"




David
Stardate 4396.1
 
M

Matthias Tacke

David Trimboli said:
Oy. That first script was a real mess, hmm? Okay, I've got it
working, and here it is for anyone who's interested (or who would like
to show me an easier way to do this):
For the cycle part, this will also do it:

::CycleSound.cmd::::::::::::::::::::::::::::::::::::::::::::.::::::::::
@echo off & setlocal
set Act=C:\Sound\SoundCycle.txt
set Old=%Act:~0,-4%.bak
SET /P First=<"%Act%"
Copy /Y "%Act%" "%Old%" >NUL

What you do else with the contents of %First% is up to you :)

For more general use, this batch expects a file argument:
::Cycle.cmd::::::::::::::::::::::::::::::::::::::::::::.::::::::::
@echo off & setlocal
if "%~1" EQU "" echo Usage: %0 Filname.ext & goto :eof
if Not Exist "%~1" echo %1 doesn't exist & goto :eof
SET /P First=<"%~f1"
Copy /Y "%~f1" "%~dpn1.Bak" >NUL
 
D

David Trimboli

Matthias Tacke said:
::CycleSound.cmd::::::::::::::::::::::::::::::::::::::::::::.::::::::::
@echo off & setlocal
set Act=C:\Sound\SoundCycle.txt
set Old=%Act:~0,-4%.bak
SET /P First=<"%Act%"
Copy /Y "%Act%" "%Old%" >NUL

Sweet! I didn't realize you could redirect a text file into a command like
that. It makes sense, though: when the first carriage return is hit, the
SET has its input, and the rest is ignored. I also didn't think of the +n
parameter for MORE. Thanks!

David
Stardate 4396.3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top