Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Finding the DOS "Short path"  (Read 7396 times)

0 Members and 1 Guest are viewing this topic.

iONik

    Topic Starter


    Beginner

    Finding the DOS "Short path"
    « on: March 10, 2021, 04:56:14 PM »
    I was originally looking to get a simple batch script to display the DOS short-path via right-click context menu and perhaps I still am.
    But I found some code via a search that looked promising, but it would need to be compiled.....I think to make an actual .exe file.

    Perhaps someone can compile this or tell me how to use it??!


    Form1.cs
    Code: [Select]
    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.Designers.cs
    Code: [Select]
    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;
        }
    }

    Hackoo



      Hopeful
    • Thanked: 42
    • Experience: Expert
    • OS: Windows 10
    Re: Finding the DOS "Short path"
    « Reply #1 on: March 11, 2021, 02:57:57 PM »
    Hi  ;)
    Perhpas this can help you : How can I find the short path of a Windows directory/file?

    Code: [Select]
    @echo off
    @for %%i in (%*) do echo %%~si
    pause

    Save it as shortpath.bat , and then drag files on it