Reputation: 3110
How do I launch Anaconda Administrator (elevated) Command Console into the Windows 10 UserProfile directory, NOT "c:\Windows\System32" ?
PROBLEM:
I want to do this because it is a hassle to constantly have to change my working directory from "c:\Windows\System32" to "c:\users\mylogin" directory every time I start Anaconda Administrator Consoles. The default Windows behavior is to ALWAYS launch administrator-privileged terminals in "c:\Windows\system32". This is a frequently used action as a Python programmer, sometimes executed multiple times per day, wasting my precious time.
WHAT I TRIED:
I tried modifying my taskbar and Windows Start button shortcut for the Anaconda Administrator Prompt with the "Start In: = %USERPROFILE%" but that does nothing. It still launches the Admin console in c:\windows\system32.
I looked high and low for solutions. Someone suggested changing Windows Registry entry for CMD.exe, which is draconian and could screw up my Windows system when various other scripts run. This SO question suggests that, but also explains why the default Windows behavior does what's expected.
Does anyone have a good solution for this problem?
Upvotes: 0
Views: 3763
Reputation: 3110
I found an ideal solution.
The normal Windows 10 shortcut for Anaconda runs the file "activate.bat" from the appropriate base environment's "Scripts" folder:
%windir%\System32\cmd.exe "/K" c:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
Modify the Anaconda file named "activate.bat" and add "cd %USERPROFILE% to the end of the "activate.bat" file. This activates the desired environment and then changes to the desired working directory. This has no other undesirable side effects on the Windows 10 operations.
The source code in "activate.bat" then looks as follows...
@REM Copyright (C) 2012 Anaconda, Inc
@REM SPDX-License-Identifier: BSD-3-Clause
@REM Test first character and last character of %1 to see if first character is a "
@REM but the last character isn't.
@REM This was a bug as described in https://github.com/ContinuumIO/menuinst/issues/60
@REM When Anaconda Prompt has the form
@REM %windir%\system32\cmd.exe "/K" "C:\Users\builder\Miniconda3\Scripts\activate.bat" "C:\Users\builder\Miniconda3"
@REM Rather than the correct
@REM %windir%\system32\cmd.exe /K ""C:\Users\builder\Miniconda3\Scripts\activate.bat" "C:\Users\builder\Miniconda3""
@REM this solution taken from https://stackoverflow.com/a/31359867
@set "_args1=%1"
@set _args1_first=%_args1:~0,1%
@set _args1_last=%_args1:~-1%
@set _args1_first=%_args1_first:"=+%
@set _args1_last=%_args1_last:"=+%
@set _args1=
@if "%_args1_first%"=="+" if NOT "%_args1_last%"=="+" (
@CALL "%~dp0..\condabin\conda.bat" activate
@GOTO :End
)
@REM This may work if there are spaces in anything in %*
@CALL "%~dp0..\condabin\conda.bat" activate %*
:End
@set _args1_first=
@set _args1_last=
@REM RICH ADDED THIS AS A CONVENIENCE to fix the Windows Shortcut for Anaconda Command Console so Console opens in %USERPROFILE%.
cd %USERPROFILE%
The only other thing required to make this totally work is to make sure that the CMD.exe for Anaconda runs as an "elevated" command console. (See picture)
Upvotes: 3