在DOS窗口中,如何获取我所在目录的完整DOS名称/简称?
例如,如果我在目录C:\Program Files\Java\jdk1.6.0_22
中,我想显示它的简称C:\PROGRA~1\Java\JDK16~1.0_2
。
我知道运行dir /x
将为我提供当前目录中文件/目录的简短名称,但我无法找到以短名称格式显示当前目录的完整路径的方法。我必须按照目录从根目录开始,逐个目的地运行dir /x
。
我确定有更简单的方法吗?
答案 0 :(得分:149)
for %I in (.) do echo %~sI
任何更简单的方式?
答案 1 :(得分:36)
您还可以在CMD窗口中输入以下内容:
dir <ParentDirectory> /X
其中<ParentDirectory>
替换为包含您想要名称的项目的目录的完整路径。
虽然输出不是简单的Timbo's answer,但它会列出指定目录中的所有项目以及实际名称和(如果不同)短名称。
如果您使用for %I in (.) do echo %~sI
,可以将.
替换为文件/文件夹的完整路径,以获取该文件/文件夹的简称(否则当前文件夹的短名称为返回)。
在Windows 7 x64上测试。
答案 2 :(得分:28)
在Windows批处理脚本中,%~s1
expands path parameters to short names。创建此批处理文件:
@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
答案 3 :(得分:10)
作为一名程序员制作了这个10分钟的Winform项目。它对我有用。将此应用程序设置为文件资源管理器的上下文菜单可以节省更多点击次数。
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;
}
}
答案 4 :(得分:7)
答案 5 :(得分:5)
Kimbo的答案非常适合普通文件。
for %I in (.) do echo %~sI
使用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`
答案 6 :(得分:1)
$fso = New-Object -com scripting.filesystemobject
$fso.GetFolder('c:\Program Files (x86)').ShortName()
PROGRA~2
答案 7 :(得分:1)
如果通过批处理文件使用:
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<title>Title</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
html,
body {
height: 100%;
}
* {
margin: 0;
padding: 0;
text-decoration: none;
font-family: 'Roboto', sans-serif;
box-sizing: border-box
}
body {
background-color: #f9fbff
}
.source-image {
width: 100%;
background-repeat: no-repeat;
background-size: auto;
position: fixed;
max-width: 100%;
display: block
}
#login-form {
width: 500px;
background: 0 0;
padding: 80px 40px;
position: absolute;
left: 50%;
top: 40%;
transform: translate(-50%, -50%);
margin: 20px 0
}
.input_img {
position: absolute;
bottom: 20px;
left: 13px;
height: 30px
}
#login-form h1 {
text-align: center;
margin-bottom: 60px;
color: #191c3c;
font-size: 30px
}
.logoHolder {
display: flex;
place-content: space-around space-between;
}
.logoHolder>img {
margin-bottom: 12px;
margin-left: auto;
margin-right: auto;
display: block
}
#login-form p {
font-size: 16px;
color: #333
}
#login-form p a {
color: #191c3c;
text-decoration: underline;
font-weight: 700;
}
#login-form p {
font-size: 16px;
color: #333
}
#login-form .entryText {
font-size: 15px;
text-align: center;
}
#login-form p a:focus {
border: 1px solid #515772
}
#login-form label {
color: #4a4d67;
font-weight: 700;
margin-bottom: 10px !important
}
.input-box {
position: relative;
margin: 30px 0
}
.input-box input {
font-size: 15px;
color: #333;
border: 1px solid #51577245;
width: 100%;
line-height: 1.2;
font-size: 18px;
outline: 0;
background: #fff;
padding: 0 5px;
height: 72px;
border-radius: 5px;
padding-left: 57px;
box-shadow: 0 3px 11px 0 rgba(81, 87, 114, .2);
margin-top: 15px
}
.input-box input:focus {
border: 1px solid #515772
}
.input-box span::before {
content: attr(data-placeholder);
position: absolute;
top: 50%;
left: 5px;
color: #515772;
transform: translateY(-50%);
z-index: -1;
transition: .5s;
padding-left: 20px
}
.validation {
border-color: #c00000 !important;
margin-bottom: 7px;
}
.feedback {
color: #c00000;
}
::placeholder {
margin-left: 60px;
color: #8b8d9d;
font-size: 14px;
font-weight: 700;
}
.cursor {
width: 17px
}
.input-box span::after {
content: '';
position: absolute;
width: 0;
height: 2px;
background: linear-gradient(120deg, #2196F3, #FF5722);
transition: .5s
}
.focus+span::before {
top: -5px
}
.focus+span::after {
width: 100%
}
.login-btn {
display: block;
width: 100%;
height: 62px;
border: none;
background: #515772;
background-size: 200%;
color: #fff;
outline: 0;
cursor: pointer;
margin: 20px 0 0;
border-radius: 8px;
transition: .5s;
font-size: 16px;
letter-spacing: 1px;
font-weight: 700;
padding-left: 5px;
padding-right: 5px;
}
.login-btn:focus {
border: 2px solid #000
}
.login-btn:hover {
background-color: #2d3142
}
.bottom-links {
margin-top: 30px;
text-align: center;
font-size: 13px
}
.versionText {
background: transparent;
position: fixed;
inline-size: -webkit-fill-available;
bottom: 10px;
z-index: 10;
text-align: right;
font-size: 13px;
margin-right: 20px;
}
.bottom-links>p>a {
text-decoration: underline;
}
.bottom-links>p {
margin-bottom: 30px;
}
@media (max-width:768px) {
#login-form {
top: 45%;
padding: 0;
}
.source-image {
display: none
}
.versionText {
right: 0% !important;
align-items: center;
text-align: center;
}
}
@media (max-width:576px) {
#login-form {
width: 90%;
top: 50%;
padding: 0;
}
.source-image {
display: none
}
.versionText {
right: 0% !important;
align-items: center;
text-align: center;
}
}
@media (max-width:320px) {
#login-form {
width: 90%;
top: 50%;
padding: 0;
}
.source-image {
display: none
}
.versionText {
right: 0% !important;
align-items: center;
text-align: center;
}
}
@media (max-width:1440px) and (min-width:1024px) {
#login-form {
top: 45%;
padding: 0;
}
.versionText {
right: 0% !important;
align-items: center;
text-align: center;
}
}
@media (max-width:1440px) {
.source-image {
display: none
}
}
.version {
font-size: 13px;
margin-top: 10px;
}
.wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
}
.keyboardHolder {
display: flex
}
.key {
width: 20%;
margin-left: 10px;
margin-top: 15px;
}
.buttonHolder {
display: flex;
place-content: space-around space-between;
}
.buttonHolder>button:nth-child(1) {
margin-right: 10px;
background-color: #a8b8c9;
color: #262733;
}
</style>
</head>
<body>
<div class="wrapper">
<main>
<form action="" id="login-form">
<div class="logoHolder">
<img src="https://logo.clearbit.com/imgur.com">
<img src="https://logo.clearbit.com/airbnb.com">
</div>
<p class="entryText">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore</p>
<div class="input-box keyboardHolder">
<label for="username">USERNAME </label>
<input id="username" name="username">
<img class="key" src="https://image.flaticon.com/icons/svg/917/917059.svg">
</div>
<div class="input-box keyboardHolder">
<label for="password">PASSWORD </label>
<input id="password" name="password" type="password">
<img class="key" src="https://image.flaticon.com/icons/svg/917/917059.svg">
</div>
<div class="buttonHolder">
<button type="submit" class="login-btn">Return</button>
<button type="submit" class="login-btn">Login</button>
</div>
<div class="bottom-links">
<small class="version">v3.0</small>
</div>
</form>
</div>
</main>
</body>
</html>
您可以使用echo命令进行检查:
set SHORT_DIR=%~dsp0%
答案 8 :(得分:0)
与此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
答案 9 :(得分:0)
将此脚本放在 windows 路径中的某个位置。我调用了我的 getshort.bat
并将其放在 System32 文件夹中。
要使用它,您必须在调用 cmd.exe 窗口中的脚本后传递一个路径参数。
因此打开 cmd.exe 并键入类似 getshort.bat "C:\folder\file name with spaces.ext"
的内容,您必须用空格双引号路径,否则不需要。
脚本将采用您提供的路径并将短名称存储在一个具有两个版本的临时文本文件中,版本 1 在短路径周围有引号,而另一个版本没有。
我使用 notepad++ 打开 txt 文件,因此如果您不使用该程序,则需要更改行 START "" /MAX NOTEPAD++ "%TMP%\Test.txt"
并将 notepad++
替换为您的编辑器名称。
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
COLOR 0A
:----------------------------------------------------------------------------------
REM SET THE CD VARIABLE EQUAL TO THE FIRST PARAMATER YOU PASSED THE SCRIPT
:: ..WHICH WAS A FILE PATH OF YOUR CHOOSING...
CD=%1
:----------------------------------------------------------------------------------
:: DELETE ANY LEFTOVERS FROM PRIOR RUNS
IF EXIST "%TMP%\Test.txt" DEL /F /Q "%TMP%\Test.txt"
:----------------------------------------------------------------------------------
:: USE THE FOR COMMAND TO CALL A SUBROUTINE TO STORE THE SHORT NAMES WITH AND WITHOUT QUOTES
FOR %%1 IN ("%CD%") DO (
SET ARG1="%%~s1"
SET ARG2=%%~s1
CALL :CREATE_TXTFILE ARG1 ARG2
START "" /MAX NOTEPAD++ "%TMP%\Test.txt"
GOTO :EOF
)
:----------------------------------------------------------------------------------
REM USE THIS SUB-ROUTINE TO STORE THE SHORTNAMES INSIDE THE A TXT FILE
:CREATE_TXTFILE
(
ECHO %ARG1%
ECHO %ARG2%
)>"%TMP%\Test.txt"
:: THE NEXT LINE WILL RETURN THE SCRIPT TO THE LINE BELOW THE CALL COMMAND ABOVE AND CONTINUE EXECUUTION AS NORMAL
:: NOTEPAD++ WILL ATTEMPT TO FIND AND OPEN THE NEWLY CREATED TXT FILE WITH THE SHORTNAMES INSIDE
:: CHANGE TO WHATEVER TEXT EDITOR YOU HAVE TO OPEN TXT FILES IF YOU DONT HAVE NOTEPAD++
EXIT /B
答案 10 :(得分:-1)
使用此链接,它将自动将您提供的任何路径转换为任何格式 https://pathconverter-pp.azurewebsites.net