Home > Software design >  How to create a .bat file to enable or disable a specific audio device with an if statement
How to create a .bat file to enable or disable a specific audio device with an if statement

Time:06-17

With my little knowledge of writing code, I am trying to make a batch file where I can disable or enable a line in audio device with an IF statement.

For example, if the device is currently enabled, when I run the .bat file it will disable. If it's disabled, then it enables it.

I have the the device ID ("SWD\MMDEVAPI{0.0.1.00000000}.{55e90a30-8001-4bc8-af56-52998c03ed88}"). I am currently using pnputil /disable-device which works. But I don't know how to go about the IF statement. Would appreciate some help with that.

Edit: Okay so I found a tool called SoundVolumeView.exe that can lower the sound so the white noise can't be heard on startup. It can also mute as well all through command line.

CodePudding user response:

I wasn't sure if you had solved this yourself already and I know I'm not supposed to spoon feed code on here but I had a script that did something similar and thought I should contribute. If you have any issues with this script, please comment with your issue.

Explaination: it runs pnputil /enum-devices /ids and looks at the output until it gets to a line with the device id. It sets a variable called ahead to 4. If ahead is above 0, then it will check if it is 0, if it isn't then it will go to the next line in the output of the command. If it is 0, then it will go to the toggle label and then it will check the line if it doesn't say Started. If it doesn't have Started in the line, then it will enable your device, if it does say started, then it will disable it. Then it just pauses and exits.

@echo off
setlocal enableDelayedExpansion
REM Needs to be run as administrator to work

set device=SWD\MMDEVAPI{0.0.1.00000000}.{55e90a30-8001-4bc8-af56-52998c03ed88}

set ahead=-1

for /f "delims=" %%a in ('pnputil /enum-devices /ids') do (
  set "line=%%a"
  if !ahead! GEQ 0 (
    if !ahead!==0 goto :toggle
    set /a ahead-=1
  ) else (
    if not "x!line:           
  • Related