How to make a toggle switch in batch?

I need a batch file that will toggle between running two command lines;

One being

nircmd.exe setdefaultsounddevice "Speakers"

The other being

nircmd.exe setdefaultsounddevice "Headset"

Ive tried using the existence of a .txt file as a flag, but for some reason it just always sees it as not existing. How do you make a batch file (silently) tick/tock every time it runs?

You can store the toggle value in the Batch file itself in a very simple way:

@echo off
setlocal

rem Get current value and update it
call :GetToggle
set /P "=+1" < nul >> "%~F0"
set /A "toggle%%=2"

if %toggle% equ 0 (
   echo Set "Speakers"
) else (
   echo Set "Headset"
)

goto :EOF

:GetToggle
rem Be sure that next line does NOT end in CR+LF:
set /A toggle=0

This method works correctly for a little less than 4096 times. If the Batch file run once everyday, this covers more than 11 years! If this is not enough for you, just add an if command after get the toggle value to check if it exceeds 4090, and in such a case insert a breaking CR+LF characters at end of the file followed by a new line with set /A toggle=0

Note that this method also allows to know how many times the Batch file had been used, so it may be used in other scenario...

Make a toggle "switch" in batch file, Here is a little batch code to toggle hidden attribute of a specified directory using the technique as explained on question How to get attributes  Batch file to toggle between balanced and high performance power plan: Is it possible to toggle between balanced and high performance power plan using a batch file ? powercfg -s 381b4222-f694-41f0-9685-ff5bb260df2e and powercfg -s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c When set to balanced, toggle to high performance and when set to high

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

How to make a toggle switch in batch file ? Solved, How to make a toggle switch in batch file ? How to toggle between this: wmic process where name="ScpService.exe" CALL setpriority "high  Batchography: How to do a “switch/case” in Batch files You have found this blog post because you are wondering if there is a way to express a “ switch/case ” logic in Batch files. The short answer is: “no, not exactly”.

How to make a toggle switch in batch file ?, How to make a toggle switch in batch file ?:How to toggle between this: wmic process where name="ScpService.exe" CALL setpriority "high  How to wire a toggle switch , "ON /OFF " switch basic. Applies to spot switches, non led switches, basic 2 wire switches (2 prong) . Generally used in 12volt installs for on off switches.

How to make a toggle switch in batch file ? - Windows, How to toggle between this: wmic process where name=ScpService.exe CALL setpriority high priority and this: wmic process where  Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete

Question, How to toggle between this: wmic process where name="ScpService.exe" CALL setpriority "high priority" and this: wmic process where  Writing a simple batch file. To create a simple batch file on Windows 10, use these steps: Open Start. Search for Notepad, and click the top result to launch the app. Type the following lines to create a simple batch file: @ECHO OFF ECHO Congratulations! Your first batch file executed successfully.

Batch file toggle actions, There is. Using this post on stack overflow as an example I get: (Create a file named first_run) copy con first_run 42 ^z. (Create a batch file  Batch file - check if program is running and switch to it « on: December 14, 2008, 10:04:20 PM » Are there commands to check if a program is already running, and give an external program focus?

Comments
  • Edit your question to include the code you tried, which uses the existence of a .txt file as a flag, but always sees it as not existing. We cannot help you with your failing code if we cannot see it, and not posting it means you're expecting somebody to write the script for you, (which is rude and off-topic).
  • how would this code work if it does not contain the 'nircmd.exe setdefaultsounddevice' command?
  • @KalamalkaKid: Well, this code solves the original question, that is "How to make a toggle switch in batch?". You need to insert the appropriate nircmd.exe setdefaultsounddevice commands at the right places... Hint: the nircmd.exe setdefaultsounddevice "Speakers" command goes in the line that display Set "Speakers", and nircmd.exe setdefaultsounddevice "Headset" command goes in the line that display Set "Headset"
  • It would be very useful if you simply edited your answer to include the information so people like me who don't know very much about coding can simply change the name of the device. I still don't know where to put those lines. Thanks!
  • does nircmd.exe setdefaultsounddevice "Speakers" go after echo set or before? please help
  • Ok ignore me then. I'll find help somewhere else.
  • I just was about to suggest to use if exist for the very first run, but then I found that this does not work for ADSs, unfortunately. What you could do though is: 2> nul (set /P $activ= < "%~nx0:activ") || set "$activ=speaker". Anyway, nice approach!
  • Thanks @aschipfl ! Better and Cleaner ! Edited with your solution too!
  • this works great but only works once. Any ideas why?