Running cmd command built in a variable Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dethkiller15
Posts: 4
Joined: 14 Oct 2016, 22:43

Running cmd command built in a variable

Post by Dethkiller15 » 05 Dec 2022, 16:41

I am working on making my script a bit more efficient by having it launch a single window instead of multiple, for instance. I am trying to make changes to my DNS config. on a bunch of diffrent adapters the base command is roughly.

Code: Select all

netsh interface ipv4 add dns name=`"%Adapter%`" addr=`"%IP%`" index=%IPv4IndexVar%
works with runwait. You will see more examples without variables below.

So each command would be needed to be ran in separate windows(runwait). as I have my code now that is.
example using cloudflare DNS:

Code: Select all

netsh interface ipv4 set dns name="Wi-Fi" source="static" address="1.1.1.1"
netsh interface ipv4 add dns name="Wi-Fi" addr="1.0.0.1" index=2
netsh interface ipv6 set dns name="Wi-Fi" source="static" address="2606:4700:4700::1111"
netsh interface ipv6 add dns name="Wi-Fi" addr="2606:4700:4700::1001" index=2
netsh interface ipv4 set dns name="Ethernet" source="static" address="1.1.1.1"
netsh interface ipv4 add dns name="Ethernet" addr="1.0.0.1" index=2
netsh interface ipv6 set dns name="Ethernet" source="static" address="2606:4700:4700::1111"
netsh interface ipv6 add dns name="Ethernet" addr="2606:4700:4700::1001" index=2
ipconfig /flushdns
I know I could throw them all into a single command with each one seperated with a "&" instead of a "`n" but for some reason it does not work when using

Code: Select all

runwait *RunAs %A_ComSpec% /c "%Command%",,Hide
example using cloudflare for 1 single command.

Code: Select all

netsh interface ipv4 set dns name="Wi-Fi" source="static" address="1.1.1.1" & netsh interface ipv4 add dns name="Wi-Fi" addr="1.0.0.1" index=2 & netsh interface ipv6 set dns name="Wi-Fi" source="static" address="2606:4700:4700::1111" & netsh interface ipv6 add dns name="Wi-Fi" addr="2606:4700:4700::1001" index=2 & netsh interface ipv4 set dns name="Ethernet" source="static" address="1.1.1.1" & netsh interface ipv4 add dns name="Ethernet" addr="1.0.0.1" index=2 & netsh interface ipv6 set dns name="Ethernet" source="static" address="2606:4700:4700::1111" & netsh interface ipv6 add dns name="Ethernet" addr="2606:4700:4700::1001" index=2 & ipconfig /flushdns
the above command is in the "Command" variable when it runs but its just giving crazy errors in CMD while it is being ran and the errors will not stop.

What would I need to do/change in order to make this function?

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Running cmd command built in a variable  Topic is solved

Post by mikeyww » 05 Dec 2022, 16:48

You could create a Windows batch file, and then use AHK to run it.

Other ideas:

viewtopic.php?p=494113#p494113

viewtopic.php?t=74647

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Running cmd command built in a variable

Post by wetware05 » 05 Dec 2022, 18:43

hi Dethkiller15 and mikeyww.

Check out my script at viewtopic.php?f=76&t=111177 I had the same problem, a command line executable (es.exe) wouldn't let me put variables. Failing that, I created the script to generate a .bat file, which would contain the variables needed in each loop.

Luck! :thumbup:

Dethkiller15
Posts: 4
Joined: 14 Oct 2016, 22:43

Re: Running cmd command built in a variable

Post by Dethkiller15 » 05 Dec 2022, 22:32

mikeyww wrote:
05 Dec 2022, 16:48
You could create a Windows batch file, and then use AHK to run it.

Other ideas:

viewtopic.php?p=494113#p494113

viewtopic.php?t=74647
Well the point of this is to not use a bat script as that does add some delay to create the file and I don't want to use my drive for anything but storing the script. I did try both those functions you linked and RunCMD("cmd /c" Command) Worked. Thank you sir.

Post Reply

Return to “Ask for Help (v1)”