【问题标题】:Get full path of the files in PowerShell获取 PowerShell 中文件的完整路径
【发布时间】:2012-10-19 00:51:40
【问题描述】:

我需要获取所有文件,包括属于特定类型的子文件夹中的文件。

我正在做这样的事情,使用Get-ChildItem:

Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"}

但是,它只返回文件名而不是整个路径。

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    | select FullName 添加到上述行的末尾。如果之后您需要实际执行此操作,则可能必须将其通过管道传输到 foreach 循环中,如下所示:

    get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % {
         Write-Host $_.FullName
    }
    

    【讨论】:

      【解决方案2】:

      这应该比使用后期过滤要快得多:

      Get-ChildItem C:\WINDOWS\System32 -Filter *.txt -Recurse | % { $_.FullName }
      

      【讨论】:

      • 这是真的。一个警告:这个命令实际上会获取像*.txt* 这样的文件(-Filter 使用 CMD 通配符)。如果这不是您想要的,请使用-Include *.txt
      • ls | % { $_.FullName } 获取文件列表中的完整路径;假设别名 ls=Get-ChildItem
      【解决方案3】:

      这是一个较短的:

      (Get-ChildItem C:\MYDIRECTORY -Recurse).fullname > filename.txt
      

      【讨论】:

      • 这是一个较短的:(gci -r c:\).fullname
      • 这是一个较短的:(ls -r c:\).fullname
      • 这是一个较短的:(ls -r c:).fullname
      【解决方案4】:

      你也可以像这样使用Select-Object

      Get-ChildItem "C:\WINDOWS\System32" *.txt -Recurse | Select-Object FullName
      

      【讨论】:

      • 请注意Select-Object 返回PSCustomObject,而不是字符串。如果您将结果用作另一个程序的参数,它可能不起作用
      • 对@Chintsu 说的是肯定的。要获取字符串而不是 PSCustomObject,请使用 -ExpandProperty FullName 而不是简单的 FullName。据我了解,-ExpandProperty 参数导致 cmdlet 将结果作为指定属性的(本机?)类型而不是某个自定义对象返回。
      • 我也需要时间(我弄乱了一个程序的源代码,后来我想我可能应该开始对它进行源代码控制,但我可以了解我第一次提交“master " 通过获取安装后的文件:Get-ChildItem -Path 'C:\Program Files\TheProgram' -Recurse | Where-Object -FilterScript {($_.LastWriteTime -gt '2020-03-01')} | Select-Object FullName
      【解决方案5】:

      试试这个:

      Get-ChildItem C:\windows\System32 -Include *.txt -Recurse | select -ExpandProperty FullName
      

      【讨论】:

      • 请添加一些信息,说明您的尝试有什么不同以及为什么另一个不起作用。
      【解决方案6】:
      gci "C:\WINDOWS\System32" -r -include .txt | select fullname
      

      【讨论】:

      • 能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?
      【解决方案7】:

      [替代语法]

      对于某些人来说,定向管道操作员不是他们的口味,但他们更喜欢链接。在 roslyn 问题跟踪器中查看有关此主题的一些有趣意见:dotnet/roslyn#5445

      根据案例和上下文,这种方法之一可以被认为是隐含的(或间接的)。例如,在这种情况下,针对可枚举使用管道需要特殊令牌 $_(又名 PowerShell's "THIS" token)可能会让某些人感到反感。

      对于这样的人,这里有一个更简洁、直接的方法,使用 点链接

      (gci . -re -fi *.txt).FullName
      

      ( 请注意,PowerShell 的命令参数解析器接受部分参数名称。所以除了-recursive;-recursiv-recursi-recurs-recur-recu-rec-re 被接受,但不幸的是 -r .. 这是唯一对单个 - 字符有意义的正确选择(如果我们遵循 POSIXy UNIXy 约定)!) em>

      【讨论】:

      • 请注意:-r 的缩写形式 -Recurse 对我来说很好。
      • @Polymorphix,你说得对,它在 Windows 10 上为我工作(不确定我之前尝试的是哪个版本)。但是,示例中的-File 开关-fi 也是如此:-fil-file 有效,但-f 无效。按照 POSIX 风格,它应该是 --file(多字母)和 -f(单字母,除非 -f 是为其他东西保留的,比如 force 开关,那么它可以是像 -l 这样的其他东西或根本没有单字母选项)。
      【解决方案8】:

      这对我有用,并生成了一个名称列表:

      $Thisfile=(get-childitem -path 10* -include '*.JPG' -recurse).fullname
      

      我通过使用get-member -membertype properties 找到了它,这是一个非常有用的命令。它为您提供的大多数选项都附加了.<thing>,例如fullname 就在这里。你可以坚持同样的命令;

        | get-member -membertype properties 
      

      在任何命令的末尾,以获取有关您可以使用它们执行的操作以及如何访问这些操作的更多信息:

      get-childitem -path 10* -include '*.JPG' -recurse | get-member -membertype properties
      

      【讨论】:

        【解决方案9】:
        Get-ChildItem -Recurse *.txt | Format-Table FullName
        

        这就是我用的。我觉得它更容易理解,因为它不包含任何循环语法。

        【讨论】:

          【解决方案10】:

          如果相对路径是您想要的,您可以使用 -Name 标志。

          Get-ChildItem "C:\windows\System32" -Recurse -Filter *.txt -Name

          【讨论】:

          • 这似乎没有给出相对路径,只有文件名。你能举个例子吗?
          • 如果文件在你开始的文件夹中,它只显示文件名。如果它在子文件夹中,它将给出subfolder/file.txt
          【解决方案11】:

          在 PS 5 中真的很烦人,其中 $_ 不会是 foreach 中的完整路径。这些是 FileInfo 和 DirectoryInfo 对象的字符串版本。由于某种原因,路径中的通配符可以修复它,或者使用 Powershell 6 或 7。您也可以通过管道连接到中间的 get-item。

          Get-ChildItem -path C:\WINDOWS\System32\*.txt -Recurse | foreach { "$_" }
          
          Get-ChildItem -path C:\WINDOWS\System32 -Recurse | get-item | foreach { "$_" }
          

          这似乎是 .Net 的一个问题,已在 .Net Core (Powershell 7) 中得到解决:Stringification behavior of FileInfo / Directory instances has changed since v6.0.2 #7132

          【讨论】:

            【解决方案12】:

            我正在使用以下脚本来提取所有文件夹路径:

            Get-ChildItem -path "C:\" -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName | Out-File "Folder_List.csv"
            

            没有完整的文件夹路径。 113个字符后,来了:

            Example - C:\ProgramData\Microsoft\Windows\DeviceMetadataCache\dmrccache\en-US\ec4d5fdd-aa12-400f-83e2-7b0ea6023eb7\Windows...
            

            【讨论】:

              【解决方案13】:

              我使用此行命令在“C:\Temp”中搜索“.xlm”文件,结果在文件“result.txt”中打印全名路径:

              (Get-ChildItem "C:\Temp" -Recurse | where {$_.extension -eq ".xml"} ).fullname > result.txt 
              

              在我的测试中,这种语法对我来说很好用。

              【讨论】:

                【解决方案14】:

                为什么还没有人使用 foreach 循环?这里的优点是您可以轻松命名变量:

                # Note that I'm pretty explicit here. This would work as well as the line after:
                # Get-ChildItem -Recurse C:\windows\System32\*.txt
                $fileList = Get-ChildItem -Recurse -Path C:\windows\System32 -Include *.txt
                foreach ($textfile in $fileList) {
                    # This includes the filename ;)
                    $filePath = $textfile.fullname
                    # You can replace the next line with whatever you want to.
                    Write-Output $filePath
                }
                

                【讨论】:

                • 如何在不使用 fileList 中的 foreach 文本文件的情况下获取文本文件的单个文件路径?像 textfile= $fileList[$i].fullname 之类的东西,我知道这不是最佳方法,但我需要这样做:(
                • 只需在textfile 前添加$$textfile = $fileList[$i].FullName。假设$i有一个数值,即。
                【解决方案15】:

                我遇到了一个问题,我有一个可执行文件,其中包含目录路径字符串作为参数和格式。像这样:

                "C:\temp\executable.exe" "C:\storage\filename" "C:\storage\dump" -jpg

                我需要在不同文件夹中的 .jpg 文件中执行此命令。

                $files = Get-ChildItem  -Path C:\storage\*.jpg -Recurse -Force | Select-Object -ExpandProperty FullName
                
                for ($i=0; $i -lt $files.Count; $i++) {
                    [string]$outfile = $files[$i]
                    
                    Start-Process -NoNewWindow -FilePath "C:\temp\executable.exe" -ArgumentList $outfile, "C:\storage\dump", "-dcm"
                   
                
                }
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-03-31
                  • 2015-06-15
                  • 2018-10-24
                  • 2015-11-03
                  相关资源
                  最近更新 更多