简体   繁体   English

获取特定子文件夹中的文件列表

[英]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.

相关问题 如何获取 cmd 中文件夹和子文件夹中存在的文件列表(完整路径) - How to get list (full path) of files exist in folder and subfolder in cmd 如何将子文件夹中的文件重命名为特定格式 - How to rename files in subfolder into a specific format 将特定子文件夹中的文件移动到另一个子文件夹 - Move files in specific subfolders to another subfolder 如何将特定文件夹的每个子文件夹中的所有 *.pdf 文件移动到每月创建的子文件夹? - How to move all *.pdf files in each subfolder of a specific folder to a monthly created subfolder? TortoiseSVN:如何获取更新特定目录中文件的所有修订的列表 - TortoiseSVN: How to get a list of all revisions that update files in specific directories 将文件移动到目标目录的子文件夹中 - Move files into a subfolder of a destination directory .bat文件以获取“服务目录路径”文件夹,并将文件从该文件夹复制到同一目录的子文件夹 - .bat file to get Service Directory Path folder and copy the files from that folder to subfolder of the same directory 文件夹及其子文件夹的权限列表 - List of permissions for a folder and it's subfolder 尝试列出没有特定目录的文件/文件夹 - Trying to list files/folders WITHOUT a specific directory CMake 获取要签名的文件列表 - CMake get list of files to be signed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM