Get-ChildItem的DIR /X等效命令

6

如何在PowerShell中以8.3表示法显示目录列表?


我的8.3名称需求是因为我想使用DEBUG.exe在字节级别上调查文件。(当然,我可以在CMD shell中完成。)不幸的是,我后来发现64位Windows不再有DEBUG.exe了。所以我现在需要开始一个新问题,询问什么是DEBUG.exe的最佳替代品。 - Old Geezer
2
这就是为什么讲述你想要做什么的原因很重要,而不仅仅是做什么。一个可能性:Windows IT Pro: Get Hex Dumps of Files in PowerShell - Bill_Stewart
@Bill_Stewart:太好了!GC-编码字节很有用。 - Old Geezer
5个回答

8
您可以使用WMI:
Get-ChildItem | ForEach-Object{
    $class  = if($_.PSIsContainer) {"Win32_Directory"} else {"CIM_DataFile"}
    Get-WMIObject $class -Filter "Name = '$($_.FullName -replace '\\','\\')'" | Select-Object -ExpandProperty EightDotThreeFileName
}

或者使用Scripting.FileSystemObject组件对象:
$fso = New-Object -ComObject Scripting.FileSystemObject

Get-ChildItem | ForEach-Object{

    if($_.PSIsContainer) 
    {
        $fso.GetFolder($_.FullName).ShortPath
    }
    else 
    {
        $fso.GetFile($_.FullName).ShortPath
    }    
}

1
干得好!Scripting.FileSystemObject方法更快。至于WMI/CIM方法:考虑到'是文件名中的合法字符,最好在过滤器内使用\"...`",如下所示(还将Get-WmiObject替换为Get-CimInstance(PSv3+)):Get-ChildItem | ForEach-Object { $class = if ($.PSIsContainer) { "Win32_Directory" } else { "CIM_DataFile" } Get-CimInstance $class -Filter "Name = `"$($.FullName -replace '\','\')`"" | Select-Object -ExpandProperty EightDotThreeFileName }` - mklement0

3
如果您安装了PSCX模块,则可以使用Get-ShortPath cmdlet进行以下操作:
dir | Get-ShortPath

或者

 dir | Get-ShortPath  | select -expa shortpath

1
作为管理员:choco install pscx -y - Cameron Taggart
我使用 (Get-ShortPath "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10586.0\um\x86").ShortPath 这样的方式来获取 C:\PROGRA~2\WI3CF2~1\10\Lib\100105~1.0\um\x86 - Cameron Taggart

0

在jpblanc的答案基础上,这里提供一种方法,可以通过调用Win32 GetShortPathName() API来缩短整个路径:

function Get-ShortPathName
{
    Param([string] $path)

    $MethodDefinition = @'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW", SetLastError = true)]
public static extern int GetShortPathName(string pathName, System.Text.StringBuilder shortName, int cbShortName);
'@

    $Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru
    $shortPath = New-Object System.Text.StringBuilder(500)
    $retVal = $Kernel32::GetShortPathName($path, $shortPath, $shortPath.Capacity)
    return $shortPath.ToString()
}

除了之前提到的链接,我在编写这个函数时还参考了Dr. ScriptoPInvoke.net

0

你可以运行 cmd...

cmd /c dir /x

请注意,get-childitem -filter 也会匹配文件名的短版本!
get-childitem -filter *~1*

0

有趣。我对8.3名称的要求是因为我想使用DEBUG.exe在字节级别上调查文件。(当然,我可以在CMD shell中完成它。)不幸的是,我后来发现64位Windows不再拥有DEBUG.exe。所以我现在需要开始一个新问题,询问什么是DEBUG.exe的最佳替代品。 - Old Geezer
你使用DEBUG.EXE的目标是什么?请查看WinDbg(Windows调试工具)http://msdn.microsoft.com/en-us/library/windows/hardware/ff551063(v=vs.85).aspx - JPBlanc
自DOS 1.1以来,古老的DEBUG.com是一个小巧的、一直存在(直到现在)的实用程序,除了反汇编英特尔操作码外,还可以读取文件中的原始字节并加载磁盘扇区。不,我并不想使用它来调试任何代码。无论如何,谢谢。 - Old Geezer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接