获取DOS路径而不是Windows路径

Cod*_*ber 95 windows command-line

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

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

我知道运行dir /x会给我当前目录中的文件/目录的短名称,但我无法找到以短名称格式显示当前目录的完整路径的方法.我必须按照从根目录开始的路径,逐个目录,dir /x在每个路径中运行.

我确定有更简单的方法吗?

Tim*_*mbo 151

for %I in (.) do echo %~sI
Run Code Online (Sandbox Code Playgroud)

有更简单的方法吗?

  • 如果你从批处理脚本中调用它,则必须转义`%`符号:`for %% I in("C:\ folder with spaces")do echo %% ~sI` (6认同)
  • 找到我的答案:`for/d%I in(*)do @echo%~sI`每个路径段都很短,很棒.麻烦不是直接与长名称,甚至是空间,虽然痛苦,但最糟糕的是当国际字符存在时,只是软管我的脚本将此目录列表作为输入. (3认同)
  • 在xp上使用中文字符对我不起作用 (3认同)
  • 这非常蹩脚,乐于助人. (2认同)

Tri*_*ped 39

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

dir <ParentDirectory> /X
Run Code Online (Sandbox Code Playgroud)

哪里<ParentDirectory>替换为包含您想要名称的项目的目录的完整路径.

虽然输出并不像Timbo的答案那么简单,但它会列出指定目录中的所有项目,并使用实际名称和(如果不同)短名称.

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

在Windows 7 x64上测试.


gil*_*ly3 28

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

@ECHO OFF
echo %~s1
Run Code Online (Sandbox Code Playgroud)

我打电话给我,shortNamePath.cmd并称之为:

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

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

@ECHO OFF
if '%1'=='' (%0 .) else echo %~s1
Run Code Online (Sandbox Code Playgroud)

没有参数调用:

C:\Program Files (x86)\Android\android-sdk>shortNamePath
C:\PROGRA~2\Android\ANDROI~1
Run Code Online (Sandbox Code Playgroud)


Tae*_*hin 10

作为程序员做了这个10分钟的Winform项目.这对我有用.将此应用程序设置为文件资源管理器的上下文菜单可以节省更多点击.

10分钟的申请

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();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


gul*_*ndr 7

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

> cd "long path name"
> command
Run Code Online (Sandbox Code Playgroud)

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

资源

  • Windows 7没有command.com,至少没有x64版本. (18认同)
  • 上面的工作在Win7 32bit上 - 我刚刚做到了.但你是对的,它不适用于64位(也经过测试). (2认同)
  • 在Windows 8 64bit上都没有 (2认同)

rai*_*tin 5

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

for %I in (.) do echo %~sI
Run Code Online (Sandbox Code Playgroud)

对于HardLinks上的MsDos文件名

使用创建的硬链接mklink /H <link> <target>将没有MsDos短文件名。

如果您dir /X和您发现缺少的短名称,则应注意以下几点:

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

 Directory of d:\personal\photos-tofix\2013-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
Run Code Online (Sandbox Code Playgroud)

普通文件

在这种情况下

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

我有我所期望的

d:\personal\PH124E~1\2013-P~3\2013-1~1.JPG
Run Code Online (Sandbox Code Playgroud)

硬链接文件

在这种情况下

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

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

d:\personal\PH124E~1\2013-P~3\2013-12-22--12-10-52------Bulevardul-Petrochimi?tilor.jpg`
Run Code Online (Sandbox Code Playgroud)