Monday, December 16, 2019

cmd.exe - opening CMD with commands already executed


Ok so I've started Java at school and we have to compile and run our code through the CMD (no admin privileges)(on windows 10). Every time I open CMD, I have to change the directory to the bin folder to be able to do my Java stuff. My question is: Is there any way to open the CMD so that there have already been specific commands run? i.e. when I open CMD or whatever file I create, can the directory already be set to where I want it to be (as well as a different font colour and title for the CMD window), then I can start using CMD as normal? I haven't really found any comprehensible answers anywhere.


Answer



If you want to run some commands whenever you open any Command Prompt window, you can create a HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun string variable in the Windows registry and set it to the path of a .cmd/.bat script. That script then can executes whatever commands you want. From cmd /?:



If /D was NOT specified on the command line, then when CMD.EXE starts,
it looks for the following REG_SZ/REG_EXPAND_SZ registry variables,
and if either or both are present, they are executed first.


HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

and/or


HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun


Be forewarned: I do not actually recommend setting the AutoRun variable since it affects all cmd.exe instances and could have unexpected (and possibly confusing) side effects that will be hard to debug later if you forget that AutoRun is set in the registry.




Alternatively, if you wish to be able to double-click on an icon, execute some commands, and get an interactive Command Prompt window, then you instead can create a .cmd (or .bat) script:


@echo off
cd SOME_DIRECTORY
SOME_COMMAND
SOME_OTHER_COMMAND
cmd /k

Double-clicking on that script will execute the specified commands, invoke a cmd.exe subshell, and leave it running waiting for additional input.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...