如何使用Batch在某些参数中使用变量?

时间:2017-03-08 17:59:01

标签: batch-file

::Set the command to cmd
set cmd="wmic useraccount where name='%username%' get sid"

::Set the output to variable User
set "User="
for /f "skip=1 delims=" %%i in ('%cmd%') do if not defined User set "User=%%i"

::Output the variable
echo %User%

::This part doesn't work
::Change registry key, using variable as placeholder
reg add "HKEY_USERS\%User%\example" /v exampleKey /d "1" /t REG_DWORD /f

pause >nul

我认为这会奏效,但显然我做错了什么。我明白了:
错误:参数不正确。

1 个答案:

答案 0 :(得分:0)

如果有充分的理由写入该密钥而不是映射的HKCU密钥,那么我会这样做:

@Echo Off
For /F "Skip=1 Delims=" %%A In (
    '"WMIC UserAccount Where (Name='%UserName%') Get SID"') Do For %%B In (%%A
) Do Reg Add "HKU\%%B\Example" /V ExampleKey /T Reg_DWord /D 1 /F>Nul