简体   繁体   English

如何使用Powershell获取文件夹中所有文件夹名称而不是子文件夹中文件/子文件夹的.txt文件?

[英]How to get a .txt file of all folders names inside a folder but not the files/subfolders inside the child folders using powershell?

Currently I'm using, SHIFT + Right Click > Open powershell window here > then paste in 当前正在使用SHIFT +右键单击>在此处打开powershell窗口>然后粘贴

dir -recurse | select -ExpandProperty Name | foreach {$_.split('.',2)[0]} | out-file file.txt

Only problem is I choose a directory to SHIFT + Right click, but I get all the names of the files/folders inside the second folder too and it really ruins the organization I'm going for. 唯一的问题是我选择了SHIFT +右键单击目录,但是我也获得了第二个文件夹内所有文件/文件夹的名称,这确实破坏了我要寻找的组织。

So for example I have a folder called "RootFolder". 因此,例如,我有一个名为“ RootFolder”的文件夹。 Inside RootFolder is 10 other folders called "Folder1" through "Folder10". RootFolder内还有另外10个文件夹,分别称为“ Folder1”至“ Folder10”。 I only want the names Folder1 - Folder10 to be inside a .txt folder the shell command creates. 我只希望名称Folder1-Folder10在shell命令创建的.txt文件夹中。 I do not want Subfolders/files inside folders Folder1-10 in the .txt file. 我不希望.txt文件中的Folder1-10文件夹内的子文件夹/文件。

If you want to only list directories you need to tell it. 如果只想列出目录,则需要告诉它。

Dir -R | ? { $_.PSIsContainer }

or 要么

Dir -R | ? { $_ -is [System.IO.DirectoryInfo] }

In more recent PowerShell versions you can do it directly. 在最新的PowerShell版本中,您可以直接进行操作。

Dir -Dir

Dir = Get-ChildItem Dir = Get-ChildItem
-R = -Recurse -R =-递归
? = Where-Object =对象
-Dir = -Directory -Dir =-目录

If you only want directories within the named directory, you don't want to recurse. 如果只需要命名目录中的目录,则不需要递归。 That will travel the entire directory hierarchy; 这将遍历整个目录层次结构; just use the -Directory switch to filter the output to only include directories. 只需使用-Directory开关来过滤输出,使其仅包含目录。

Then, you can just extract the Name property from the DirectoryInfo objects. 然后,您可以从DirectoryInfo对象中提取Name属性。

PS C:\> dir C:\Windows -Directory |% { $_.Name }
addins
appcompat
apppatch
AppReadiness
[...]

and output as desired. 并根据需要输出。 Even more concisely: 更简而言之:

(dir C:\Windows -Directory).Name

暂无
暂无

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

相关问题 如何获取包含子文件夹的文件夹中文件的文件路径? - How to get file paths of files inside a folder including subfolders? 批处理文件以列出文件夹内的子文件夹 - Batch file to list subfolders inside a folder Powershell按升序获取过去4个月内修改的所有文件夹和文件的列表 - Powershell get list of all folders and files modified in the last 4 months in ascending order Powershell 以表格形式获取子文件夹列表和每个文件的数量 - Powershell get list of sub folders and number of files in each in a table format 将文件列表中的第一个文件放入文件夹列表的第一个文件夹中 - Getting first file in a list of files into the first folder of a list of folders 批处理:如何制作包含没有扩展名的文件夹中的所有文件的.txt文件 - Batch: How to make a .txt file with all files in a folder without extensions Python:识别文件夹结构中的数字名称文件夹 - Python: Identifying numerically names folders in a folder structure Joomla componente / module列出文件夹中的文件并按子文件夹分组 - Joomla componente/module to list files on folders and group them by subfolders powershell 生成包含所有其他文件夹名称的列表 - powershell Generate a list with all others folders name 批处理:创建文件夹列表,然后将多个文件/文件夹复制到所有文件夹中 - Batch: Create a list of folders and then copy multiple files/folders into all of them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM