How to run a command on command prompt startup in

2019-01-10 02:57发布

问题:

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


Back to the question

I have two questions:

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

    e.g. cls to 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.

How can I do it?

回答1:

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 AutoRun registry value. Create it like this (an expandable string value allows you to use environment variables like %USERPROFILE%):

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

Then create a file init.cmd in your profile folder:

@echo off

command_A
command_B
...
cls

To remove these changes, delete the registry key:

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


回答2:

  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>

e.g. 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.

Run multiple commands

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

Eg.

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

Credits: user6589073



回答3:

Sorry for the noob type of question. I should have given it a li'l more thought. I found my answer. I should use /K switch using which I can enter a new command on the opened command prompt.

e.g. cmd /K cls will open a command prompt for me and clear it. (Answer for question 1)

and

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



回答4:

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.