当前位置:网站首页>【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
2022-06-25 06:39:00 【dssgresadfsrgre】
一、程序启动命令——start
start命令一般用来打开一个文件(通常是可执行程序)或文件夹。
先用start /?命令大致了解帮助信息。
(1)打开某一个文件夹。start dir,这个dir既可以是磁盘根目录,也可以是磁盘内的子目录。如果不加min或max参数,就会默认最大化打开(在前台打开)。
如果要打开e盘根目录,执行命令【start e:】。
如果要打开e盘根目录下的某文件夹(例如adobe),执行命令【start e:\adobe】
(2)打开某个文件。 start path,这个path既可以是文档类的txt、doc、excel文件,也可以是多媒体mp3、mp4文件,也可以是可执行exe、bat文件。
尝试一下打开exe文件,【start D:\绿色免安装小软件汇总\PdgCntEditor_PDF目录编辑器\PdgCntEditor.exe】。
顺利地打开了。
(3)不管是打开文件,还是打开文件夹,都要注意空格的问题。
啥意思呢?啥叫注意空格问题?
cmd窗口中的不同参数之间往往使用空格进行区分,如果你使用空格往往意味着空格之后是下一个参数或者赋予空格前的参数的取值。
例如酷狗音乐的路径是【D:\Program Files (x86)\KuGou\KGMusic\KuGou.exe】,那么这时我们执行命令【start D:\Program Files (x86)\KuGou\KGMusic\KuGou.exe】,会发现出错了。提示找不到文件(夹)【D:\Program】,容易知道cmd程序将空格前后的路径字符串分割开了,我们如何解决这个问题呢?
解决方法很简单,哪个层级目录的名称中有空格,就在哪里加双引号。执行命令【start D:\"Program Files (x86)"\KuGou\KGMusic\KuGou.exe】。
注意不是执行命令【start "D:\Program Files (x86)\KuGou\KGMusic\KuGou.exe"】 ,具体原因我也不知道,直观上反映出来,会重新打开一个cmd窗口,工作目录不变。
如果完整路径上有多个空格,这时候给每一个层级加双引号显然就比较麻烦了。
事实上我们还有另一种方法。
假设要打开文件夹【D:\BaiduNetdiskDownload\test1 2 3\test 3 4 5】,执行命令【start "" "D:\BaiduNetdiskDownload\test1 2 3\test 3 4 5"】(第一个双引号里随意填什么内容)。
(4)如果不想执行命令后窗口立即显示在屏幕上,也即想要在后台打开,可以用/min参数。
例如打开某exe程序,执行命令【start /min D:\绿色免安装小软件汇总\PdgCntEditor_PDF目录编辑器\PdgCntEditor.exe】
果然是在后台悄咪咪打开的。
但是我还是发现有一些程序,即使添加/min参数,
也无法在后台打开,比如酷狗音乐。
二、程序调用命令——call
Call命令的用处是调用已经写好的bat文件,假设被调用bat文件名称是【demo.bat】,主动调用的bat文件名是【main.bat】,那么在main.bat中调用demo.bat就需要用到call命令。
类似C语言中的函数声明,函数定义在demo.bat中进行,函数声明在主程序文件main.bat中进行。
先打印call命令的帮助信息。
下面我们看看具体怎么用call这个命令。
新建一个demo.bat文件,里面的代码为。
@echo off
echo 现在开始执行子程序
echo 你会数到10吗?
echo 难不倒我! 1,2,3,4,5,6,7,8,9,10
echo 真厉害呢
echo 祝你幸福
echo 见了鬼
echo 怎么后面的都没执行
echo 见了鬼
echo 继续啊?
再新建一个main.bat文件,里面的代码为。
chcp 65001
@echo off
echo 嘿老铁抓好了!主程序开始跑了...
echo 你会数数吗?
echo 这个我不会,但是我儿子会,你去问他。
call D:\D-desktop\demo.bat
echo 主程序也结束了,好好休息一下吧。
pause
可见直接用【call path】就能成功调用。
但是在实验过程中我还是发现了一些问题。
第一个问题:假使子程序文件最后一行有pause,就会把靠近pause的几行中句号结尾、或者问号结尾的行当做命令看待,结果就会出错。
例如将demo.bat文件修改,而main.bat文件保持不变。
@echo off
echo 现在开始执行子程序
echo 你会数到10吗?
echo 难不倒我! 1,2,3,4,5,6,7,8,9,10
echo 真厉害呢
echo 祝你幸福。
echo 见了鬼。
echo 怎么后面的都没执行。
echo 见了鬼。
echo 继续啊?
pause
执行后pause的前4行都出现了问题。
第二个问题:假使我去掉子程序文件中最后一行的pause,那么也会出现最后几行命令失效的情况。
同样将demo.bat文件予以修改,然后main.bat保持不变。
@echo off
echo 现在开始执行子程序
echo 你会数到10吗?
echo 难不倒我!1,2,3,4,5,6,7,8,9,10
echo 真厉害呢
echo 祝你幸福
echo 见了鬼
echo 怎么后面的都没执行
echo 见了鬼
echo 继续啊
执行结果如下图所示,最后4行又全军覆没。千万别以为仅仅最后4行才会出问题,你们可以自己去试,一大堆bug,所以建议这个功能谨慎使用,能把代码放在主程序就放进去。
三、进程/服务操作——tasklist、net
3.1 进程管理
- 显示当前正在运行的进程:
tasklist
- 运行程序或命令:
start 程序名
- 结束进程,按名称:
taskkill /im notepad.exe
(关闭记事本) - 结束进程,按 PID:
taskkill /pid 1234
(关闭 PID 为 1234 的进程)
3.2 服务管理
- 显示当前正在运行的服务:
net start
- 启动指定服务:
net start 服务名
- 停止指定服务:
net stop 服务名
边栏推荐
- Ltpowercad II and ltpowerplanner III
- Streamnational platform version 1.5 is released, integrating istio and supporting openshift deployment
- Changing the background color of tab bar - changing the background color of tab bar
- 【一起上水硕系列】Day 4
- Clearing Magento log data - clearing Magento log data
- I have used it for six years!
- SQL query, if value is null then return 1 - SQL query, if value is null then return 1
- MySQL - definition and assignment of variables
- 深入解析 Apache BookKeeper 系列:第三篇——读取原理
- Analysis on the trend of the number of national cinemas, film viewers and average ticket prices in 2021 [figure]
猜你喜欢
LTpowerCAD II和LTpowerPlanner III
正版photoshop2022購買體驗經曆分享
Enter an integer with any number of bits, and output the sum of each bit of the number. For example: 1234 – > 10
In depth analysis of Apache bookkeeper series: Part 3 - reading principle
[tool sharing] a software that pays equal attention to appearance and skills
基于 KubeSphere 的分级管理实践
Several schemes of traffic exposure in kubernetes cluster
Rotation vector (rotation matrix) and Euler angle
Display purchase Summary - Dell 2705qm BenQ pd2700u
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭
随机推荐
1W words | 40 pictures | hard core es actual combat
Blue Bridge Cup SCM module code (timer) (code + comments)
Google extender address
Ltpowercad II and ltpowerplanner III
Hanxin's trick: consistent hashing
Can we use function pointers in go- Can we have function pointers in Go?
Large funds support ecological construction, and Plato farm builds a real meta universe with Dao as its governance
【一起上水硕系列】Day 4
几款不错的天气插件
【UVM入门 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常规方法、寄存器模型的应用场景
Reading sensor data with GPIO analog SPI interface
Keepalived monitors the process and automatically restarts the service process
全局变量&局部变量
Changing the background color of tab bar - changing the background color of tab bar
Lotus v1.16.0-rc2 Calibration net
[introduction to UVM== > episode_9] ~ register model, integration of register model, general methods of register model, application scenarios of register model
弱大数定理的意义与证明
Don't you know the evolution process and principle of such a comprehensive redis cluster model?
5g private network market is in full swing, and it is crucial to solve deployment difficulties in 2022
线程状态变化涉及哪些常用 API