【问题标题】:Create database in Batch file with SET, DOSKEY, etc使用 SET、DOSKEY 等在批处理文件中创建数据库
【发布时间】:2017-10-22 17:51:04
【问题描述】:

我是新来的,很抱歉我的英语和帖子中的错误。

我需要帮助在批处理文件中设置变量 -> 并不像你想象的那么容易。 ..我的批处理文件中的示例:

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%

但只有我得到的是 compmgmt.msc /computer:\pc1

所以,我的问题是设置不起作用(或者我使用它不好),doskey,setlocal.. 也不起作用。

我不想使用选择和错误级别,因为我说的是 600 台计算机。所以我需要创建一个“小型”数据库,但我想在 txt (.bat) 中完成

【问题讨论】:

  • 使用 compmgmt.msc /computer:\\!%pc%! 并启用延迟扩展。见this answer

标签: database batch-file cmd set


【解决方案1】:

您实际上是在尝试对变量进行双重扩展。所以你有两个选择。

使用延迟扩展

@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%

使用调用集

@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%

【讨论】:

  • call这一行少了一个百分号,应该是:call set pc=%%%pc%%%
猜你喜欢
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-07-21
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多