Create your shortcut and then right click to get the properties dialog and set the "Start in:" property to be your folder.
If you are running the shortcut as an administrator you should add your desired path to the target (instead of "start in"), for example:
%windir%\System32\cmd.exe /k cd c:\crp
or
%windir%\System32\cmd.exe /k pushd c:\crp
because the "start in" value is ignored when running as administrator (all credits go to @barlop for their answer and @T_D for their comment)
It is a bit tricky, because you do not want the background color changing process to intercept any of your console input. You could add the /NOBREAK option to the TIMEOUT command, such that the script never reads stdin, except it still could process a <CTRL-Break>
interupt, and ask if you want to terminate the script. The user could respond with Y
and terminate the background process, but that could get confusing if the main process also has a running batch script. Which process gets the input?
You can redirect stdin to nul for the background process, but TIMEOUT fails if input is redirected.
I modified your auto-change-text-color.bat to use a PING delay instead of TIMEOUT, thus allowing stdin to be redirected to NUL. I also put one last COLOR command at the end to finish with a more readable color combination.
@echo off
set NUM=0 1 2 3 4 5 6 7 8 9 A B C D E F
for %%x in (%NUM%) do (
for %%y in (%NUM%) do (
color %%x%%y
>nul ping localhost -n 2
)
)
color 07
The following command is used to launch the script in the background with stdin redirected to nul:
start "" /b cmd /c "auto-change-text-color.bat <nul"
The only odd side effect is that the background process still can receive <CTRL-Break>
, and it prints out the message Terminate batch job (Y/N)?
, but immediately continues without waiting for input. So you can safely start a batch script in the main process, issue <CTRL-Break>
and get two Terminate messages, and press Y or N knowing that only the batch script in your main process will respond (terminate or not).
Best Answer
This is a typical X-Y problem. If you want CMD to always open at a specific directory instead of the default, all you need to do is simply change the shortcut's properties as follows:
In Windows 7 the Command Prompt shortcut is typically located in
Start Menu > All Programs > Accessories
, so just right-click the shortcut, select Properties and edit the Start in field to your liking.You can also create a batch file named for example d.bat that contains a single line
cd /d c:\wamp\www
. Place the batch file somewhere in your path and now all you need to do is open CMD and type d to change to the specific directory. There are many more similar solutions as well.If you are dead set on parsing a shortcut (.LNK) file from the command prompt, save the following as ParseLnk.bat and execute it from the Command Prompt as
ParseLnk <LNK File>
: