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