What is the alternative for ~ (user's home directory) on Windows command prompt?

前端 未结 10 1485
离开以前
离开以前 2020-11-30 18:10

I\'m trying to use the command prompt to move some files, I am used to the linux terminal where I use ~ to specify the my home directory I\'ve looked everywhere

相关标签:
10条回答
  • 2020-11-30 19:04

    Update - better version 18th July 2019.

    Final summary, even though I've moved on to powershell for most windows console work anyway, but I decided to wrap this old cmd issue up, I had to get on a cmd console today, and the lack of this feature really struck me. This one finally works with spaces as well, where my previous answer would fail.

    In addition, this one now is also able to use ~ as a prefix for other home sub-folders too, and it swaps forward-slashes to back-slashes as well. So here it is;

    Step 1. Create these doskey macros, somewhere they get picked up every time cmd starts up.

    DOSKEY cd=cdtilde.bat $* 
    DOSKEY cd~=chdir /D "%USERPROFILE%"
    DOSKEY cd..=chdir ..
    

    Step 2. Create the cdtilde.bat file and put it somewhere in your PATH

    @echo off
    
    set dirname=""
    set dirname=%*
    set orig_dirname=%*
    
    :: remove quotes - will re-attach later.
    set dirname=%dirname:\"=%
    set dirname=%dirname:/"=%
    set dirname=%dirname:"=%
    
    :: restore dirnames that contained only "/"
    if "%dirname%"=="" set dirname=%orig_dirname:"=%
    
    :: strip trailing slash, if longer than 3
    if defined dirname if NOT "%dirname:~3%"==""  (
        if "%dirname:~-1%"=="\" set dirname="%dirname:~0,-1%"
        if "%dirname:~-1%"=="/" set dirname="%dirname:~0,-1%"
    )
    
    set dirname=%dirname:"=%
    
    :: if starts with ~, then replace ~ with userprofile path
    if %dirname:~0,1%==~ (
        set dirname="%USERPROFILE%%dirname:~1%"
    )
    set dirname=%dirname:"=%
    
    :: replace forward-slashes with back-slashes
    set dirname="%dirname:/=\%"
    set dirname=%dirname:"=%
    
    chdir /D "%dirname%"
    

    Tested fine with;

    cd ~ (traditional habit)
    cd~  (shorthand version)
    cd.. (shorthand for going up..)
    cd / (eg, root of C:)
    cd ~/.config (eg, the .config folder under my home folder)
    cd /Program Files (eg, "C:\Program Files")
    cd C:/Program Files (eg, "C:\Program Files")
    cd \Program Files (eg, "C:\Program Files")
    cd C:\Program Files (eg, "C:\Program Files")
    cd "C:\Program Files (eg, "C:\Program Files")
    cd "C:\Program Files" (eg, "C:\Program Files")
    

    Oh, also it allows lazy quoting, which I found useful, even when spaces are in the folder path names, since it wraps all of the arguments as if it was one long string. Which means just an initial quote also works, or completely without quotes also works.

    All other stuff below may be ignored now, it is left for historical reasons - so I dont make the same mistakes again


    old update 19th Oct 2018.
    In case anyone else tried my approach, my original answer below didn't handle spaces, eg, the following failed.

    > cd "c:\Program Files"
    Files""]==["~"] was unexpected at this time.
    

    I think there must be a way to solve that. Will post again if I can improve my answer. (see above, I finally got it all working the way I wanted it to.)


    My Original Answer, still needed work... 7th Oct 2018.
    I was just trying to do it today, and I think I got it, this is what I think works well;

    First, some doskey macros;

    DOSKEY cd=cdtilde.bat $* 
    DOSKEY cd~=chdir /D "%USERPROFILE%"
    DOSKEY cd..=chdir ..
    

    and then then a bat file in my path;

    cdtilde.bat

    @echo off
    if ["%1"]==["~"] ( 
        chdir /D "%USERPROFILE%"
    ) else ( 
        chdir /D %* 
    )
    

    All these seem to work fine;

    cd ~ (traditional habit)
    cd~  (shorthand version)
    cd.. (shorthand for going up..)
    
    0 讨论(0)
  • 2020-11-30 19:07

    Simply

    First Define Path

    doskey ~=cd %homepath%
    

    Then Access

    ~
    
    0 讨论(0)
  • 2020-11-30 19:12

    You can do almost the same yourself. Open Environment Variables and click "New" Button in the "User Variables for ..." .
    Variable Name: ~
    Variable Value: Click "Browse Directory..." button and choose a directory which you want.

    And after this, open cmd and type this:
    cd %~%
    . It works.

    0 讨论(0)
  • 2020-11-30 19:14

    Just wrote a script to do this without too much typing while maintaining portability as setting ~ to be %userprofile% needs a manual setup on each Windows PC while cloning and setting the directory as part of the PATH is mechanical.

    https://github.com/yxliang01/Snippets/blob/master/windows/

    0 讨论(0)
提交回复
热议问题