【问题标题】:How to remove current path from dir command in PowerShell?如何从 PowerShell 中的 dir 命令中删除当前路径?
【发布时间】:2022-12-13 12:59:48
【问题描述】:

当我在Powershell中输入dir命令时,它总是显示当前路径+几个空行,这真是没必要。我想设置一个从结果中删除当前路径的别名。但是我可以使用什么命令/属性来删除该路径?我在互联网上或 dir 的手册页上找不到任何内容。

我使用 Powershell 7.2.1 并且是 PS 的新手。

【问题讨论】:

  • dir 只是一个别名。你搜索的是哪个页面?您可以将整个输出字符串化,或者选择相同的属性输出(最佳解决方案)。 gci | select mode,lastwritetime,length,name

标签: windows powershell directory ls


【解决方案1】:

首先,我认为提及 7.2.1 会吓跑人们,使其不敢尝试回答您的问题。许多人,比如我自己,目前都在使用 5.x。如果我知道如何将 7.x 安装到 WinPE 上,我可能会进行转换。我对此可能是错的,但这似乎是一个问题。

其次,Dir,在PowerShell中,是Get-ChildItem的别名。 看到这个List of Compatibility Aliases

第三,您需要查看Working with Files and FoldersGet-ChildItemGet-Item

第四,PowerShell 中的一切都是对象。因此,您在 Dir 中看到的所有额外行实际上并不是由 Dir 创建的,它们是格式化绒毛,PowerShell 粘在那里以试图使其可读。 PowerShell 获取 Dir/Get-ChildItem 返回的对象,并试图让它们对您来说更漂亮,但是当您直接使用这些对象时,所有这些多余的东西都不存在。当您开始使用 Pipeline 时,请记住这一点,它只是一次将一组对象送入管道。

第五,PowerShell 5.x 和更新版本的所有版本都有相当多的重叠,所以理论上,如果我小心的话,我给你的 5.x 代码应该在 7.x 中工作。如果我犯了错误,我想很抱歉——我试过了!

在这段代码中:

  1. 轮流注释掉和取消注释顶部附近的“Objects =”行。
  2. 记下注释掉的“$_ | Format-List -Property *”。如果您取消注释,它将生成一个长输出,其中包含被送入管道的对象中的所有属性。您可以使用它来查看我主要是如何访问这些对象来设置变量的。
  3. 注意代码中 SubString 的使用。我很难证明 SubString 在 PowerShell 核心中可用,但如果是的话,它是一种可用于将路径分成几部分的工具。另一个工具是Split-Path,所以你可能想看看它。
  4. 如果您有所需单个文件的确切名称,则在下面的代码中将 *.ps1 替换为该确切名称。或者在许多命令中,将 FileName.ext 添加到路径末尾都可以。
  5. 此代码在 Windows 中运行良好,但在其他操作系统中您可能需要进行调整。
    #   Uncomment only one of the following lines at a time:
    #$Objects = Get-ChildItem -Path $Home -File                 #   Gets files in home path
    #$Objects = Get-ChildItem -Path $Home -Directory           #   Gets Directories in home path
    #$Objects = Get-ChildItem -Path $PSScriptRoot -File        #   Gets files in same folder as the script
    #$Objects = Get-ChildItem -Path $PSScriptRoot -Directory   #   Gets Directories in same folder as the script
    $Objects = Get-ChildItem -Path "$HomeDocuments" -File
    #$Objects = Get-ChildItem -Path "$PSScriptRoot*.ps1" -File
    #$Objects = Get-Item -Path "$PSScriptRoot*.ps1"
    
    $Objects  | ForEach-Object {    #   Get files
        #$_ | Format-List -Property *
    
        $f =$_.FullName                     #   Get full path name
        $d = "$($_.PSDrive):"     #   Get the drive
        $dp = $_.DirectoryName              #   Get drive and path
        $p = $dp.SubString($d.Length)       #   Get path only
        $n = $_.BaseName                    #   Get file name only
        $x = $_.Extension                   #   Get file extension only
        $nx = $_.Name                       #   Get files name and extension
        
        Write-Host
        Write-Host "f: $f"
        Write-Host "dp: $dp, nx: $nx"
        Write-Host "d: $d, p: $p, n: $n, x: $x"
    }
    
    

【讨论】:

  • MySurmise,不客气!我希望你能成功进入 PowerShell,我认为它是目前最有用的语言之一。我一直在学习关于它的事情,我不知道它能做什么。
【解决方案2】:

您看到的是 Get-ChildItem 的格式定义,特别是 GroupBy tag

<GroupBy>
  <PropertyName>PSParentPath</PropertyName>
</GroupBy>

您可以通过使用 Get-FormatData 查询目标 TypeNames 然后使用 Export-FormatData 将其存储在文件中将其导出到文件来查看它的格式定义:

Get-FormatData -TypeName System.IO.FileInfo, System.IO.DirectoryInfo |
    Export-FormatData -Path path	omyCustomGetChildItemView.ps1xml

然后,您可以随意更新此 ps1xml 文件,并为这些类型设计您自己的自定义视图,在这种情况下,我们的目标是 System.IO.FileInfoSystem.IO.Directory。对于这个答案,你可以试试这个ps1xml,它应该可以去除那些空行并显示父路径。

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <ViewDefinitions>
    <View>
      <Name>customgciview</Name>
      <ViewSelectedBy>
        <TypeName>System.IO.DirectoryInfo</TypeName>
        <TypeName>System.IO.FileInfo</TypeName>
      </ViewSelectedBy>
      <TableControl>
        <TableHeaders>
          <TableColumnHeader>
            <Label>Mode</Label>
            <Width>7</Width>
            <Alignment>Left</Alignment>
          </TableColumnHeader>
          <TableColumnHeader>
            <Label>LastWriteTime</Label>
            <Width>26</Width>
            <Alignment>Right</Alignment>
          </TableColumnHeader>
          <TableColumnHeader>
            <Label>Length</Label>
            <Width>14</Width>
            <Alignment>Right</Alignment>
          </TableColumnHeader>
          <TableColumnHeader>
            <Label>Name</Label>
            <Alignment>Left</Alignment>
          </TableColumnHeader>
        </TableHeaders>
        <TableRowEntries>
          <TableRowEntry>
            <Wrap />
            <TableColumnItems>
              <TableColumnItem>
                <PropertyName>Mode</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>LastWriteTimeString</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>LengthString</PropertyName>
              </TableColumnItem>
              <TableColumnItem>
                <PropertyName>NameString</PropertyName>
              </TableColumnItem>
            </TableColumnItems>
          </TableRowEntry>
        </TableRowEntries>
      </TableControl>
    </View>
  </ViewDefinitions>
</Configuration>

将其存储在文件中后,您可以使用 Update-FormatData 更新这些对象在您的控制台中的外观:

Update-FormatData -PrependPath path	omyCustomGetChildItemView.ps1xml

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多