Batch file for changing cmd directory and pre-filling a command

☆樱花仙子☆ 提交于 2019-12-24 13:56:04

问题


I've searched long and hard to get details on this, with no luck.

All I want to do is:

  1. open CMD
  2. change my working directory
  3. enter a text string command, and pause there so I can manually enter the last portion of the command and press enter

Example:

@ECHO OFF
start cmd.exe /K "cd C:\ProgramData\Microsoft\Windows\Start Menu"

I have the first half of it working fine, it opens cmd and changes the directory - but how do I fill in the text string into the window at this point?


回答1:


Ok, you want to:

  • open CMD
  • change my working directory
  • enter a text string command, and pause there so I can manually enter the last portion of the command and press enter

And then? After that there are two cmd.exe sessions active, so it will be problems with the following input. The Batch file below allows you do to what you want, but have the problem of what to do next. Try it and give feedback, so we can fix the details.

@if (@CodeSection == @Batch) @then

@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

rem Start cmd.exe program 
start "" cmd

rem Send whatever you want to previous cmd.exe
%SendKeys% "echo Hello world!{ENTER}"
%SendKeys% "cd C:\ProgramData\Microsoft\Windows\Start Menu{ENTER}"
%SendKeys% "echo You continue at this point: "

set /P "="
ECHO TERMINATE ORIGINAL BATCH
goto :EOF

@end

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));



回答2:


Try this:

@ECHO OFF
set /p "txt=Enter Path"
start cmd.exe /K "cd /d %txt%"


来源:https://stackoverflow.com/questions/22767331/batch-file-for-changing-cmd-directory-and-pre-filling-a-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!