Windowsパスの代わりにDOSパスを取得


99

DOSウィンドウで、現在のディレクトリの完全なDOS名/短縮名を取得するにはどうすればよいですか?

たとえば、ディレクトリにいる場合は、C:\Program Files\Java\jdk1.6.0_22短い名前を表示しC:\PROGRA~1\Java\JDK16~1.0_2ます。

実行dir /xすると現在のディレクトリにあるファイル/ディレクトリの短い名前が表示されることはわかっていますが、現在のディレクトリの完全パスを短い名前の形式で表示する方法を見つけることができませんでした。私はルートからのパスをディレクトリdir /xごとに実行し、それぞれで実行する必要があります。

これを行う簡単な方法があると思いますか?


2
ここで質問するとどうなりますか?DOSまたはMS-DOSでタグ付けされた何百もの質問があります。
CodeClimber 2010年

おそらく、DOSまたはMS_DOSに関連するプログラミングの質問ですか?
Pascal Cuoq

1
メールやビデオのタグが付けられた何千もの質問がありますが、これはたとえばメールにビデオを添付する方法などについて尋ねる場所ではありません...
Guffa

1
質問するのは完全に有効な質問だと思うので、反対票はありません。
CodeClimber 2010年

12
私はそれがここで尋ねられてうれしいです-以下の答えは私を助けました。
monojohnny、2011年

回答:


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

もっと簡単な方法は?


2
これはとても下手で、役に立ちます。
elgabito 2011

わかりましたが、ディレクトリ名を含める方法は?
Marcos

3
私の答えが見つかりました:for /d %I in (*) do @echo %~sI すべてのパスセグメントは短く、素晴らしいです。問題は、長い名前に直接関係するものではなく、スペースも苦痛でしたが、最悪の場合、このdirリストを入力として受け取るスクリプトにホースをかけるだけの国際的な文字が存在する場合です。
Marcos

驚くばかり!とても役に立ちました。
kulNinja 2013年

6
あなたは脱出する必要がバッチスクリプトからこれを呼び出している場合%の兆候を:for %%I in ("C:\folder with spaces") do echo %%~sI
イゴール・ポポフ

41

CMDウィンドウに次のように入力することもできます。

dir <ParentDirectory> /X

where <ParentDirectory>は、名前を付けたいアイテムを含むディレクトリの完全パスに置き換えられます。

出力はTimboの回答のように単純ではありませんが、指定されたディレクトリ内のすべてのアイテムが実際の名前と(異なる場合は)短い名前でリストされます。

使用するfor %I in (.) do echo %~sI場合.は、をファイル/フォルダーの絶対パスに置き換えて、そのファイル/フォルダーの短い名前を取得できます(そうでない場合、現在のフォルダーの短い名前が返されます)。

Windows 7 x64でテスト済み。


29

Windowsバッチスクリプトで、%~s1パスパラメータを短い名前に展開します。次のバッチファイルを作成します。

@ECHO OFF
echo %~s1

私は自分にshortNamePath.cmd電話して、次のように呼びました。

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

編集:パラメータが指定されていない場合、現在のディレクトリを使用するバージョンは次のとおりです。

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

パラメータなしで呼び出されました:

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

1
将来使用するためのユーティリティを作成するための細心の方法。私はこの解決策に十分に感謝できませんでした。そのようなコマンドをいつでも楽に呼び出すことは幸運です。
Izzy Helianthus 2017年

この巧妙なソリューションに別のnoobが出くわした場合:スクリプトは、最初のパラメーターが空かどうかを確認します。その場合、スクリプトは自動的に実行されますが、今回は最初の引数として現在のディレクトリを使用しています(%0バッチスクリプトのパス名です)。
シンジャイ2018年

11

プログラマーとして、この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();
        }

    }
}

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

1
これは、コマンドラインから作業したい人にとってはやりすぎです。しかし、私はC#プログラムが好きです。
Eniola

APIのMSDNページ:GetShortPathName
Amro

7

cmd.exe次を実行して実行します。

> cd "long path name"
> command

その後、command.comが表示され、短いパスのみが表示されます。

ソース


18
Windows 7には、少なくともx64バージョンではcommand.comがありません。
Timbo

2
上記はWin7 32ビットで動作します-私はそれをやっただけです。しかし、そうです、64ビットでは動作しません(これもテスト済みです)。
cssyphus

2
Windows 8 64ビットではいずれも
Dasun

5

キンボの答えは通常のファイルに最適です。

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

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

通常のファイル

この場合

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

期待したとおりです

d:\personal\PH124E~1\2013-P~3\2013-1~1.JPG

ハードリンクファイル

この場合

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

私は通常のMsDosパスを持っていますが、通常のファイル名を持っています。

d:\personal\PH124E~1\2013-P~3\2013-12-22--12-10-52------Bulevardul-Petrochimiștilor.jpg`

1

この回答に似ていますが、サブルーチンを使用します

@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

1

より直接的な答えは、バグを修正することです。

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

それ以外の場合、RUNNERは引用符で"%RUNNER%" -Xmx128m ... 終わり、コマンド は二重引用符で終わります。その結果、プログラムとファイルは別々のパラメーターとして扱われます。



0

バッチファイルを使用する場合:

set SHORT_DIR=%~dsp0%

echoコマンドを使用して、次のことを確認できます。

echo %SHORT_DIR%

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.