如何在 Windows 中的命令提示符启动时运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17404165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:38:41  来源:igfitidea点击:

How to run a command on command prompt startup in Windows

windowscmdcommandcommand-prompt

提问by Rajkiran

EDIT

If you want to perform any task at computer startup or based on an event this is very helpful

http://answers.microsoft.com/en-us/windows/forum/windows_7-performance/how-to-schedule-computer-to-shut-down-at-a-certain/800ed207-f630-480d-8c92-dff2313c193b

编辑

如果您想在计算机启动时或基于事件执行任何任务,这将非常有帮助

http://answers.microsoft.com/en-us/windows/forum/windows_7-performance/how-to-schedule-computer-to-shut-down-at-a-certain/800ed207-f630-480d-8c92- dff2313c193b



Back to the question

回到问题

I have two questions:

我有两个问题:

  1. I want some specific commands to be executed when I start command prompt.

    e.g. clsto clear my command prompt.

  2. I want to execute some commands in a batch file and wait for the user to enter new commands (if any).

    e.g. A batch file which will take the user to some specified folder and then wait for the user to rename/delete a file from the command prompt.

  1. 我希望在启动命令提示符时执行一些特定的命令。

    例如cls清除我的命令提示符。

  2. 我想在批处理文件中执行一些命令并等待用户输入新命令(如果有)。

    例如一个批处理文件,它将把用户带到某个指定的文件夹,然后等待用户从命令提示符重命名/删除文件。

How can I do it?

我该怎么做?

回答by Ansgar Wiechers

If you want a defined set of commands to run every time you start a command prompt, the best way to achieve that would be to specify an init script in the AutoRunregistry value. Create it like this (an expandable string value allows you to use environment variables like %USERPROFILE%):

如果您希望每次启动命令提示符时都运行一组已定义的命令,实现这一目标的最佳方法是在AutoRun注册表值中指定一个 init 脚本。像这样创建它(可扩展的字符串值允许您使用环境变量,如%USERPROFILE%):

reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun ^
  /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f

Then create a file init.cmdin your profile folder:

然后init.cmd在您的配置文件文件夹中创建一个文件:

@echo off

command_A
command_B
...
cls

To remove these changes, deletethe registry key:

要删除这些更改,请删除注册表项:

reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun

回答by LewisTehMinerz

  1. Make a shortcut
  2. Go to the properties
  3. The bit where it says: C:\Users\<Your username>\Desktop\cmd.exe, you put: -cmd /K <your command here>
  1. 创建快捷方式
  2. 进入属性
  3. 它说的那一点:C:\Users\<Your username>\Desktop\cmd.exe,你把:-cmd /K <your command here>

e.g. C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f

例如 C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f

This is the way to launch 1 command without having to mess about with the registry.

这是启动 1 个命令而不必弄乱注册表的方法。

Run multiple commands

运行多个命令

You can also use &(and) operator to execute multiple commands.

您还可以使用&(and) 运算符来执行多个命令。

Eg.

例如。

C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f & H: & <your command>

Credits: user6589073

学分:user6589073

回答by Rajkiran

I found my answer: I should use the /Kswitch, using which I can enter a new command on the opened command prompt.

我找到了答案:我应该使用/K开关,使用它我可以在打开的命令提示符下输入新命令。

E.g. cmd /K clswill open a command prompt for me and clear it. (Answer for question 1)

例如,cmd /K cls将为我打开一个命令提示符并清除它。(问题 1 的答案)

and

cmd /K MyBatchFile.batwill start a command prompt, execute the batch file and stay on the command prompt and will not exit. (Answer for question 2).

cmd /K MyBatchFile.bat将启动命令提示符,执行批处理文件并停留在命令提示符处,不会退出。(回答问题 2)。

回答by Chaos

First, you need to press Windows Key + R. In the box that appears, type "regedit" (without the quotes). The Windows Registry Editor should open. Now, locate to HKEY_CURRENT_USER/Software/Microsoft/Command Processor. Once you have clicked on Command Processor on the left side, click Edit on the top bar. Then go to New > String Value in the Edit menu. Rename the String Value that appears to Autorun. Right click on Autorun and select Modify. Under the "Value Data" area, type in the commands you want to run. You can run multiple by typing && between them.

首先,您需要按 Windows 键 + R。在出现的框中,键入“regedit”(不带引号)。Windows 注册表编辑器应该会打开。现在,定位到 HKEY_CURRENT_USER/Software/Microsoft/Command Processor。单击左侧的命令处理器后,单击顶部栏上的编辑。然后转到“编辑”菜单中的“新建”>“字符串值”。重命名出现在自动运行中的字符串值。右键单击自动运行并选择修改。在“值数据”区域下,键入要运行的命令。您可以通过在它们之间键入 && 来运行多个。

回答by Asad Javed

I have a command to run a python program. I do not want to run this command manually after login, I want this command should run automatically after I logged in to my ubuntu. I am using Ubuntu 16.04.

我有一个运行 python 程序的命令。我不想在登录后手动运行这个命令,我希望这个命令应该在我登录到我的 ubuntu 后自动运行。我正在使用 Ubuntu 16.04。

Here is the command.

这是命令。

sh demo_darknet_yolov3.sh , this shell is placed in this directory littro@littro-System-Product-Name:~/MobileNet-YOLO-master/MobileNet-YOLO-master

sh demo_darknet_yolov3.sh,这个shell放在这个目录下 littro@littro-System-Product-Name:~/MobileNet-YOLO-master/MobileNet-YOLO-master