1

I'm new here, so sorry for my english and mistakes in post.

I need help to set variable in batch file -> not so easy as you think. ..example from my batch file:

rem set shortcut for long pc name
set pc1=LongNameOfMyPcNo.1
set pc2=LongNameOfMyPcNo.2
set pc3=LongNameOfMyPcNo.3

rem now, user can type "pc1" and in compmgmt line he get long name
set /p pc=type shortcut name

rem in next line i want get compmgmt.msc /computer:\\LongNameOfMyPcNo.1

compmgmt.msc /computer:\\%pc%

But only what i get is compmgmt.msc /computer:\pc1

So, my problem is that set not working (or i using it bad), doskey, setlocal.. also not working.

I don't want use choice and errorlevel cause i'm talking about 600 computers. So i need to create a "small" database, but i want to do it in txt (.bat)

bbc-s
  • 13
  • 2
  • 1
    Use `compmgmt.msc /computer:\\!%pc%!` with Delayed Expansion Enabled. See [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Oct 22 '17 at 18:23

1 Answers1

0

You are essentially trying to get a double expansion of a variable. So you have two options.

Use Delayed Expansion

@echo off
setlocal enabledelayedexpansion
rem set shortcut for long pc name
set pc1=LongNameOfMyPcNo.1
set pc2=LongNameOfMyPcNo.2
set pc3=LongNameOfMyPcNo.3

rem now, user can type "pc1" and in compmgmt line he get long name
set /p pc=type shortcut name
set pc=!%pc%!

rem in next line i want get compmgmt.msc /computer:\\LongNameOfMyPcNo.1

compmgmt.msc /computer:\\%pc%

Use CALL SET

@echo off
rem set shortcut for long pc name
set pc1=LongNameOfMyPcNo.1
set pc2=LongNameOfMyPcNo.2
set pc3=LongNameOfMyPcNo.3

rem now, user can type "pc1" and in compmgmt line he get long name
set /p pc=type shortcut name
call set pc=%%%pc%%%

rem in next line i want get compmgmt.msc /computer:\\LongNameOfMyPcNo.1

compmgmt.msc /computer:\\%pc%
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • 1
    There is one percent sign missing in the `call` line, it should be: `call set pc=%%%pc%%%` – Aacini Oct 22 '17 at 18:26