To fix this issue you can use the following code (this is not a very elegant solution, but it will work reasonably well). Note that I'm using c:\temp
to test rather than your home directory, but its the same concept:
alias cloud=mklink /J "C:\temp\OneDrive - chjensen\$1" $2 $3 $4 $5 $6 $7 $8 $9
This would be the MS cmd command:
cloud sublime "C:\Program Files\Sublime Text 3\Data\Packages"
Here's an easy way to debug the alias, but using an echo
command to put it to the terminal:
alias cloudp=echo mklink /J "C:\temp\OneDrive - chjensen\$1" $2 $3 $4 $5 $6 $7 $8 $9
As you've already mentioned, the issue that you're running into is that $2
is translating into "C:\Program
, $3
=Files\Sublime
, etc. With this inelegant solution, it will simply put them all back together again, with the spaces and perhaps some trailing spaces that we don't care about after the last double quote.
You could experiment with escaping each space via the carrot symbol, e.g., C:\Program^ Files\Sublime^ Text^ 3\Data\Packages
, but I don't think that it will work and you don't want to remember to put a carrot symbol every time.
If you can remove the spaces from your OneDrive path (spaces are evil, especially in the Microsoft cmd prompt), you could solve this much more elegantly via:
alias cloud2=mklink /J C:\temp\OneDrive_chjensen\$*
The $*
means everything after the cloud
command.
Given what I've said above, what I would actually suggest...
if at all possible, transition to using the PowerShell console in Cmder. It will be so much simpler than using the doskey based alias'. I wrote about a method that I use to pull in .ps1
files to the PowerShell consoles in this stackoverflow answer.
I've created an example of some PowerShell that you could use (depending on your PowerShell version) to do the same thing, but with a lot more error checking and flexibility. See this link for more on PowerShell symbolic links.
Additionally, consider using $env:USERPROFILE
instead of C:\Users\chjensen
to make the script more reusable on other machines (also, $env:ProgramFiles
instead of c:\Program Files
).
function Get-DestPath
{
[CmdletBinding()]
param ( [AllowNull()][String] $DestPath )
if (!$DestPath)
{
# set to default
$DestPath = 'C:\temp\OneDrive - chjensen\'
}
return $DestPath
}
function New-SymbolicLinkItem
{
[CmdletBinding(SupportsShouldProcess=$true)]
param
(
[Parameter(Mandatory=$true)][String] $NewLinkName
,[Parameter(Mandatory=$true)][String] $ItemToLink
,[AllowNull()][String] $DestPath
)
$DestPath = Get-DestPath -DestPath $DestPath
Write-Verbose -Message ('New Link: NewLinkName={0}, ItemToLink={1}, DestPath={2}' -f $NewLinkName, $ItemToLink, $DestPath)
if ($PSCmdlet.ShouldProcess($ItemToLink, 'New-SymbolicLinkItem'))
{
try
{
New-Item -ItemType SymbolicLink -Path $DestPath -Name $NewLinkName -Value $ItemToLink -ErrorAction Stop
}
catch
{
('Error creating link: {0}, line number: {1}' -f $_, $_.InvocationInfo.ScriptLineNumber)
}
}
}
Set-Alias -Name cloud -Value New-SymbolicLinkItem
You would use this command the same way in a PowerShell console as with the alias via:
cloud sublime "C:\Program Files\Sublime Text 3\Data\Packages"
I hope that this helps. Let me know if you have any questions.