A good solution is to store the switch in an ADS
(alternative data stream) in the bat itself (will work only if your file system is NTFS
):
You just will get a file not found
warning at the first run while it can find the ADS
stream but after that it will be created
@echo off
set "$activ="
set /p $activ=<%~nx0:activ
if not defined $activ (
echo speaker>%~nx0:activ
set "$activ=speaker"
)
echo actual [%$activ%]
if /i "%$activ%"=="speaker" (
nircmd.exe setdefaultsounddevice "Headset"
echo headset>%~nx0:activ
) else (
nircmd.exe setdefaultsounddevice "Speakers"
echo speaker>%~nx0:activ
)
EDIT : Thanks @aschipfl
here is the solution to avoid the first run Warning
@echo off
set "$activ="
2> nul (set /P $activ= < "%~nx0:activ") || set "$activ=speaker"
echo actual [%$activ%]
if /i "%$activ%"=="speaker" (
nircmd.exe setdefaultsounddevice "Headset"
echo headset>%~nx0:activ
) else (
nircmd.exe setdefaultsounddevice "Speakers"
echo speaker>%~nx0:activ
)
very easy and don't need any temp file and don't have call limitation