简体   繁体   English

使用PowerShell Get-ChildItem获取目录中的所有文件

[英]Getting all files in a directory with PowerShell Get-ChildItem

I have been using the following command to get the MD5 hashes for all files in a directory (and all its subdirectories): 我一直在使用以下命令来获取目录(及其所有子目录)中所有文件的MD5哈希值:

Get-FileHash -Algorithm MD5 -LiteralPath (Get-ChildItem "*.*" -Recurse)

However, I realised that a few of the subdirectories have files with no file extension. 但是,我意识到一些子目录的文件没有文件扩展名。

What is the difference between the following two commands and is either a good way to get all files in a directory (including files without a file extension)? 以下两个命令之间的区别是什么,一种获取目录中所有文件(包括不带文件扩展名的文件)的好方法? Their outputs appear to be the same for my test directory yet only the first one works as an input for the Get-FileHash cmdlet. 对于我的测试目录,它们的输出似乎相同,但是只有第一个作为Get-FileHash cmdlet的输入。

Get-ChildItem "*" -Recurse

Get-ChildItem -Recurse | where {!$_.PsIsContainer}

Edit: Thank you Mathias, these both appear to work with Get-FileHash (including files with no file extension and also files with square brackets in the filename): 编辑:谢谢Mathias,它们似乎都可以与Get-FileHash (包括没有文件扩展名的文件以及文件名中带有方括号的文件):

Get-FileHash -Algorithm MD5 -LiteralPath (Get-ChildItem "*" -Recurse)

Get-ChildItem -Recurse | where {!$_.PsIsContainer} | Get-FileHash -Algorithm MD5

It helps to post the error messages. 它有助于发布错误消息。 This would work, if you really wanted to do it this way. 如果您真的想用这种方法,那就可以用。 This is part of an annoying problem with PS 5, where the string version of what get-childitem returns isn't the full path. 这是PS 5令人讨厌的问题的一部分,其中get-childitem返回的字符串版本不是完整路径。 Strange that *.* returns the full path. 奇怪的是*。*返回完整路径。 One workaround is to pipe it to get-item after that. 一种解决方法是在此之后将其通过管道传递到get-item。

get-filehash -Algorithm MD5 -LiteralPath (Get-ChildItem -Recurse | get-item | 
where {!$_.PsIsContainer})

Another way. 其他方式。 Get-ChildItem has a -File option now. Get-ChildItem现在具有-File选项。 And get the fullname. 并获取全名。

get-filehash -Algorithm MD5 -LiteralPath (Get-ChildItem -Recurse -File).fullname

Simple demo of the problem: 问题的简单演示:

get-childitem * -recurse | foreach-object { "$_" }

foo2
hi.doc
hi2.doc


get-childitem *.* -recurse | foreach-object { "$_" }

C:\Users\js\foo\foo2\hi2.doc
C:\Users\js\foo\hi.doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM