Batch file for changing cmd directory and pre-fill

2019-08-05 05:25发布

问题:

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%"