dir/b > files.txt
我想它必须在 PowerShell 中完成以保留 unicode 符号。
Get-ChildItem | Select-Object -ExpandProperty Name > files.txt
或更短:
ls | % Name > files.txt
但是,您可以轻松地在以下位置执行相同操作cmd
:
cmd /u /c "dir /b > files.txt"
该/u
开关告诉cmd
将重定向到文件中的内容作为 Unicode 写入。
Get-ChildItem
实际上已经有一个相当于的标志dir /b
:
Get-ChildItem -name
(或dir -name
)
简单的说:
dir -Name > files.txt
在 PSHdir
中(其中别名Get-ChildItem
)为您提供对象(如另一个答案中所述),因此您需要选择所需的属性。使用Select-Object
(alias select
) 创建具有原始对象属性子集的自定义对象(或可以添加其他属性)。
然而,在格式阶段这样做可能是最简单的
dir | ft Name -HideTableHeaders | Out-File files.txt
(ft
是format-table
。)
如果您想在files.txt
(out-file
默认使用 UTF-16)中使用不同的字符编码,请使用该-encoding
标志,您还可以附加:
dir | ft Name -HideTableHeaders | Out-File -append -encoding UTF8 files.txt
由于 powershell 处理对象,因此您需要指定要如何处理管道中的每个对象。
此命令将仅打印每个对象的名称:
dir | ForEach-Object { $_.name }
刚刚发现这篇很棒的帖子,但子目录也需要它:
DIR /B /S >somefile.txt
采用:
Get-ChildItem -Recurse | Select-Object -ExpandProperty Fullname | Out-File Somefile.txt
或简短版本:
ls | % fullname > somefile.txt
我在用:
(dir -r).FullName > somefile.txt
并带有过滤器*.log
:
(dir -r *.log).FullName > somefile.txt
笔记:
dir is equal to `gci` but fits the naming used in cmd
-r recursive (all subfolders too)
.FullName is the path only