how to pass a variable to batch

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

how to pass a variable to batch

01 Mar 2021, 10:56

I have a batch file, where I check if a computer is used, now I want to pass a variable vComputer in AHK when I run the batch file.
Run, %comspec% /k test1.cmd, %A_WorkingDir%

the batch file is:

Code: Select all

@echo off
TITLE Computer used by
cls
mode 400,35
color 5
query user /server:vComputer | findstr "Active"
IF %ERRORLEVEL% EQU 0 (
call 
) else (
echo ^>^> The computer is free for use.
)
echo.
pause
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: how to pass a variable to batch

01 Mar 2021, 10:59

Code: Select all

RunWait, %ComSpec% /k "test1.cmd %vComputer%", %A_WorkingDir%
A batch file argument would be like %1 in the batch file.

If this is a variable from a GUI, then the initial "v" in the options is not part of the variable name.
need4speed
Posts: 143
Joined: 22 Apr 2016, 06:50

Re: how to pass a variable to batch

01 Mar 2021, 17:51

thanks for taking the time to answer, your solution did not work (or maybe I don't understand...)
the work-around by creating the batch file via FileAppend then run it, works for me. thanks anyway.

Code: Select all

vComputer := "Samantha"

FileAppend,
(
@echo off
TITLE Computer used by
cls
mode 400,35
color 5
query user /server:%vComputer% | findstr "Active"
IF %ERRORLEVEL% EQU 0 `(
call 
`) else `(
echo ^>^> The computer is free for use.
`)
echo.
pause
), d:\test1.cmd
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: how to pass a variable to batch

01 Mar 2021, 18:09

I'm glad it worked.

Below is an example in case you need it in the future.

AHK:

Code: Select all

vComputer = Samantha
Run, %A_ScriptDir%\test.cmd "%vComputer%"
CMD:

Code: Select all

@echo off
echo:
echo %~1
echo:
Pause
lexikos
Posts: 9665
Joined: 30 Sep 2013, 04:07
Contact:

Re: how to pass a variable to batch

02 Mar 2021, 04:52

Environment variables are normally inherited automatically by child processes. In other words, just set the environment variable within the script, then run cmd.exe.

Code: Select all

EnvSet vComputer, %vComputer%
User avatar
mikeyww
Posts: 27214
Joined: 09 Sep 2014, 18:38

Re: how to pass a variable to batch

02 Mar 2021, 07:04

Thank you, @lexikos!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], KilliK, mikeyww and 111 guests