Registry context menu addition works under win7 but not win10 - windows

I have a registry edit that works under windows 7 but not under windows 10 and I can't figure out why.
My goal is to add a context menu action, "Run R script", to appear in the context menu when someone right-clicks on a file ending in ".R". Here, the command is to launch a .cmd (.bat) script with the file name as an argument.
Under windows 7, this is relatively straightforward. Adding a key under HKEY_CURRENT_USER\Software\Classes\R_auto_file\shell\Run Rscript\command works fine. For example, from cmd.exe the command might be something like this:
reg add "HKEY_CURRENT_USER\Software\Classes\R_auto_file\shell\Run Rscript\command" /v "" /t REG_SZ /d "\"C:\path\to\run_rscript.cmd\" \"%1\""
After running the above, right clicking on an R file shows this:
while that option does not show up for non-R files.
So far so good. The problem is that this doesn't work under windows 10. What's curious is that if I replace R_auto_file with * in the reg add command, then "Run R script" does show up in the context menu, and it does work. But the context menu action shows up for all files, not just R files. That is undesirable because I want this option to only show up for R files, and ultimately I want different context actions for different files.
Edit (more info) ------
Here is what's under .r in the FileExtx key. (exported as a .reg file and copy and pasted here)
```
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.r]
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.r\OpenWithList]
"a"="rstudio.exe"
"MRUList"="acb"
"b"="a"
"c"="{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\OpenWith.exe"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.r\OpenWithProgids]
"R_auto_file"=hex(0):
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.r\UserChoice]
"Hash"="SkJTR1uegvY="
"ProgId"="Applications\\rstudio.exe"
```

Related

Batch: start application doesn't show a GUI

I try to launch a self written autoit application called "KeyShortcuts.exe" using a batch called "launchMacros.bat". This applications provides keyboard shortcuts for various things and includes a GUI which shows me the available shortcuts.
launchMacros.bat:
start "MyMacros" "M:\applications\AutoIt\KeyShortcuts.exe"
The application does start and I'm able to use every shortcut but I'm not able to see the GUI.
If I start the application direct (double click on KeyShortcuts.exe) everythings works fine.
I also tryed starting the application using runas:
runas /user:REQUIREDUSERNAME /savecred "M:\applications\AutoIt\KeyShortcuts.exe"
Same problem here. Even right click -> "Run as administrator" doesnt worked.
Any suggestions?
If your batch file is in a different directory than KeyShortcuts.exe, you may need to specify the starting directory using the /D parameter for START.
Like this:
START "MyMacros" /D "M:\applications\AutoIt" "M:\applications\AutoIt\KeyShortcuts.exe"
Every batch file launched from Windows GUI create a new console window, run the batch file, then close. If you need this to be different, there's several ways:
Create a shortcut to CMD /K YOURBATCHFILE.BAT
Add a pause to your BAT file
Here's a demonstration of method 1:
New > Shortcut
Type the location of the item: C:\Windows\System32\CMD.EXE
Type the name for this shortcut: InsertYourNameHere
Click Finish
Right click on your Shortcut and go properties
Change Target to: C:\Windows\System32\CMD.EXE /K "InsertYourBatchFileName.BAT"
Click OK
Done, now you have a shortcut that opens a new console window and leaves it open whilst ir runs your batch file.

Creating a shell right click menu item to edit desktop.ini

I am trying to create a right click shell menu item to edit desktop.ini. I have managed to get this far:
[HKEY_CLASSES_ROOT\Folder\shell\editdesktop]
#="editdesktop"
[HKEY_CLASSES_ROOT\Folder\shell\editdesktop\command]
#="notepad "%L/desktop.ini"
use the cmd command to create an desktop.ini and append the following format to the desktop.ini file ,Then open it(desktop.ini) with notepad.exe
/* Format start */
[.ShellClassInfo]
InfoTip=
ConfirmFileOp=0w
/* Format end */
// ps: i just want to creat the folder InfoTip , ,when i hover the folder,it will so 2 me.
CMD.exe nor Windows Scripting Host has .ini support so implementing this without overwriting is a bit risky, you also need to set the correct attribute on the folder and this could be SYSTEM or READONLY depending on a registry key!
This example should work in the default windows configuration and tries its best to not overwrite a existing file:
[HKEY_CLASSES_ROOT\Folder\shell\editdesktop\command]
#="cmd /C ((if not exist \"%L\\desktop.ini\" (>\"%L\\desktop.ini\" (echo.[.ShellClassInfo]&echo.InfoTip=&echo.ConfirmFileOp=0w)))&attrib +s \"%L\"&attrib +h +s \"%L\\desktop.ini\"&start notepad \"%L\\desktop.ini\")"
It is probably a better idea to write a WSH or powershell script...

How to run a .bat from inside the IDE

What I want to do is very simple, yet can't find the way to do it.
In my projects I have several .bat files (I know, I'm old) the perform some tasks like running SqlMetal, etc.
I just want to right-click the file and select "run" or something so the bat is executed. How do I do it?
Note: I know I can use Tools -> External Tools, but it's not what I'm looking for
If each project has a few batch files associated with it then why not include them in the project and add a new external tool and link it with a custom menu item to run the tool? This will give you a list of batch files in each project and a context menu command to run them. The only downside is that it isn't file type specific (it will let you run any file as a batch file).
Here's how to do it...
Create an external tool called "Run batch file"
Set the Command to: CMD.EXE
Set the Arguments to: /c "$(ItemPath)"
Set the Initial directory to: $(ItemDir)
Check the "use output window" checkbox and then Apply to create the command
Note where the new command appeared in the list of commands. The external commands are numbered from 1 starting below the divider bar. #1 is usually "Create GUID"
Now go to Tools -> Customize and select the commands tab.
Select the Context menu radio button and select "Project and Solution Context menus | Item" from the dropdown.
Now use "Add Command..." to add a new command
In the Categories list select "Tools"
From the commands select the "External Command #" that corresponds to the position of the "Run Batch file" custom command you noted the number of in step 5 above.
Move it to the correct position in the list add keyboard shortcuts etc.
Close the dialog.
Now right click on the batch file and you should see a "Run batch file" menu item. This will execute the batch file and show its output in the VS Output window.
Right click the batch file in the Solution Explorer
Select “Open With…” from the context menu
Click “Add…”
In the “Program name” textbox, enter powershell.exe
In the “Friendly name” textbox enter “PowerShell”
Select “Set As Default”
Click OK
via
Check out the Open Command Line Visual Studio extension.
As per the description, it features:
Opening of a command line at the root of the project.
Support for all consoles such as CMD, PowerShell, Bash etc.
Syntax highlighting, Intellisense and execution of .cmd and .bat files.
For files in your solution, a context-menu button shows up.
Alternatively, the keyboard shortcut Shift+Alt+5 can be used when editing a batch file. This makes it really easy and fast to execute any batch file - even ones that are not part of your project.
Right-click a file in the Solution Explorer window, Open With, click the Add button to add your .bat files. Use %1 in your .bat to get the path to the selected file.
Couldn't get any of the above to quite work, but this did (VS 2019/2022):
Right click a .bat file in Solution Explorer and Open With...
Add...
Program: CMD.exe
Arguments: /c %1
Friendly name: Command
OK and Set as Default for double clicking of batch files to run from Solution Explorer as the default (or right click Open With... and select Command if you don't want it the default).
Merely a suggestion: This is a scenario which can be done with the VsCommandBuddy extension. The following configuration is an example:
{
"cmdname": "mk",
"title": "Run MK.BAT",
"description": "Run the MK bat file.",
"cwd": "$(SolutionDir)",
"filename": "cmd.exe",
"arguments": "/C mk.bat",
"async": false
}
You can assign shortkeys as well, so you can invoke your batch files with shortcut keys. Output comes in your output pane.

Changing default startup directory for command prompt in Windows 7

How do I change default startup directory for command prompt in Windows 7?
I usually do the following to start command prompt from C:\
WIN-R (Run Prompt)
cmd /K cd C:\
I want to do the following to start command prompt from C:\
WIN-R (Run Prompt)
cmd
Make a shortcut pointing to cmd.exe somwhere (e.g. desktop) then right-click on the copy and select "properties". Navigate to the "Shortcut" menu and change the "Start in:" directory.
The following solution worked well for me. Navigate to the command prompt shortcut in the start menu:
C:\Users\ your username \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Accessories\Command Prompt
Right click on the shortcut file to open the properties dialog. Inside the "Start in:" textbox you should see %HOMEDRIVE%%HOMEPATH%. If you want the prompt to start in C:\ just replace the variables with "C:\" (without quotes).
update
It appears that Microsoft has changed this behavior recently and so now an additional step is required. After performing the steps above copy the modified shortcut "Command Prompt" and rename it to "cmd". Then when typing "cmd" in the start menu it should once again work.
Open regedit and browse to this path
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
Create new string vale named Autorun. Set its value to cd /d C:\.
Run cmd again. Voila!
While adding a AutoRun entry to HKEY_CURRENT_USER\Software\Microsoft\Command Processor like Shinnok's answer is the way to go it can also really mess things up, you really should try to detect a simple cmd.exe startup vs a script/program using cmd.exe as a child process:
IF /I x"%COMSPEC%"==x%CMDCMDLINE% (cd /D c:\)
Easiest way to do this
Click "Start" and type "cmd" or "command prompt".
Select Top most search application named exactly same "cmd" or "command prompt".
Right Click on it and select "Send To"=>"Desktop".
On Your Desktop New "cmd" Shortcut will appear
Right Click on that icon and choose "properties"
Popup will appear, In "Shortcut" Tab Type the new location in "Start In" option (e.g D:\xyz)
Drag that icon and add/pin it in "Task Bar"
On Windows Start Menu, right click on Command Prompt.
Click on "Properties".
"Command Prompt Properties" dialog box opens.
Edit the field "Start in " to a location where you want to start the command prompt.
Example: Chand %HOMEDRIVE%%HOMEPATH% to D:\PersonalPrograms.
Next time when you start command prompt the start up directory will be D:\PersonalPrograms
Bit late but ignore the registry mods.
Simply change the shortcut target to:
cmd /k "command"
i.e.
cmd /k "cd\myStartUpFolder"
Voila!
This doesn't work for me. I've tried this both under Win7 64bit and Vista 32.
I'm using the below commandline to add this capability.
reg add "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "IF x"%COMSPEC%"==x%CMDCMDLINE% (cd /D c:)"
I think the easiest way is to make a cmd shortcut, then change the shortcut's "Start in" directory to the one you want to start with.
On windows 7:
Do a search for "cmd" on your Windows computer
right-click cmd and left click "Pin to start menu" (Alternatively, right-click cmd - click copy and then paste to your desktop )
right-click the cmd in your start menu or on your desktop (depending on choice 2 above) - left click properties
inside the "start in" text box paste the location of your default start directory
Press Apply and OK
Every time you click on the cmd in your start menu or your desktop shortcut, the CMD will open in your default location
changing shortcut under Windows System on 8.1 worked for me - another thing I found is that 'Start In:' WORKS when Advanced -> Run as admin is UNCHECKED, however, if CHECKED, it does not work
HKEY_CURRENT_USER\Software\Microsoft\Command Processor
string: Autorun
value: cd /d %~dp0
all bat files will run from the bat file location
One easy way to do it + bonus.
Start cmd with administrator rights, the default directory for the prompt will be C:\WINDOWS\system32.
I created a bat file in that directory (notes.bat)
Opened it with notepad and wrote the following lines. Each line is followed with a comment which should not be added to the bat file.
#echo off
prompt $S$CYourNamel$F$S$G$S
/* This is a comment, do not include it in the bat file - above line will make the prompt look like (YourName) > */
cd C:\Your_favorite_directory
/* This is a comment, do not include it in the bat file - above line will navigate you to your desired directory */
Saved the file and that was it.
Now when You open cmd with admin rights, just write: notes or notes.bat
and it will execute the notes.bat file with desired changes.
go to regedit ( go to search and type regedit)
expand "HKEY_CURRENT_USER" node
under HKEY_CURRENT_USER node expand "software" node
under software node expand "microsoft" node
under microsoft node click on "Command Processor"
path looks like this : "HKEY_CURRENT_USER\Software\Microsoft\Command Processor"
it looks something like this :
if you do not see "Autorun" String Value
Right Click - New - Expandable String Value, and rename it to Autorun
double click on "Autorun"
10.enter this value path format:
"CD/d C:\yourfoldername\yoursubfoldername"
Edit: It actually seems that editing the file shortcut breaks the Win+x, c key shortcut. (Moral of the story: only change system files you know how to fix! Eventually after a Windows update it repaired itself.)
What I ended up doing is creating a new customized Command Prompt shortcut in the start folder and pinned to the taskbar that I launch instead of cmd.exe
As other answers point out, changing the registry Autorun cmd start location is a bad idea because it silently will break other programs that shell out for tasks, like Visual Studio Code.
You should just change whatever shortcut you use to open cmd to have a Start In entry.
If you use Win+x, c to launch cmd, you can edit the Start In for
"%LOCALAPPDATA%\Microsoft\Windows\WinX\Group3\02 - Command Prompt.lnk"
type
cmd.exe /k cd c:/
in a text file and save as cmd.bat Clicking this file does the trick. You can pin it to the start menu as well.
To start in a partitioned drive (e.g. D), use
cmd.exe /k d:
My default dir was system32 when starting CMD.
I then created a batch file in that directory to change dir to the one I was after.
This caused me to always call that bat when starting CMD every time.
So I made a reg file & put this inside:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"Autorun"="cd C:\\Users\\Me\\SomeFolder"
After saving it, I opened the file, clicked ok to merge with registry, and since then every time I open CMD, I get my dir
In the new Windows Terminal, you can click Settings and edit the line "startingDirectory" to achieve something similar.
Please note, however, that this changes the default startup directory only in Windows Terminal, and not for the command prompt globally.
Use Windows Terminal and configure a starting directory.
Partial settings.json:
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "Command Prompt",
"commandline": "cmd.exe",
"hidden": false,
"startingDirectory": "C:\\DEV"
},
HKEY_CURRENT_USER\Software\Microsoft\Command Processor is no longer valid.
The new key is here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
Change "Autorun" to "CD /d C:\YourFolder" (without double quotes)
Keep the opening of the command prompt clean. Avoid editing the registry key and adding an Autorun, it may come back to bite you.
Create a simple batch file and save it in the C:\Windows or C:\Windows\System32 folder. I call mine !.bat (exclamation mark). It has the following commands:
#echo off
c:
cd \
cls
whoami
It goes to the folder where I need to work, clears the screen and tells me what security context I'm in.
"start in directory" command
cmd /K cd C:\WorkSpace
but if WorkSpace happens to be on different than C drive, console will be launched in default folder and then you still need to put D: to change drive
To avoid this use cd with -d parameter
cmd /K cd -d D:\WorkSpace
create a shortcut and your fixed ;)
hi if you want cmd to automatically open when the machine starts up you can place the cmd.exe executable in the startup folder(just search for startup and place a shortcut of cmd.exe there)
regedit worked great. HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\Command Processor, all you have to do is change the AutoRun key value, which is already set to wherever you are currently getting dumped into to a new value in the format of:
cd /d <drive:path>
for c:\, that would be cd /d c:\
for junk, that would be cd d/ c:\junk
its very simple, even a novice thats never used regedit should be able to figure it out. if not, go to the c:\prompt and just type in regedit, then follow the path to the key.

BAT file to open CMD in current directory

I have many scripts which I interact with from the command line. Everytime I need to use them, I have to open a command line window and copy+paste and CD to the path to the directory they are in. This is tedious (they are in a rather deep file system, so typing out the full path is a pain, copy+paste is better but not much). I tried to create a .BAT file that I could double-click on that would open a new command-line window in the folder the .bat file exists in but it does not work. It opens a new window, but the working directory is not the directory that .bat file is in. Here's what I've got after much googling (My cmd skills ain't so great):
cd %CD%
cmd.exe
I know from when I used Linux that Konqueror had a "Command-line window here" feature, and that's the effect I'm trying to get on Windows.
you probably want to do this:
cd /d %~dp0
cmd.exe
this will set your current directory to the directory you have the batch file in
Create a file named open_dos_here.cmd with the following lines:
%~d1
cd "%~p1"
call cmd
Put this file at any folder.
Then, go to your Send To folder (Win+E; Alt+D;shell:sendto;Enter).
Create a shortcut to point to this open_dos_here.cmd
Then, in any folder, select any file or sub-folder. Right-click and select "Send To" and then select open_dos_here.cmd to open the DOS in that folder.
You can just enter cmd into the address bar in Explorer and it starts up in that path. Likewise for PowerShell.
There's a simpler way -
start /d "folder path"
As a more general solution you might want to check out the Microsoft Power Toy for XP that adds the "Open Command Window Here" option when you right-click: http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx
In Vista and Windows 7, you'll get that option if you hold down shift and right-click (this is built in).
I'm thinking that if you are creating a batch script that relies on the Current Directory being set to the folder that contains the batch file, that you are setting yourself up for trouble when you try to execute the batch file using a fully qualified path as you would from a scheduler.
Better to add this line to your batch file too:
REM Change Current Directory to the location of this batch file
CD /D %~dp0
unless you are fully qualifying all of your paths.
Another solution is to use a shortcut file to cmd.exe instead of a batch file.
Edit the shortcut's start in property to %~dp0.
You achieve the same thing, except it has the Cmd icon (and you can change this).
Some people don't like clicking on batch files without knowing what's in them, and some corporate network drives have a ban on .bat files...
The simplest command to do this:
start
You can always run this in command line to open new command line window in the same location. Or you can place it in your .bat file.
Most simple way in explorer is to Shift + right mouse click on the folder or on an empty space in the folder and click on Open command prompt here.
CMD will then start in that folder
I must say, I'm not sure if it works for Windows Vista and below, but it surely works for Windows 7, 8, 8.1 and 10.
Referring to answer of #Chris,
We can also go to parent directory of batch file and run commands using following
cd /d %~dp0..
<OTHER_BATCH_COMMANDS>
cmd.exe
To understand working of command cd /d %~dp0.. please refer below link
What does it mean by command cd /d %~dp0 in Windows
You could add a context menu entry through the registry:
Navigate in your Registry to
HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell
and create a key called "Command Prompt" without the quotes.
Set the default string to whatever text you want to appear in the right-click menu.
Create a new key within your newly created command prompt named "command," and set the default string to
cmd.exe /k pushd %1
You may need to add %SystemRoot%\system32\ before the cmd.exe if the executable can't be found.
The changes should take place immediately. Right click a folder and your new menu item should appear.
Also see http://www.petri.co.il/add_command_prompt_here_shortcut_to_windows_explorer.htm
When you are in the desired folder , just type CMD in your address bar
A bit late to the game but if I'm understanding your needs correctly this will help people with the same issue.
Two solutions with the same first step:
First navigate to the location you keep your scripts in and copy the filepath to that directory.
First Solution:
Click "Start"
Right-click "Computer" (or "My Computer)
Click "Properties"
On the left, click "Advanced System Settings"
Click "Environment Variables"
In the "System Variables" Box, scroll down and select "PATH"
Click "Edit"
In the "Variable Value" field, scroll all the way to the right
If there isn't a semi-colon (;) there yet, add it.
Paste in the filepath you copied earlier.
End with a semi-colon.
Click "OK"
Click "OK" again
Click "OK" one last time
You can now use any of your scripts as if you were already that folder.
Second Solution: (can easily be paired with the first for extra usefulness)
On your desktop create a batch file with the following content.
#echo off
cmd /k cd "C:\your\file\path"
This will open a command window like what you tried to do.
For tons of info on windows commands check here: http://ss64.com/nt/
Create a new file startCmdLine.bat in your directory and put this line in it
call cmd
That is it. Now double click on the .bat file. It works for me.
You can replace call with start, it will also work.
this code works for me
name it cmd.bat
#echo off
title This is Only A Test
echo.
:Loop
set /p the="%cd%"
%the%
echo.
goto loop
you can try:
shift + right click
then, click on Open command prompt here
Inside given folder click on the top Adddress Bar and type cmd and click enter
It will open command prompt with current folder address.
You can simply create a bat file in any convenient place and drop any file from the desired directory onto it.
Haha. Code for this:
cmd

Resources