有没有一种方法可以显示批处理文件中的消息框(类似于xmessage
Linux中bash脚本中的用法)?
有没有一种方法可以显示批处理文件中的消息框(类似于xmessage
Linux中bash脚本中的用法)?
Answers:
我将制作一个非常简单的VBScript文件,并使用CScript调用它来解析命令行参数。
类似于以下内容的文件保存在MessageBox.vbs
:
Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox messageText
您会这样称呼:
cscript MessageBox.vbs "This will be shown in a popup."
首先,DOS与它无关,您可能需要Windows命令行解决方案(再次:无DOS,纯Windows,不是窗口,而是控制台)。
您可以使用boflynn提供的VBScript方法,也可以误用net send
或msg
。 net send
仅适用于旧版Windows:
net send localhost Some message to display
但是,这也取决于Messenger服务的运行。
对于较新的版本(显然是XP及更高版本):
msg "%username%" Some message to display
请注意,使用发送的消息框msg.exe
仅持续60秒。但是,可以使用/time:xx
开关覆盖它。
可能会显示一点闪光,但不需要临时文件。应该一直工作到(IIRC)IE5时代的某个地方。
mshta javascript:alert("Message\n\nMultiple\nLines\ntoo!");close();
if 1 == 1 (
mshta javascript:alert^("1 is equal to 1, amazing."^);close^(^);
)
close() was unexpected at this time
。
^
蝙蝠文件中的那些转义字符
这将弹出另一个命令提示符窗口:
START CMD /C "ECHO My Popup Message && PAUSE"
start cmd /c "@echo off & mode con cols=18 lines=2 & echo My Popup Message & pause>nul"
将更cols=18
改为message + 2中的字符数。并且lines=2
无论行数是+1。
cmd /k
尝试:
Msg * "insert your message here"
如果您使用的是Windows XP的command.com,将打开一个消息框。
我搜集到,打开一个新的cmd窗口并不是您所要的。您也可以使用VBScript,并将其与.bat文件一起使用。您可以使用以下命令从bat文件中打开它:
cd C:\"location of vbscript"
这样做是更改目录command.com将在其中搜索文件,然后在下一行:
"insert name of your vbscript here".vbs
然后创建一个新的记事本文档,输入
<script type="text/vbscript">
MsgBox "your text here"
</script>
然后,您可以将其另存为.vbs文件(通过在文件名的末尾添加“ .vbs”),在文件名下方的下拉框中另存为“所有文件”(这样就不会另存为.txt) ),然后点击保存!
<script/>
标签。
msg *
,将提示您输入消息,然后按ctrl-Z。您可以在此处输入将出现在消息中的换行符。
很少的其他方法。
1)最怪异,最讨厌-它使用IEXPRESS创建小型exe文件,该文件将通过一个按钮创建弹出窗口(它可以创建两种以上的弹出消息)。适用于XP及以上版本的每个窗口:
;@echo off
;setlocal
;set ppopup_executable=popupe.exe
;set "message2=click OK to continue"
;
;del /q /f %tmp%\yes >nul 2>&1
;
;copy /y "%~f0" "%temp%\popup.sed" >nul 2>&1
;(echo(FinishMessage=%message2%)>>"%temp%\popup.sed";
;(echo(TargetName=%cd%\%ppopup_executable%)>>"%temp%\popup.sed";
;(echo(FriendlyName=%message1_title%)>>"%temp%\popup.sed"
;
;iexpress /n /q /m %temp%\popup.sed
;%ppopup_executable%
;rem del /q /f %ppopup_executable% >nul 2>&1
;pause
;endlocal
;exit /b 0
[Version]
Class=IEXPRESS
SEDVersion=3
[Options]
PackagePurpose=InstallApp
ShowInstallProgramWindow=1
HideExtractAnimation=1
UseLongFileName=0
InsideCompressed=0
CAB_FixedSize=0
CAB_ResvCodeSigning=0
RebootMode=N
InstallPrompt=%InstallPrompt%
DisplayLicense=%DisplayLicense%
FinishMessage=%FinishMessage%
TargetName=%TargetName%
FriendlyName=%FriendlyName%
AppLaunched=%AppLaunched%
PostInstallCmd=%PostInstallCmd%
AdminQuietInstCmd=%AdminQuietInstCmd%
UserQuietInstCmd=%UserQuietInstCmd%
SourceFiles=SourceFiles
[SourceFiles]
SourceFiles0=C:\Windows\System32\
[SourceFiles0]
%FILE0%=
[Strings]
AppLaunched=subst.exe
PostInstallCmd=<None>
AdminQuietInstCmd=
UserQuietInstCmd=
FILE0="subst.exe"
DisplayLicense=
InstallPrompt=
2)使用MSHTA
。也可以在XP及更高版本的每台Windows机器上运行(尽管OP不想使用“外部”语言,但此处的JavaScript最小化了)。应另存为.bat
:
@if (true == false) @end /*!
@echo off
mshta "about:<script src='file://%~f0'></script><script>close()</script>" %*
goto :EOF */
alert("Hello, world!");
或一行:
mshta "about:<script>alert('Hello, world!');close()</script>"
要么
mshta "javascript:alert('message');close()"
要么
mshta.exe vbscript:Execute("msgbox ""message"",0,""title"":close")
3)这是参数.bat/jscript
化的Hybrid(应另存为bat
)。尽管有OP请求,它仍然使用JavaScript,但由于它是蝙蝠,因此可以毫无疑问地称为bat文件。它使用POPUP,它比更流行的MSGBOX允许更多的控制。它使用WSH,但不像上面的示例那样使用MSHTA。
@if (@x)==(@y) @end /***** jscript comment ******
@echo off
cscript //E:JScript //nologo "%~f0" "%~nx0" %*
exit /b 0
@if (@x)==(@y) @end ****** end comment *********/
var wshShell = WScript.CreateObject("WScript.Shell");
var args=WScript.Arguments;
var title=args.Item(0);
var timeout=-1;
var pressed_message="button pressed";
var timeout_message="timed out";
var message="";
function printHelp() {
WScript.Echo(title + "[-title Title] [-timeout m] [-tom \"Time-out message\"] [-pbm \"Pressed button message\"] [-message \"pop-up message\"]");
}
if (WScript.Arguments.Length==1){
runPopup();
WScript.Quit(0);
}
if (args.Item(1).toLowerCase() == "-help" || args.Item(1).toLowerCase() == "-h" ) {
printHelp();
WScript.Quit(0);
}
if (WScript.Arguments.Length % 2 == 0 ) {
WScript.Echo("Illegal arguments ");
printHelp();
WScript.Quit(1);
}
for (var arg = 1 ; arg<args.Length;arg=arg+2) {
if (args.Item(arg).toLowerCase() == "-title") {
title = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-timeout") {
timeout = parseInt(args.Item(arg+1));
if (isNaN(timeout)) {
timeout=-1;
}
}
if (args.Item(arg).toLowerCase() == "-tom") {
timeout_message = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-pbm") {
pressed_message = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-message") {
message = args.Item(arg+1);
}
}
function runPopup(){
var btn = wshShell.Popup(message, timeout, title, 0x0 + 0x10);
switch(btn) {
// button pressed.
case 1:
WScript.Echo(pressed_message);
break;
// Timed out.
case -1:
WScript.Echo(timeout_message);
break;
}
}
runPopup();
4)和一个 jscript.net/.bat
混合文件(应另存为.bat
)。这一次它使用.NET
并编译了一个.exe
可以删除的小文件:
@if (@X)==(@Y) @end /****** silent jscript comment ******
@echo off
::::::::::::::::::::::::::::::::::::
::: compile the script ::::
::::::::::::::::::::::::::::::::::::
setlocal
::if exist "%~n0.exe" goto :skip_compilation
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
if exist "%%v\jsc.exe" (
rem :: the javascript.net compiler
set "jsc=%%~dpsnfxv\jsc.exe"
goto :break_loop
)
)
echo jsc.exe not found && exit /b 0
:break_loop
call %jsc% /nologo /out:"%~n0.exe" "%~f0"
::::::::::::::::::::::::::::::::::::
::: end of compilation ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation
::
::::::::::
"%~n0.exe" %*
::::::::
::
endlocal
exit /b 0
****** end of jscript comment ******/
import System;
import System.Windows;
import System.Windows.Forms
var arguments:String[] = Environment.GetCommandLineArgs();
MessageBox.Show(arguments[1],arguments[0]);
5),最后一次调用powershell会创建一个弹出窗口(可以从命令行或从批处理中调用(如果已安装powershell):
powershell [Reflection.Assembly]::LoadWithPartialName("""System.Windows.Forms""");[Windows.Forms.MessageBox]::show("""Hello World""", """My PopUp Message Box""")
6) dbenham的方法在这里
start "" cmd /c "echo(&echo(&echo Hello world! &echo(&pause>nul"
7)对于系统托盘通知,您可以尝试以下操作:
call SystemTrayNotification.bat -tooltip warning -time 3000 -title "Woow" -text "Boom" -icon question
这是一个PowerShell变体,不需要在创建窗口之前加载程序集,但是它的运行速度比@npocmaka在此处发布的PowerShell MessageBox命令要慢得多(〜+ 50%):
powershell (New-Object -ComObject Wscript.Shell).Popup("""Operation Completed""",0,"""Done""",0x0)
您可以将最后一个参数从“ 0x0”更改为以下值,以在对话框中显示图标(有关更多参考,请参见弹出方法):
0x10停止
0x20问号
0x30感叹号
0x40信息标记
改编自Microsoft TechNet文章PowerTip:使用PowerShell显示弹出窗口。
echo X=MsgBox("Message Description",0+16,"Title") >msg.vbs
–您可以写0、1、2、3、4中的任何数字,而不是0(在'+'符号之前),这是每个数字的含义:
0 = Ok Button
1 = Ok/Cancel Button
2 = Abort/Retry/Ignore button
3 = Yes/No/Cancel
4 = Yes/No
–您可以写出16、32、48、64中的任何数字,而不是16(在“ +”符号之后),这是每个数字的含义:
16 – Critical Icon
32 – Warning Icon
48 – Warning Message Icon
64 – Information Icon
消息*“在此处插入您的消息”
可以正常工作,只需将其另存为.bat文件在记事本中,或确保将格式设置为“所有文件”
msg %SESSIONNAME% msg
我从这里使用名为msgbox.exe的实用程序:http : //www.paulsadowski.com/WSH/cmdprogs.htm
您可以从user32.dll调用dll函数,我认为
Rundll32.exe user32.dll,MessageBox(0,“文本”,“ titleText”,{最顶部消息框的额外标志等})
从我的手机中输入内容,不要判断我...否则我会链接额外的标志。
rundll32.exe user32.dll,MessageBoxA X
在“运行”框中执行该操作时,可以显示一个标题为X的消息框。不管我使X是什么,我都无法将其解释为多个参数-所有内容都包含在标题中。因此,rundll32.exe user32.dll,MessageBoxA (0, "MyTitle", "MyText", 0)
显示一个标题为的消息框,(0, "MyTitle", "MyText", 0)
但我无法从命令行-仅从运行框中获取它。在命令行上,它什么也不做。它是否可以从命令行或批处理文件或仅从“运行”框中确定起作用?
msg * /server:127.0.0.1在此处输入您的消息
where msg
返回C:\Windows\System32\msg.exe
。我认为您正在考虑net send
更好的选择
set my_message=Hello world
&& start cmd /c "@echo off & mode con cols=15 lines=2 & echo %my_message% & pause>nul"
描述:
lines=
行数,加上cols=
消息中的1
个字符,再加上3个字符(但是,最小值必须为15
)
cols
版本:set my_message=Hello world
&& (echo %my_message%>EMPTY_FILE123 && FOR %? IN (EMPTY_FILE123 ) DO SET strlength=%~z? && del EMPTY_FILE123 ) && start cmd /c "@echo off && mode con lines=2 cols=%strlength% && echo %my_message% && pause>nul"
start cmd /c "@echo off & mode con cols=18 lines=2 & echo My Popup Message & pause>nul"
)。你的不是。它回显该消息,但无法设置The screen cannot be set to the number of lines and columns specified.
至少在我的Windows 7中返回的窗口大小
x
当前目录中没有命名文件,或者您不介意丢失文件,它确实可以工作。
x
,则命令将失败。不用谢,请修复您的代码。您甚至不需要使用单线。而且,如果您认为自己的代码很好,请将其发布在Code Review中。
这是我在此处和其他帖子中给出的良好答案所组成的批处理脚本
您可以设置标题超时,甚至可以休眠以将其排在后面,而\ n可以安排在新行
将其命名为popup.bat并将其放在Windows路径文件夹中以在PC上全局工作
例如popup Line 1\nLine 2
将产生一个2行弹出框(popup /?
使用类型)
这是代码
<!-- : Begin CMD
@echo off
cscript //nologo "%~f0?.wsf" %*
set pop.key=[%errorlevel%]
if %pop.key% == [-1] set pop.key=TimedOut
if %pop.key% == [1] set pop.key=Ok
if %pop.key% == [2] set pop.key=Cancel
if %pop.key% == [3] set pop.key=Abort
if %pop.key% == [4] set pop.key=Retry
if %pop.key% == [5] set pop.key=Ignore
if %pop.key% == [6] set pop.key=Yes
if %pop.key% == [7] set pop.key=No
if %pop.key% == [10] set pop.key=TryAgain
if %pop.key% == [11] set pop.key=Continue
if %pop.key% == [99] set pop.key=NoWait
exit /b
-- End CMD -->
<job><script language="VBScript">
'on error resume next
q =""""
qsq =""" """
Set objArgs = WScript.Arguments
Set objShell= WScript.CreateObject("WScript.Shell")
Popup = 0
Title = "Popup"
Timeout = 0
Mode = 0
Message = ""
Sleep = 0
button = 0
If objArgs.Count = 0 Then
Usage()
ElseIf objArgs(0) = "/?" or Lcase(objArgs(0)) = "-h" or Lcase(objArgs(0)) = "--help" Then
Usage()
End If
noWait = Not wait()
For Each arg in objArgs
If (Mid(arg,1,1) = "/") and (InStr(arg,":") <> 0) Then haveSwitch = True
Next
If not haveSwitch Then
Message=joinParam("woq")
Else
For i = 0 To objArgs.Count-1
If IsSwitch(objArgs(i)) Then
S=split(objArgs(i) , ":" , 2)
select case Lcase(S(0))
case "/m","/message"
Message=S(1)
case "/tt","/title"
Title=S(1)
case "/s","/sleep"
If IsNumeric(S(1)) Then Sleep=S(1)*1000
case "/t","/time"
If IsNumeric(S(1)) Then Timeout=S(1)
case "/b","/button"
select case S(1)
case "oc", "1"
button=1
case "ari","2"
button=2
case "ync","3"
button=3
case "yn", "4"
button=4
case "rc", "5"
button=5
case "ctc","6"
button=6
case Else
button=0
end select
case "/i","/icon"
select case S(1)
case "s","x","stop","16"
Mode=16
case "?","q","question","32"
Mode=32
case "!","w","warning","exclamation","48"
Mode=48
case "i","information","info","64"
Mode=64
case Else
Mode=0
end select
end select
End If
Next
End If
Message = Replace(Message,"/\n", "°" )
Message = Replace(Message,"\n",vbCrLf)
Message = Replace(Message, "°" , "\n")
If noWait Then button=0
Wscript.Sleep(sleep)
Popup = objShell.Popup(Message, Timeout, Title, button + Mode + vbSystemModal)
Wscript.Quit Popup
Function IsSwitch(Val)
IsSwitch = False
If Mid(Val,1,1) = "/" Then
For ii = 3 To 9
If Mid(Val,ii,1) = ":" Then IsSwitch = True
Next
End If
End Function
Function joinParam(quotes)
ReDim ArgArr(objArgs.Count-1)
For i = 0 To objArgs.Count-1
If quotes = "wq" Then
ArgArr(i) = q & objArgs(i) & q
Else
ArgArr(i) = objArgs(i)
End If
Next
joinParam = Join(ArgArr)
End Function
Function wait()
wait=True
If objArgs.Named.Exists("NewProcess") Then
wait=False
Exit Function
ElseIf objArgs.Named.Exists("NW") or objArgs.Named.Exists("NoWait") Then
objShell.Exec q & WScript.FullName & qsq & WScript.ScriptFullName & q & " /NewProcess: " & joinParam("wq")
WScript.Quit 99
End If
End Function
Function Usage()
Wscript.Echo _
vbCrLf&"Usage:" _
&vbCrLf&" popup followed by your message. Example: ""popup First line\nescaped /\n\nSecond line"" " _
&vbCrLf&" To triger a new line use ""\n"" within the msg string [to escape enter ""/"" before ""\n""]" _
&vbCrLf&"" _
&vbCrLf&"Advanced user" _
&vbCrLf&" If any Switch is used then you must use the /m: switch for the message " _
&vbCrLf&" No space allowed between the switch & the value " _
&vbCrLf&" The switches are NOT case sensitive " _
&vbCrLf&"" _
&vbCrLf&" popup [/m:""*""] [/t:*] [/tt:*] [/s:*] [/nw] [/i:*]" _
&vbCrLf&"" _
&vbCrLf&" Switch | value |Description" _
&vbCrLf&" -----------------------------------------------------------------------" _
&vbCrLf&" /m: /message:| ""1 2"" |if the message have spaces you need to quote it " _
&vbCrLf&" | |" _
&vbCrLf&" /t: /time: | nn |Duration of the popup for n seconds " _
&vbCrLf&" | |<Default> untill key pressed" _
&vbCrLf&" | |" _
&vbCrLf&" /tt: /title: | ""A B"" |if the title have spaces you need to quote it " _
&vbCrLf&" | | <Default> Popup" _
&vbCrLf&" | |" _
&vbCrLf&" /s: /sleep: | nn |schedule the popup after n seconds " _
&vbCrLf&" | |" _
&vbCrLf&" /nw /NoWait | |Continue script without the user pressing ok - " _
&vbCrLf&" | | botton option will be defaulted to OK button " _
&vbCrLf&" | |" _
&vbCrLf&" /i: /icon: | ?/q |[question mark]" _
&vbCrLf&" | !/w |[exclamation (warning) mark]" _
&vbCrLf&" | i/info|[information mark]" _
&vbCrLf&" | x/stop|[stop\error mark]" _
&vbCrLf&" | n/none|<Default>" _
&vbCrLf&" | |" _
&vbCrLf&" /b: /button: | o |[OK button] <Default>" _
&vbCrLf&" | oc |[OK and Cancel buttons]" _
&vbCrLf&" | ari |[Abort, Retry, and Ignore buttons]" _
&vbCrLf&" | ync |[Yes, No, and Cancel buttons]" _
&vbCrLf&" | yn |[Yes and No buttons]" _
&vbCrLf&" | rc |[Retry and Cancel buttons]" _
&vbCrLf&" | ctc |[Cancel and Try Again and Continue buttons]" _
&vbCrLf&" ---> | ---> |The output will be saved in variable ""pop.key""" _
&vbCrLf&"" _
&vbCrLf&"Example:" _
&vbCrLf&" popup /tt:""My MessageBox"" /t:5 /m:""Line 1\nLine 2\n/\n\nLine 4""" _
&vbCrLf&"" _
&vbCrLf&" v1.9 By RDR @ 2020"
Wscript.Quit
End Function
</script></job>