是否有等效于$ SHLVL的Windows? [英] Is there a Windows equivalent to $SHLVL?

查看:81
本文介绍了是否有等效于$ SHLVL的Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近运行了一个在其中运行 cmd的第3方脚本,从而增加了cmd窗口的嵌套深度(这使我的历史记录和DOSKey宏在该过程中不可用)。所以我想知道是否有等效的$ SHLVL或其他方法来确定这种情况?我想我只是可以向上箭头查看我的历史记录是否仍然存在,但是有一天我可能会从脚本中获取它。

I recently ran a 3rd party script that ran "cmd" inside of it, thus increasing the nesting depth of my cmd window (making my history and DOSKey macros unavailable in the process). So I was wondering if there's an equivalent of $SHLVL or some other way to determine this situation? I suppose I could just up-arrow to see if my history is still there, but someday I might need this from a script.

似乎没有什么不同当我查看 set的输出时。谢谢!

There does not appear to be anything different when I review the output of "set". Thanks!

推荐答案

编辑更改了代码以列出PID。

EDIT Changed code to list PIDs.

编辑2 更改代码以将级别输出为%errorlevel%

Edit 2 Changed code to output the level as %errorlevel%

请参见 https://docs.microsoft.com/zh-cn/ Windows /控制台/ getconsoleprocesslist

CountConsoleProcess.vb

imports System.Runtime.InteropServices 
Public Module MyApplication  

Public Declare Function GetConsoleProcessList Lib "Kernel32" (ByRef ProcessList as Integer, ByVal Count as Integer) As Integer

Public Sub Main ()
                Dim ProcessList(0 To 9) As Integer
                Dim Count As Integer
                Dim Ret As Integer
                Dim x as Integer
                Dim ColItems as Object
                Dim objWMIService As Object
                objWMIService = GetObject("winmgmts:\\.\root\cimv2")
                Count = 10
                'subtract one to account for this program
                Ret = GetConsoleProcessList(ProcessList(0), 10) - 1
                Console.Writeline("Level = " & Ret)

                For x = Ret  to 1 step -1
                    colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessID=" & ProcessList(x))
                    For Each objItem in colItems
                        Console.writeline("PID : " & objItem.ProcessID & "  Command line : " &  objItem.CommandLine)
                    Next
                Next
                Environment.ExitCode = Ret  
End Sub
End Module 

CountConsoleProcess.bat

C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\CountConsoleProcess.vb" /debug:full /out:"%~dp0\CountConsoleProcess.exe" /target:exe 
pause

ConsoleTest.Bat

REM Showing error level
"C:\Users\User\Desktop\ConsoleTest.bat"
Echo %errorlevel%
pause

并使用(在执行 cmd / k 之后,它们是两个正在运行的CMD)。第一个是当前的PID。

And to use (after doing a cmd /k so their are two CMDs running). The first is the current PID.

Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Windows\system32>cmd /k echo hello world
hello world

C:\Windows\system32>"C:\Users\User\Desktop\ConsoleTest.bat"

C:\Windows\system32>"C:\Users\User\Desktop\Console.exe"
Level = 2
PID : 6848  Command line : "C:\Windows\System32\cmd.exe"
PID : 7824  Command line : cmd  /k echo hello world

C:\Windows\system32>Echo 2
2    

C:\Windows\system32>

并将当前文件夹添加到路径中

And to add the current folder to the path

_AddThisFolderToPath.bat

Setx path "%path%;%~dp0"
Pause

这篇关于是否有等效于$ SHLVL的Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆