获取 DOS 路径而不是 Windows 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4051088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:42:08  来源:igfitidea点击:

Get DOS path instead of Windows path

windowscommand-line

提问by CodeClimber

In a DOS window, how can I get the full DOS name/short name of the directory I am in?

在 DOS 窗口中,如何获得我所在目录的完整 DOS 名称/短名称?

For example, if I am in the directory C:\Program Files\Java\jdk1.6.0_22, I want to display it's short name C:\PROGRA~1\Java\JDK16~1.0_2.

例如,如果我在目录中C:\Program Files\Java\jdk1.6.0_22,我想显示它的短名称C:\PROGRA~1\Java\JDK16~1.0_2

I know running dir /xwill give me the short names of files/directories in the current directory but I haven't been able to find a way to display the full path of the current directory in short name format. I'm having to work my way through the path from the root, directory by directory, running dir /xin each.

我知道运行dir /x会给我当前目录中文件/目录的短名称,但我还没有找到一种方法来以短名称格式显示当前目录的完整路径。我必须通过从根目录开始的路径,逐个目录,dir /x在每个目录中运行。

I'm sure there is an easier way to do this?

我确定有更简单的方法来做到这一点?

回答by Timbo

for %I in (.) do echo %~sI

Any simpler way?

有没有更简单的方法?

回答by Trisped

You could also enter the following into a CMD window:

您还可以在 CMD 窗口中输入以下内容:

dir <ParentDirectory> /X

Where <ParentDirectory>is replaced with the full path of the directory containing the item you would like the name for.

Where<ParentDirectory>替换为包含您要为其命名的项目的目录的完整路径。

While the output is not a simple as Timbo's answer, it will list all the items in the specified directory with the actual name and (if different) the short name.

虽然输出不像Timbo's answer那样简单,但它会列出指定目录中的所有项目,并带有实际名称和(如果不同)短名称。

If you do use for %I in (.) do echo %~sIyou can replace the .with the full path of the file/folder to get the short name of that file/folder (otherwise the short name of the current folder is returned).

如果确实使用for %I in (.) do echo %~sI,则可以将 替换为.文件/文件夹的完整路径以获取该文件/文件夹的短名称(否则返回当前文件夹的短名称)。

Tested on Windows 7 x64.

在 Windows 7 x64 上测试。

回答by gilly3

In windows batch scripts, %~s1expands path parameters to short names. Create this batch file:

在 Windows 批处理脚本中,%~s1将路径参数扩展为短名称。创建此批处理文件:

@ECHO OFF
echo %~s1

I called mine shortNamePath.cmdand call it like this:

我打电话给我的shortNamePath.cmd,这样称呼它:

c:\>shortNamePath "c:\Program Files (x86)\Android\android-sdk"
c:\PROGRA~2\Android\ANDROI~1

Edit:here's a version that uses the current directory if no parameter was supplied:

编辑:如果没有提供参数,这是一个使用当前目录的版本:

@ECHO OFF
if '%1'=='' (%0 .) else echo %~s1

Called without parameters:

不带参数调用:

C:\Program Files (x86)\Android\android-sdk>shortNamePath
C:\PROGRA~2\Android\ANDROI~1

回答by Tae-Sung Shin

Being a programmer made this 10-minute Winform project. It's been useful for me. Making this app to a context menu for file explorer would save more clicks.

作为一名程序员,制作了这个 10 分钟的 Winform 项目。它对我很有用。将此应用程序设置为文件资源管理器的上下文菜单将节省更多点击次数。

10-minute application

10分钟申请

Form1.cs:

Form1.cs:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace ToShortPath
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetShortPathName(
                 [MarshalAs(UnmanagedType.LPTStr)]
                   string path,
                 [MarshalAs(UnmanagedType.LPTStr)]
                   StringBuilder shortPath,
                 int shortPathLength
                 );
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Show the dialog and get result.
            var openFileDialog1 = new OpenFileDialog();
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var openFileDialog1 = new FolderBrowserDialog();
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                textBox1.Text = openFileDialog1.SelectedPath;
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            StringBuilder shortPath = new StringBuilder(65000);
            GetShortPathName(textBox1.Text, shortPath, shortPath.Capacity);
            textBox2.Text = shortPath.ToString();
        }

    }
}

Form1.Designer.cs:

Form1.Designer.cs:

namespace ToShortPath
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(69, 13);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(516, 53);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(69, 72);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.ReadOnly = true;
            this.textBox2.Size = new System.Drawing.Size(516, 53);
            this.textBox2.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(7, 35);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Long Path";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(7, 95);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(57, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Short Path";
            // 
            // button1
            // 
            this.button1.AutoSize = true;
            this.button1.Location = new System.Drawing.Point(591, 13);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(40, 53);
            this.button1.TabIndex = 4;
            this.button1.Text = "File";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.AutoSize = true;
            this.button2.Location = new System.Drawing.Point(637, 12);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(46, 53);
            this.button2.TabIndex = 5;
            this.button2.Text = "Folder";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(687, 135);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Short Path";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
}

回答by gulbrandr

run cmd.exeand do the following:

运行cmd.exe并执行以下操作:

> cd "long path name"
> command

Then command.com will come up and display only short paths.

然后 command.com 将出现并仅显示短路径。

source

来源

回答by raisercostin

Kimbo's answer is perfect for normal files.

Kimbo 的回答非常适合普通文件。

for %I in (.) do echo %~sI

For MsDos file names on HardLinks

对于 HardLinks 上的 MsDos 文件名

The hard links created with mklink /H <link> <target>will not have an MsDos short file name.

创建的硬链接mklink /H <link> <target>不会有 MsDos 短文件名。

In case you dir /Xand you discover that missing short name you should expect the followings:

如果您dir /X和您发现缺少短名称,您应该期待以下内容:

d:\personal\photos-tofix13-proposed1-bad>dir /X
 Volume in drive D has no label.
 Volume Serial Number is 7C7E-04BA

 Directory of d:\personal\photos-tofix13-proposed1-bad

03/02/2015  15:15    <DIR>                       .
03/02/2015  15:15    <DIR>                       ..
22/12/2013  12:10         1,948,654 2013-1~1.JPG 2013-12-22--12-10-42------Bulevardul-Petrochimi?tilor.jpg
22/12/2013  12:10         1,899,739              2013-12-22--12-10-52------Bulevardul Petrochimi?tilor.jpg

Normal file

普通文件

In this case

在这种情况下

> for %I in ("2013-12-22--12-10-42------Bulevardul-Petrochimi?tilor.jpg") do echo %~sI

I've got what I expected

我得到了我的期望

d:\personal\PH124E~113-P~313-1~1.JPG

Hard link file

硬链接文件

In this case

在这种情况下

> for %I in ("2013-12-22--12-10-52------Bulevardul-Petrochimi?tilor.jpg") do echo %~sI

I've got the normal MsDos path but the normal filename.

我有正常的 MsDos 路径,但有正常的文件名。

d:\personal\PH124E~113-P~313-12-22--12-10-52------Bulevardul-Petrochimi?tilor.jpg`

回答by ClearBlueSky85

similar to this answerbut uses a sub-routine

类似于this answer但使用子程序

@echo off
CLS

:: my code goes here
set "my_variable=C:\Program Files (x86)\Microsoft Office"

echo %my_variable%

call :_sub_Short_Path "%my_variable%"
set "my_variable=%_s_Short_Path%"

echo %my_variable%

:: rest of my code goes here
goto EOF

:_sub_Short_Path
set _s_Short_Path=%~s1
EXIT /b

:EOF

回答by Steven Davis

A someone more direct answer is to fix the bug.

一个更直接的答案是修复错误。

%SPARK_HOME%\bin\spark-class2.cmd; Line 54
Broken: set RUNNER="%JAVA_HOME%\bin\java"
Windows Style: set "RUNNER=%JAVA_HOME%\bin\java"

%SPARK_HOME%\bin\spark-class2.cmd; 第 54 行
Broken: set RUNNER="%JAVA_HOME%\bin\java"
Windows Style: set "RUNNER=%JAVA_HOME%\bin\java"

Otherwise, the RUNNER ends up with quotes, and the command "%RUNNER%" -Xmx128m ...ends up with double-quotes. The result is that the Program and File are treated as separate parameters.

否则,RUNNER 以引号结束,命令 "%RUNNER%" -Xmx128m ...以双引号结束。结果是程序和文件被视为单独的参数。

回答by Jj Rivero

$fso = New-Object -com scripting.filesystemobject
$fso.GetFolder('c:\Program Files (x86)').ShortName()

PROGRA~2

Inspired by Dr. Scripto's answer

灵感来自 Scripto 博士的回答

回答by Ossama

use this link, it will automatically convert any path you give to any format https://pathconverter-pp.azurewebsites.net

使用此链接,它会自动将您提供的任何路径转换为任何格式 https://pathconverter-pp.azurewebsites.net