The importance of “EnableDelayExpansion” in batch

So, i was trying to make my laptop work as a hot spot.Pretty standard stuff by reading all those tutorials on the internet. I thought “i can do better than that”, so i made a simple batch file to execute the commands needed (for example to set your network parameters or close/open the network).

The code is pretty much straightforward and easy , too.Υou can download the code from github. My batch skills were a little bit rusty, but with a little effort it was done in no time. When I thought it was finished , I tried to test it. This is where the confusion begins. When i tried to set my network parameters through the script, i always got a blank SSID  and of course a wrong password. The piece of code responsible for that :

::Set the SSID and Password of the Hosted Network::
If %_option%==2 ( set /p _SSID=Enter the name of your Network :
     set /p _password=Enter the password of your Network :
     netsh wlan set hostednetwork mode=allow ssid=%_SSID% key=%_password%
     goto Start)

The confusion continued when i tried to set my parameters again. For some reason i would get the previously set parameters. Finally it hit me. There was an issue in the update of the parameters. They would get the right value, but not right away.  So I did what i always do , search for a solution to that issue. By reading the command line reference i stumbled upon this command. So the solution was simple. The only thing needed was to enable the Delayed Expansion and modify the previous code to look like this :

::Set the SSID and Password of the Hosted Network::
If %_option%==2 ( set /p _SSID=Enter the name of your Network :
     set /p _password=Enter the password of your Network :
     netsh wlan set hostednetwork mode=allow ssid=!_SSID! key=!_password!
     goto Start)

Problem Solved!

 

 

Leave a comment