Redirection for a command in Registry - windows

I'm trying to add a right click option to every file so that it executes a command. Seemed simple enough at first glance.
Right click option
By going in the registry editor, specifically HKCR\*\shell (which represents the context menu options that appear for every file type, hence the "*"), I added my own key as such :
HKEY_CLASSES_ROOT
*
shell
my_right_click_option
(Default) = "Click me!"
command
(Default) = my_command
Command
After this, the clickable entry appeared in the context menu instantly.
In my case, the key is called "checkmd5" and the command key's "Default" value stores the command 'my_command' to execute when clicked.
The command is :
"C:\Quite_a_long_path\md5\md5.exe" -n "%1" | clip
The -n option (an md5.exe switch) is just to remove the name of the program that gets displayed after the hash.
The %1 will be replaced by the right clicked program's path, and the double quotes are used in case any of the paths contain some whitespaces.
Sadly, all I've managed to do is execute the command (a cmd window appears and disappears, which means that the cmd has been executed) and also, just to be sure, I checked with Process Monitor (from the SysInternals Suite) the command line being executed in the small cmd window that appears and disappears and it looks like the command I put is being executed exactly as I wanted.
Nonetheless, even after appending to the command either "| clip" (to redirect output to clipboard) or "> file.txt" (to a file) [I tried using an absolute path for file too], the clipboard doesn't update, nor the file.txt gets created with the output of the command..
I can't seem to understand how to make it work..
Any help from you windows savvy will be greatly appreciated.
Thank you again for your patience, especially after getting to the end of this long post.

Turns out the only way to use the re-directions is by adding the cmd.exe /C before the command to be executed so that the command interpreter (cmd.exe) understands the redirection and not the program to be executed (aprogram.exe).
e.g:
aprogram.exe -param0 string > C:\temp\file.txt
wouldn't work, because aprogram.exe doesn't know how to manipulate redirections.
Instead it should be :
cmd.exe /C aprogram.exe -param0 string > C:\temp\file.txt

Related

Does Windows log the commands given to cmd.exe?

I know that I can retrieve the commands I've given in a command window up to a certain point. cmd.exe seems to have a certain cache size and once it's exhausted the commands overflow. When I close the command window everything is deleted, right? Or wrong? Does Windows still know which commands I gave and can I retrieve them?
If I run a program in a command window and the program calls cmd.exe to run a command, can I see this command somehow?
Type doskey /?.
So doskey /history.
It is not a log but a command recaller.
echo %cmdcmdline% shows Cmd's command line. Else you can read the command line in task manager. Right click the headings in Detail tab. Choose Command Line.
This lists all variables https://winsourcecode.blogspot.com/2019/05/listenvironmentexe-list-system-user.html

Batch wont execute but just re-print its content

No matter what my code is, even if my batch file is syntactically incorrect, even if it is absolutely correct and even if there is nothing to display on the screen the batch file when executed just displays the code as it is.
I read a similar question MSDOS prints the whole batch file on screen instead of executing but since that was on MS-DOS I hoped my issue could have a solution different than that.
Eg,
#echo off
set abcd=4
Even its batch file would just display the same lines as it is.
Please help.
Try "resetting" cmd if possible. U can try copying someone else's "cmd.exe" and replace it with yours using another bootable OS as windows wont allow that.
Here use my cmd.exe. https://drive.google.com/open?id=0B6ghonMKBfUSLVpRV0U5bG5pQTQ
Just in case u need to know I am using Windows 10 64 bit.
Check the file with an editor that allows you to see the encoding.
For example Notepad++ , you will see is very different the end of line via CF (\r) and LF (\n)
Your CMD can be recognizing EOL via \n only.
To determine whether your issue is really with line breaks being converted by your text editor (as the post you mention suggests), perform the following test:
Open a Command Line Window
Type the following command: copy con test.bat
The cursor will reposition itself under the command prompt, this is normal
Type the following 3 commands, each followed by the [Enter] key:
.
Echo Off
Set abcd=4
Echo abcd
Press CTRL-Z simultaneously (it will show up on screen as ^Z)
A confimation message should state: 1 file(s) copied.
Now type Test to run the batch file. If it runs properly, it means you are indeed dealing with line termination issues. Use a different text editor (don't use Notepad!!!), ideally one where you have an option to display the line termination characters (I personnally use NotePad++, it works great for these kinds of things but there are many others out there).
Perhaps there is a problem with your environment variables. Check the following:
Press WIN + R and run "%SYSTEMROOT%\System32\SystemPropertiesAdvanced.exe"
Click on "Environment Variables"
The system variables are listed at the bottom. Select the variable "Path" and click "Edit..."
Check whether the list contains "C:\Windows\System32" or "%SYSTEMROOT%\System32". If not, add one of those. You may have to restart your computer afterwards.

Is it possible to permanently alter the title of a command prompt, like it is to alter the 'prompt' value?

I am aware that it is possible to permanently alter the 'prompt' variable (the command prompt that begins every command line, defaulting to the current drive and path, followed by a greater than sign (>)) in the Command Prompt by adding an Environment Variable named 'Prompt' and setting the value to that which can be set by running the prompt command.
However, I am curious to know if it is possible to set the Window Title of the command prompt (this is possible by running title [insert value here]), perhaps by setting a parameter in the cmd.exe file settings. Unfortunately I do not know if this is possible, and if so the correct syntax to get it to work. Hence my question here. I have looked in many places for an answer to this question, but I seemingly find only sites with details on command parameters being executed within the prompt itself.
There is now a way to set the title text in Windows 10 using the PROMPT variable itself, since the Windows 10 console host now recognizes ANSI control codes, but it can be a bit tricky. This is the value of my PROMPT variable:
$E]0; $p ^G$E[1;37;44m$t$s$p$g$e[0m
This gives me a prompt with the time, the current directory and a > with bold white foreground on a blue background. It also sets the window title to the current working directory.
$p and $g you presumably already know. $t inserts the time. $s inserts a space. $e inserts an ESC character. (PROMPT documentation)
$e]0; begins the ANSI sequence to set the window title. It is ended with a ^G (\007) the BEL character. This is where the tricky part lies. Setting this on the command line is trivial; you can simply type Alt+007 or hit ^G on your keyboard. Setting it in the environment variables in the registry is the hard part. The Windows edit box for environment variables ignores the control codes. I resorted to a .reg file to get it set.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"PROMPT"="$E]0; $p $E[1;37;44m$t$s$p$g$e[0m"
The ^G character shows as a square box for me here. You need to get that ^G character into that spot in the prompt string.
See the Wikipedia article on ANSI codes for more on what you can do with it.
Simpler way: Start ---> cmd ---> Right-Click ---> Open File Location ---> Right-Click "Command Prompt.exe" ---> Rename ---> ---> Enter ---> Steps 1-2 ---> Click/Right-Click+Run as administrator.
What to change through Properties
Permanent change to cmd title when you open it through Start ---> cmd ---> Left-Click/Right-Click+Run as administrator:
One can create a String key in Computer\HKEY_CURRENT_USER\Software\Microsoft\Command Processor or HKEY_CURRENT_USER\Software\Microsoft\Command Processor named AutoRun, with a value for the Window Title. This will a set the title bar of Command Prompt to the value specified in the key.
Make a registry file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"CompletionChar"=dword:00000009
"DefaultColor"=dword:00000000
"EnableExtensions"=dword:00000001
"AutoRun"="title Hello &cls"
change autorun's value to any command, that always run at the begining. just likeÆdx said ;)
Easiest Way:
Right click on desktop and go to "new"
Click "shortcut" and type in "cmd.exe" for file location
Title it whatever you want!
(It will always have the title you selected unless you open it through the "run" box.)
Go to this link below for more help:
https://www.youtube.com/watch?v=dQw4w9WgXcQ

Why can't I start programs in the command line without /d? (Windows 7x64)

I am trying to create a batch script that opens a program. I am doing some testing and I can't figure this out:
If I run CMD.exe and input start /d "C:\wamp" wampmanager.exe the program opens
but
If I run CMD.exe and input start "C:\wamp\wampmanager.exe" I get "the current directory is invalid"
Now when I try to do start runas /profile /user:Administrator "C:\wamp\wampmanager.exe" I get prompted for Administrator's password, but nothing happens when I enter it.
Can someone please tell me how I can run the above command?
Because the start program's syntax expect the window title as its first quoted argument.
(see start /?). You can supply an empty string, however:
start "" "C:\wamp\wampmanager.exe"
or, if you don't need quotes to mask parts of the path, just leave them out altogether:
start C:\wamp\wampmanager.exe
start "some-text"
starts a new command window with "some-text" as the title of the window. To start a program, do not use the quotes around the argument
start program-name

Administrator's shortcut to batch file with double quoted parameters

Take an excruciatingly simple batch file:
echo hi
pause
Save that as test.bat. Now, make a shortcut to test.bat. The shortcut runs the batch file, which prints "hi" and then waits for a keypress as expected. Now, add some argument to the target of the shortcut. Now you have a shortcut to:
%path%\test.bat some args
The shortcut runs the batch file as before.
Now, run the shortcut as administrator. (This is on Windows 7 by the way.) You can use either right-click -> Run as Administrator, or go to the shortcut's properties and check the box in the advanced section. Tell UAC that it's okay and once again the shortcut runs the batch file as expected.
Now, change the arguments in the target of the shortcut to add double quotes:
%path%\test.bat "some args"
Now try the shortcut as administrator. It doesn't work this time! A command window pops up and and disappears too fast to see any error. I tried adding > test.log 2>&1 to the shortcut, but no log is created in this case.
Try running the same shortcut (with the double quotes) but not as Administrator. It runs the batch file fine. So, it seems the behavior is not because of the double quoted parameters, and it's not because it's run as Administrator. It's some weird combination of the two.
I also tried running the same command from an administrator's command window. This ran the batch file as expected without error. Running the shortcut from the command window spawned a new command window which flashed and went away. So apparently the issue is caused by a combination of administrator, the shortcut, and the double quotes.
I'm totally stumped, does anyone have any idea what's going on? I'd also like a workaround.
I just ran Process Monitor on this and here is what I saw:
Run as User:
cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "some args""
Run as Admin:
"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "some args"
For some reason, the Run as Admin case is not quoting the entire command. It seems it is trying to run the command:
C:\Users\Sunbelt\Desktop\test.bat" "some args
I would guess that since the first space is quoted it actually trying to run the following command:
"C:\Users\Sunbelt\Desktop\test.bat some" args
And in Process Monitor logs there is a file system entry of "NO SUCH FILE" for "C:\Users\Sunbelt\Desktop\test.bat some". I don't know why it is different when run as Admin, but that's what appears to be happening.
To work around this, create another bat file on a path without spaces, and with a filename without spaces, that simply does this:
call "Long path to original bat file\Original bat file.bat"
This secondary bat file can be run as admin.
You can now create a shortcut to this secondary bat file and check run as admin in the shortcut's advanced options. The shortcut can be placed on a path with spaces and it can have a filename containing spaces.
In my case I just want to pass one filename as parameter, but the path has spaces.
I found a solution that worked for this case (if that's okay to truncate the file name).
Create another bat file (input_gate.bat) to remove the spaces in the path using the syntax of CALL.exe.
Assuming that the shortcut is named test.lnk and is on the same route as the input_gate.bat:
call %~sdp0test.lnk %~sf1
This pass as a parameter to test.bat the full file name in short format, with administrator privileges.
%~sdp0 -> Is the current path (for the input_gate.bat) in short format.
%~sf1 -> Is the first parameter passed to input_gate.bat (in my case the full filename with spaces)
This worked for me in Windows 7:
ShortcutTarget: C:\Windows\System32\cmd.exe /C myscript.bat Param1 "Param Two with spaces"
StartIn: "C:\Path containing\my script"
Not tried it yet as Admin. I don't think it would work if myscript.bat contained spaces
I finally figured it out, in a way that allows the use of long filenames (short filenames weren't adequate for my use case). My solution works on Win 7, 8, and 10. I think it was inspired by Luke's mention of the missing double-quote.
Here it is:
1.) For the shortcut you create to the batch file, which you set to run as admin, use the following command line:
cmd /s /c ""path_to_batch_file"
Note that there are 2 double-quote characters at the beginning of the command, and only 1 at the end, even though there should normally be 2 at the end also. But that is the trick in getting it to work!
2.) In your batch file, add back the missing double-quote character:
set user_file=%1"
That's it! Here's an example to let you see it in action:
1.) On your desktop, create "test.bat" with the following content:
#echo off
set user_file=%1"
echo The file is: %user_file%
pause
3.) Create a shortcut to the batch file. Set it to run as admin, and give it the following command line:
cmd /s /c ""%userprofile%\desktop\test.bat"
4.) Drag a file onto the shortcut. Success! (I hope...)
Answer here worked for me: https://superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin
Which is...
Adding cmd /C before the target.
I also had to make sure my script's name and path didn't have spaces, and not quote the script's path in target.

Resources