25
dir/b > files.txt

我想它必须在 PowerShell 中完成以保留 unicode 符号。

4

7 回答 7

42
Get-ChildItem | Select-Object -ExpandProperty Name > files.txt

或更短:

ls | % Name > files.txt

但是,您可以轻松地在以下位置执行相同操作cmd

cmd /u /c "dir /b > files.txt"

/u开关告诉cmd将重定向到文件中的内容作为 Unicode 写入。

于 2011-04-04T12:04:52.283 回答
18

Get-ChildItem实际上已经有一个相当于的标志dir /b

Get-ChildItem -name(或dir -name

于 2011-04-04T12:51:55.240 回答
5

简单的说:

dir -Name > files.txt
于 2017-11-01T02:09:34.210 回答
4

在 PSHdir中(其中别名Get-ChildItem)为您提供对象(如另一个答案中所述),因此您需要选择所需的属性。使用Select-Object(alias select) 创建具有原始对象属性子集的自定义对象(或可以添加其他属性)。

然而,在格式阶段这样做可能是最简单的

dir | ft Name -HideTableHeaders | Out-File files.txt

ftformat-table。)

如果您想在files.txtout-file默认使用 UTF-16)中使用不同的字符编码,请使用该-encoding标志,您还可以附加:

dir | ft Name -HideTableHeaders | Out-File -append -encoding UTF8 files.txt
于 2011-04-04T10:25:49.883 回答
3

由于 powershell 处理对象,因此您需要指定要如何处理管道中的每个对象。

此命令将仅打印每个对象的名称:

dir | ForEach-Object { $_.name }
于 2011-04-04T10:18:47.660 回答
2

刚刚发现这篇很棒的帖子,但子目录也需要它:

DIR /B /S >somefile.txt

采用:

Get-ChildItem -Recurse | Select-Object -ExpandProperty Fullname | Out-File Somefile.txt

或简短版本:

ls | % fullname > somefile.txt
于 2018-09-18T07:11:36.073 回答
1

我在用:

(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
于 2021-04-22T06:57:01.920 回答