gpt4 book ai didi

list - wshShell 和 WMIC 批量协同工作?

转载 作者:行者123 更新时间:2023-12-01 19:39:53 25 4
gpt4 key购买 nike

我试图使用此处找到的方法为批处理文件创建可滚动列表:Scrollable Lists in .bat Files信用 https://stackoverflow.com/users/778560/aacini

我尝试一次在常规批处理的代码中添加几行,我注意到当我使用 WMIC 时该解决方案不起作用。这是什么原因?有简单的解决方案吗?您可以运行下面的代码,然后取消注释 WMIC 行,您会发现它不再起作用。

编辑:我使用的是 Windows 7

谢谢!

@if (@CodeSection == @Batch) @then
@echo off
setlocal EnableDelayedExpansion
color F0


::FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Name /VALUE ^| FIND /I "Name="') DO SET machine=%%A


Echo Reset or Continue
Set MenuOptions=RESET Continue
call :ShowMenu

pause
exit

:ShowMenu
set numOpts=0
for %%a in (%MenuOptions%) do (
set /A numOpts+=1
set "option[!numOpts!]=%%a"
)
rem Clear previous doskey history
doskey /REINSTALL
rem Fill doskey history with menu options
cscript //nologo /E:JScript "%~F0" EnterOpts
for /L %%i in (1,1,%numOpts%) do set /P "var="

rem Send a F7 key to open the selection menu
cscript //nologo /E:JScript "%~F0"
set /P "MenuSelected=Option Selected: "
echo/
@end
var wshShell = WScript.CreateObject("WScript.Shell"),
envVar = wshShell.Environment("Process"),
numOpts = parseInt(envVar("numOpts"));

if ( WScript.Arguments.Length ) {
// Enter menu options
for ( var i=1; i <= numOpts; i++ ) {
wshShell.SendKeys(envVar("option["+i+"]")+"{ENTER}");
}
} else {
// Enter a F7 to open the menu
wshShell.SendKeys("{F7}");
}

最佳答案

SendKeys Method向事件窗口发送一个或多个击键。不幸的是,Sendkeys()有时会失败,通常是由于专注时机,既麻烦又难以解决问题。我试过pausetimeout ,尝试过WshShell.AppActivate各种组合,但都没有成功......

终于找到了清除之前doskey的罪魁祸首历史记录通过doskey /REINSTALL命令:它会安装 doskey 的新副本。我可以推荐更直观(且侵入性较小)的方法来清除 doskey命令历史缓冲区:

doskey /ListSize=0
doskey /ListSize=50

奖励:FOR 循环解释。

  • 外部%%A循环检索name属性(property);
  • 内部%%a循环删除返回值中的结尾回车符:wmic行为:每个输出行以 0x0D0D0A 结尾( <CR><CR><LF> ) 而不是常见的 0x0D0A (<CR><LF>)。

信用:Dave Benham 的 WMIC and FOR /F : A fix for the trailing <CR> problem

工作脚本(Windows 8.1 64 位):

@if (@CodeSection == @Batch) @then
@echo OFF
setlocal EnableDelayedExpansion
REM color F0

FOR /F "tokens=2 delims==" %%A IN ('
WMIC csproduct GET Name /VALUE ^| FIND /I "Name="
') DO FOR %%a in ("%%~A") DO SET "_machine=%%~a"

Echo Reset or Continue %_machine%
Set "MenuOptions=%_machine% RESET Continue" expanded merely for debugging purposes
call :ShowMenu

REM pause
exit /B

:ShowMenu
set numOpts=0
for %%a in (%MenuOptions%) do (
set /A numOpts+=1
set "option[!numOpts!]=%%a"
)
rem Clear previous doskey history
REM this causes problems: doskey /REINSTALL
doskey /ListSize=0
doskey /ListSize=50
rem Fill doskey history with menu options
cscript //nologo /E:JScript "%~F0" EnterOpts
for /L %%i in (1,1,%numOpts%) do set /P "var="

rem Send a F7 key to open the selection menu
cscript //nologo /E:JScript "%~F0"
set /P "MenuSelected=Option Selected: "
echo/
goto :eof
@end
var wshShell = WScript.CreateObject("WScript.Shell"),
envVar = wshShell.Environment("Process"),
numOpts = parseInt(envVar("numOpts"));

if ( WScript.Arguments.Length ) {
// Enter menu options
for ( var i=1; i <= numOpts; i++ ) {
wshShell.SendKeys(envVar("option["+i+"]")+"{ENTER}");
}
} else {
// Enter a F7 to open the menu
wshShell.SendKeys("{F7}");
}

关于list - wshShell 和 WMIC 批量协同工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40165693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com