Why is my alias in CMDER behaving differently than when I enter a file path? - cmd

I'm using CMDER and want to make this filepath: D:\Documents\Programming an alias (pg), so that I can just type "cd /d pg" and cd there. When I type this:
cd /d D:\Documents\Programming
it works just fine, but when I create an alias like this:
alias pg=D:\Documents\Programming
then enter
cd /d pg
I get this result:
The system cannot find the path specified
Why is that? Just a note, I am using CMDER but figured aliasing is the same across Terminal and Windows CMD.

alias pg="cd /d D:\Documents\Programming"
is of wrong syntax because of the single " which lets cmd.exe think it should run the file "cd /d D:\Documents\Programming". Define the alias pg with
alias pg=cd /d D:\Documents\Programming
reposted #Mofi's answer here because commments can't be marked as answers. Feel free to post your answer if you want your answer accepted and I'll delete this one!

Related

Batch: how to mklink with arguments?

How to create a symlink in windows including arguments using mklink, (and no powershell)?
I want to create on Desktop a link OpenVPN which links to "C:\Program Files\OpenVPN\bin\openvpn-gui.exe", with argument: --connect client.ovpn
So I tried:
cd %homepath%\Desktop && mklink "OpenVPN GUI" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe --connect client.ovpn"FAILED -> symlink created but unable to point to .exe.
cd %homepath%\Desktop && mklink "OpenVPN GUI" ""C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect client.ovpn"FAILED -> Obviously syntax error.
Trying to use escape character ^ and \: cd %homepath%\Desktop && mklink "OpenVPN GUI" "^"C:\Program Files\OpenVPN\bin\openvpn-gui.exe^" --connect client.ovpn"FAILED.
I tried to set a variable set patharglink="C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect client.ovpn, and use it, mklink "OpenVPN GUI" %patharglink%FAILED.
Any idea how to solve this?
Here's a complete batch-file, to create the shortcut, not symbolic link, you require.
;#If Not Exist "%UserProfile%\Desktop\OpenVPN.lnk" (
; "%__AppDir__%rundll32.exe" advpack.dll,LaunchINFSection "%~0",,1)
;#GoTo :EOF
[Version]
Signature="$Windows NT$"
[DefaultInstall]
ProfileItems=AddLnk
[AddLnk]
Name="OpenVPN",8,16
CmdLine=16422,"OpenVPN\bin\openvpn-gui.exe"," --connect client.ovpn"
InfoTip="Connect OpenVPN using client config file"
WorkingDir=0
Just save the above as OVPNLink.cmd and double click it!
I found a way to create a shortcut (not a symbolic link, as others pointed out) with arguments, mainly through this thread.
The Idea is to call a VB script, because there is an easy implementation for shortcuts:
:: make sure the linkpath exists:
if not exist "%linkpath%" md "%linkpath%"
:: create temporary VBScript ...
echo Set objShell=WScript.CreateObject("Wscript.Shell")>%temp%\MakeShortCut.vbs
echo Set objShortcut=objShell.CreateShortcut("%linkpath%\%linkname%.lnk")>>%temp%\MakeShortCut.vbs
echo objShortcut.TargetPath="%progpath%\%progexe%.exe">>%temp%\MakeShortCut.vbs
echo objShortcut.Arguments="%arguments%">>%temp%\MakeShortCut.vbs
echo objShortcut.Description="%description%">>%temp%\MakeShortCut.vbs
echo objShortcut.WorkingDirectory="%progpath%">>%temp%\MakeShortCut.vbs
echo objShortcut.Save>>%temp%\MakeShortCut.vbs
::... run it ...
cscript //nologo %temp%\MakeShortCut.vbs
::... and delete it.
del %temp%\MakeShortCut.vbs
So you will want to set the following variables before running these lines:
%linkpath% is the path where the shortcut is created
%linkname% is the name of the shortcut
%progpath% is the path to your executable
%progname% is the name of your executable
%arguments%
%description%
%temp% is not to be set, it is an environment variable
note:
I modified that code a little from the code I am using, as I have a specific use for it in my code, and did not test if I made a typo here. If anyone uses this and it works, please feel free to remove this note

Running File from Notepad Plus Plus and Current Directory

There are a number of examples on the web of how to run a file from the Notepad Plus Plus (NPP). But they all fail to account for the fact that the current working directory is the location of the NPP's executable, and not the location of the file.
Usually they go something like this:
cmd /K "$(FULL_CURRENT_PATH)"
Consider the following Python script:
with open('somefile.txt', 'a') as file:
file.write('Hello there.\n')
This file will be created in the NPP folder, which is not at all what most people would expect. Most people would want it in the same location as the Python file.
You could also do something like this, and it works as expected, but this limits you to Python files only:
<Command name="Run This Python File" Ctrl="no" Alt="no" Shift="yes" Key="116">cmd /K python "$(FULL_CURRENT_PATH)"</Command>
I would not want to add extra code to the Python script to change the current working directory, as normally this would not be needed.
I have been trying to solve this and came up with the following. This line goes in "shortcuts.xml" in the NPP folder.
<Command name="Run This File" Ctrl="yes" Alt="no" Shift="no" Key="116">cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""</Command>
So you shut down the NPP, edit the "shortcuts.xml" by adding this line, using another editor, then launch the NPP.
To run the file, use Ctrl+F5 key combination.
This works in Windows 10, but fails in Windows XP.
How can I tweak it to work in Windows XP?
Try this:
cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)
My guess would be that the problem is the improperly nested quotes in the command; I'm not sure exactly why it would work on later Windows' while failing on XP.
The command
cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""
represents
cmd /K "cd "$(CURRENT_DIRECTORY)" && "$(FULL_CURRENT_PATH)""
Even from the syntax highlighting you can see that the quotes are not quoting what you expect.
To get the desired effect, you can use this command:
cmd /K cd "$(CURRENT_DIRECTORY)" ^&^& "$(FULL_CURRENT_PATH)"
:: XML-ified:
cmd /K cd "$(CURRENT_DIRECTORY)" ^&^& "$(FULL_CURRENT_PATH)"
I run several Windows .bat files from Notepad++. To achieve this one of the entries in the <UserDefinedCommands> section of the file C:\Users\AdrianHHH\AppData\Roaming\Notepad++\shortcuts.xml is:
<Command name="CD and run file" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /C "cd /d $(CURRENT_DIRECTORY) && $(FULL_CURRENT_PATH)"</Command>
With the .bat as the current file I then use menu => Run => CD and run file.
The command line shown in the question appears to have too many " symbols. The current directory includes the drive specifier and so the CD needs the \D option.
The command I use starts cmd \C ... (rather than the \K in the question) so the command window closes automatically. My .bat files normally finish with choice /t 60 /C Y /d Y /n so I can see the command's output.
Notepad++ > F5 (Run) > then type following command
cmd /K cd "$(CURRENT_DIRECTORY)" && python "$(FULL_CURRENT_PATH)"
assuming you have setup the path, or you may use C:\Python27\python.exe or the path of your python binary, and you will run the python at the folder where the python resides in. You can also save this command to shortcut by clicking button Save....
Afterwards, you also can modify the command in toolbar > Run > Modify shortcut/delete command.

Activating a Python virtual environment & changing directory in one shortcut (with cmd)

I'm trying to do the following:
Open a CMD prompt
Activate a virtual environment
Change the current directory to my project folder
In essence, I need to execute the following commands sequentially:
C:\Envs\djangorocks\Scripts\activate
cd "D:\GitHub\steelrumors"
I've found this link, but creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
After quite a while of searching I'm still doing it manually, any help is appreciated.
"creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):"
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
Observations:
cmd \k should be cmd /k.
& should be && when using a shortcut.
You dont need all the " characters.
Try the following as the shortcut target:
cmd /k C:\Envs\djangorocks\Scripts\activate && cd D:\GitHub\steelrumors
Consider creating a batch file (e.g. c:\scripts\launchEnv.cmd) that does something like the following:
#echo off
C:\Envs\djangorocks\Scripts\activate
cd /d "D:\GitHub\steelrumors"
Then create a shortcut that invokes cmd /k c:\scripts\launchEnv.cmd .
Some notes:
the #echo off will prevent the commands from showing up in the cmd windows. If you do want to see the commands, then omit that line from your batch file
you'll need the /d param when changing directories to make sure you actually change and navigate there, independent of where the script is currently executing from.
As an extension to the great answer from #DavidPostill I've added an additional step to run a command from the newly created python env.
In my example below, I'm launching a new instance of the awesome data mining program, orange, from an anaconda env called orange. I've also cd'ed to the directory containing my orange data files. Note that I had to use the quotation marks "" to make it work.
C:\Windows\System32\cmd.exe /k "F: && cd \Dropbox\IT\Python\Orange && C:\Users\dreme\Anaconda3\Scripts\activate.bat orange && python -m Orange.canvas"

How to define customized command in cmd prompt for directory navigation?

Say I want to navigate to a directory but don't want typing the directory every time.
Is there a way to define a customized short keys in the cmd prompt like
set root=C:\Users\sz\to\this\path
So next time only typing cd root can direct me to the target root. I hope the short key will be stored permanently - when I re-open the cmd prompt, the short key is still valid.
Even better, is it possible to define a short key that only typing root (without typing cd) can do the same thing?
Thanks in advance!
doskey root=cd /d "C:\Users\sz\to\this\path"
Note: the macro is gone, if you close your cmd-window
EDIT
another way is a batchfile somewhere in the path, for example root.bat as a "one-liner":
#cd /d "C:\Users\sz\to\this\path"

How to create ls in windows command prompt?

I want to use ls in windows command prompt and make it run the dir command.
How can I do that?
You can solve this question with one simple command:
echo #dir %* > %systemroot%\system32\ls.bat
Make sure you run cmd.exe as admin first if you are on vista and up
You could:
create a batch file called ls.bat and have it contain the dir command only
add the directory where the ls.bat file exists to your PATH environment variable
You could then execute ls from a command prompt.
Its an old question but for the record:
http://gnuwin32.sourceforge.net/packages/coreutils.htm
Gives you ls and a whole lot more!
Easiest way I have found is:
Install Git for Windows
Add the bin directory of Git to your Path variable. Mine was located in C:\Program Files\Git\usr\bin.
Start a command prompt and enjoy ls in all its glory.
I have a solution but it's dirty:
Create a file named ls.bat containing only "dir".
Put it in C:\windows\system32 (or any directory in PATH env var).
That (should) works!
Edit: Something more consistent: https://superuser.com/questions/49170/create-an-alias-in-windows-xp
If you have Node.js installed on your system, you can install it from Cash, a library I wrote for Linux commands on Windows:
npm install cash-ls -g
Windows command prompt for Vista/7 will allow NTFS symbolic links, run cmd.exe as administrator then:
mklink ls %System%\dir.exe
Then set up your PATH environment variable to include the location of the link you just created.
If you want more than just the 'ls' command, you should look into cygwin.
EDIT- Just realized dir.exe is not a separate program, so this doesn't really work. But mklink and cygwin are good things to know about.
If you just want to have cmd recognize ls as an alias for dir, you can use the doskey command (from this answer on superuser).
This does not change the original command line parameter handling of the dir command.
+1 on the post above suggesting to install git for windows and add the directory bin to your path variables.
Another way I got touch, ls, and a lot of other UNIX commands working in cmd.exe on my Windows 8 and Windows 7 machines.
Go to the following site to install Cygwin
https://www.cygwin.com/install.html
Install the 32 or 64 bit version for your system. The default settings and packages should include what you need so you don't have to change anything once you get to the packages screen.
After installation, copy the Cygwin folder path to your environment path variables. For example; if you installed cygwin to C:\Cygwin, you will add the following to your environment system path variables:
;C:\Cygwin\bin
On my system I installed the 64bit version and the default folder name and path was C:\cygwin64. So i added the following to my system environment path variables:
;C:\cygwin64\bin
Restart your terminal if it's open. Then type ls and you'll see a directory listing.
See the following if you are not familiar with setting PATH environment variables:
Superuser Link 1
Superuser Link 2
my ls.bat was below
#dir %*
that can transfer cli args
ls /b
ls /w
put it in %windir% or any directory in your %PATH% variable.
Just make sure you save the file with ANSI encoding :)
you could also use cygwin and just use the ls command directly along with all the other unix command line tools you might be used to.
I recommend the following recipe.
Use DOSKEY and $* to create your ls command.
Make the command persistent by recording it in a .bat/.cmd file and add the path of the file to registry.
For example, your command may look like
DOSKEY ls=dir
DOSKEY sublime="C:\Program Files\Sublime Text 2\sublime_text" $*
$* is useful for commands that take on arguments. For example, here I like to be able to do sublime my_code.c.
The registry for cmd is at HKEY_CURRENT_USER -> Software -> Microsoft -> Command Processor. Create a string valued entry called AutoRun with the full path of the file (not the containing folder) such as %USERPROFILE%\custom_command.cmd. Then each time cmd is run, your command will be loaded!
You can add more useful stuffs to the batch file too. See here for an example template.
Another solution that worked for me is to use UnxUtils, which adds multiple utilities from executable files (including ls, sed, and grep).
To use: download source code. Unzip. Add the UnxUtils\usr\local\wbin path to the Windows PATH variable. Start a new CMD instance.
The most easiest way is
install git
add C:\Program Files\Git\usr\bin to your path variable
now you can use ls
Someone who uses Linux Subsystem for Windows could call ls from the Linux bash. The following Command creates the ls Command in System32:
echo #bash -c "ls %*" > %systemroot%\system32\ls.bat
(The Linux Subsystem feature must be enabled/installed first)
You could follow this guide:
https://gist.github.com/vladikoff/38307908088d58af206b
TL;DR: pass /K path/to/custom/init_cmd.bat to your "shell startup" command.
I'm using ConsoleZ as my shell wrapper, so in my case I can find the setup option in "tabs", then I set the shell path to "C:\Windows\System32\cmd.exe "/K C:\cmd_init.bat""
like this.
Where C:\cmd_init.bat is the batch script containing my macros, here's what I would go for:
#echo off
doskey ls=dir /b
rem other macro stuff..
Sorry for formatting and other mistakes, this is my first time answering here.
I hope it helps!
Create an alias in .bat or .cmd file using doskey key:
#echo off
title "ls command cmd bar"
doskey ls=echo off $T dir $* $T echo on
Enjoy =)
Surely ls would not work as a unix command for the batches. If you check %1 for -l or -a etc. and all combinations of them, it would work...
Here is my C# source code and binary.
Just add ls.exe somewhere and add the path to the path environment variable.

Resources