[英]get files list in a specific subfolder
I am trying with no success to get a list of files in a .csv (and relative properties) recursevely just in one subfolders. 我试图以递归方式仅在一个子文件夹中获取.csv(和相对属性)中的文件列表,但没有成功。 This is my situation:
这是我的情况:
Root
|
| - Folder 1
| | - xxxxxx
| | - yyyyyyy
| | - INTERESTED_FOLDER (that contains files requested)
|
| - ...
|
| - Folder n:
| - xxxxxx
| - yyyyyy
| - INTERESTED_FOLDER (that contains files requested)
Examples I found on web talk about getting all files names in ALL subdirectories, when I'd need to search just in one but iterate for all root folders 1-n. 当我只需要在一个子目录中搜索但要遍历所有根文件夹1-n时,我在网上发现的示例都涉及在所有子目录中获取所有文件名。 Thanks for your help.
谢谢你的帮助。
You can use the Get-ChildItem cmdlet to first retrieve all Folders 1-n
, than you call it again and filter the INTERESTED_FOLDER
and finally call it another time to retrieve all items within that and export the csv: 您可以使用Get-ChildItem cmdlet首先检索所有Folder
1-n
,然后再次调用它并过滤INTERESTED_FOLDER
,最后再次调用它以检索其中的所有项目并导出csv:
Get-ChildItem | ? { $_.PSIsContainer } |
Get-ChildItem -Filter 'INTERESTED_FOLDER' | ? { $_.PSIsContainer } |
Get-ChildItem -Recurse |
Export-Csv -Path c:\content.csv
Grab all the INTERESTED_FOLDERs
in one go using the -Include
parameter, then get the contents of all of them. 使用
-Include
参数一次性抓住所有INTERESTED_FOLDERs
,然后获取所有内容。
If you use Where-Object then PowerShell retrieves all items then filters them, which takes longer and may be significant if you have a lot of items (you can assess the difference by including your code block in a Measure-Object
statement). 如果使用Where-Object,则PowerShell将检索所有项目,然后对其进行过滤,这将花费更长的时间,并且如果您有很多项目,则可能很重要(可以通过在
Measure-Object
语句中包含代码块来评估差异)。 Always try and use the native provider's filtering capabilities as this will reduce execution time. 始终尝试使用本机提供程序的筛选功能,因为这将减少执行时间。 If you are using PowerShell v3 or above there are additional arguments for files or folders available in Get-ChildItem:
如果您使用的是PowerShell v3或更高版本,则Get-ChildItem中提供了文件或文件夹的其他参数:
Get-ChildItem -Path $Path -Recurse -Directory -Include 'INTERESTED_FOLDER','INTERESTED_FOLDER1' |
Foreach-Object {Get-ChildItem $_ -File} |
Export-Csv -Path $OutPutFile -NoTypeInformation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.