简体   繁体   中英

batch file that opens cmd prompt types string as command line

I got my batch file to open cmd prompt and change directory successfully, however what I cannot get to work is after the directory has changed I want the batch to then to enter a string as a command.

I've tried cmd /k string but that didn't seem to work, the cmd just sits at the changed directory. I've also tried:

    set Opvar= echo string
    %Opvar%

Again it just sits at the last changed directory. The batchisp line that's commented out is what the string is that I want to act as if I typed the whole string and pressed enter at the current directory prompt. Here's the whole thing:

@Echo OFF

FOR /F "Tokens=*" %%@ IN ('Reg Query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" ^| FIND "-1000"') DO (
FOR /F "Tokens=2,*" %%A IN ('Reg Query "%%@" /v "ProfileImagePath" ^| FIND /V "%%@"') DO (
set drive=%%B
    Echo    Admin SID: %%~n@
    Echo Admin Folder: %%B
)
)

:Ask

echo What are you flashing? Please select one option(1,2, or 3):
echo 1. Main Board w/ 6-Station OpCon.
echo 2. Main Board w/ 12-Station Opcon.
echo 3. OpCon Board.


set /P INPUT=Type input: %=%
If "%INPUT%"=="1" goto Option1
If "%INPUT%"=="2" goto Option2
If "%INPUT%"=="3" goto Option3

::::::::::::

:Option1

echo Starting 6-Station DFU flash...
echo xcopy "%~dp0M1k_MainPCB\6-STATION\*.*" "%drive%\Desktop\MainPCB6\" /d /s /h /v /c /f /k /y
echo cmd /k "cd /d %drive%\Desktop\MainPCB6\6-STATION\Debug\"

goto End

::::::::::::

:Option2

echo Starting 12-Station DFU flash...
echo xcopy "%~dp0M1k_MainPCB\12-STATION\*.*" "%drive%\Desktop\MainPCB12\" /d /s /h /v /c /f /k /y
echo cmd /k "cd /d %drive%\Desktop\MainPCB12\12-STATION\Debug\"

goto End

::::::::::::

:Option3

echo Starting OpCon DFU flash...
xcopy "%~dp0M1k_SWPCB\SWPCB\*.*" "%drive%\Desktop\SWPCB\" /d /s /h /v /c /f /k /y
cmd /k "cd /d %drive%\Desktop\SWPCB\SWPCB\Debug\"

::batchisp -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0


goto End
::::::::::::

:End

echo ******
echo *****
echo ****
echo ***
echo **
echo *
echo Flash Completed!  Press any key to exit...

Pause>NUL&Exit

Any help would be much appreciated.

Thank you

You are changing the working directory in a new instance of CMD (CMD /K), use PUSHD to change the working directory in your current CMD instance and POPD to return to the last used directory:

@Echo OFF
PUSHD %WINDIR%"
Echo I'm on "%CD%" Directory!
POPD
Echo I'm on "%CD%" Directory!
Pause&Exit

Change your code to this:

:Option3

echo Starting OpCon DFU flash...
xcopy "%~dp0M1k_SWPCB\SWPCB\*.*" "%drive%\Desktop\SWPCB\" /d /s /h /v /c /f /k /y
PUSHD "%drive%\Desktop\SWPCB\SWPCB\Debug"
batchisp -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0
POPD
goto :End

...Or if you want to start directly the command in a new instance of CMD change it to this:

CMD /k "Start /W """" "%drive%\Desktop\SWPCB\SWPCB\Debug\batchisp" -device at32uc3a0512 -hardware usb -operation erase f memory flash blankcheck loadbuffer SWPCB.elf program verify start reset 0"

PS: Remember that you don't need to change the working dir to start an app, you can write the path followed by the process name like in my last example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM