【问题标题】:How to display file list with inverted comma using command prompt如何使用命令提示符显示带引号的文件列表
【发布时间】:2022-01-23 15:55:24
【问题描述】:

我想使用命令提示符显示带有逗号和逗号的文件列表。假设我的文件夹有几个文件,如

abc.txt
hello.mp3
world.mp4

我想在命令提示符下运行一个脚本,它可以将文件名存储在这样的文本文件中

'abc.txt',
'hello.mp3',
'world.mp4',

我用 dir 命令尝试了一些选项,但没有奏效。

【问题讨论】:

  • for %a in (*) do @echo '%~nxa'
  • 在命令提示符而不是 powershell 上运行命令
  • ' 字符的名称是撇号。 In computing,通常称为单引号。
  • 感谢@SantiagoSquarzon 的建议
  • 作为旁注,单引号 are allowed 在 NTFS 中,所以 Sean O'Malley.jpg 是完全合法的文件名。如果你有这样的文件,解析单引号分隔的数据可能会很棘手。

标签: powershell cmd


【解决方案1】:

[编辑 - 感谢 Compo 指出缺少的“尾随逗号”。]

以下代码将生成一个包含在单引号中的文件名列表 [也称为“撇号”]。

它的作用...

  • 抓取目标目录中的所有文件名
  • 使用-f 字符串格式运算符来构建所需的字符串
  • 将字符串发送到$QuotedFileNameList var
  • 在屏幕上显示该集合

您可以使用Set-Content 将列表保存到文件中。 [咧嘴一笑]

代码...

$QuotedFileNameList = Get-ChildItem -LiteralPath $env:TEMP -File |
    ForEach-Object {
        "'{0}'," -f $_.Name
        }

$QuotedFileNameList

今天给我输出...

'.ses',
'ALSysIO64.sys',
'FXSTIFFDebugLogFile.txt',
'MpCmdRun.log',
'MTShell.m3u8',
'qtsingleapp-fmlast-93b-1-lockfile',
'~DF76B4F7376F975558.TMP',

【讨论】:

  • output to text file 想法包含在Set-Content 想法中。 ///// 我完全错过了尾随的逗号...我现在将添加...感谢您指出! [咧嘴]
【解决方案2】:

以上适用于Windows Powershell。如果你有Powershell,这是一个新方法

PS> Get-ChildItem C:\Windows\
| Join-String -SingleQuote -sep ",`n" -os ','

或喷溅

PS> $splat = @{ sep = ",`n"; os = ',' }

PS> Get-ChildItem C:\Windows\
| Join-String @splat

输出

'C:\Windows\addins',
'C:\Windows\appcompat',
'C:\Windows\apppatch',
'C:\Windows\AppReadiness',
'C:\Windows\assembly',
'C:\Windows\bcastdvr',
'C:\Windows\Boot',
'C:\Windows\Branding',
'C:\Windows\CbsTemp',
'C:\Windows\Containers',
'C:\Windows\CSC',
'C:\Windows\Cursors',
'C:\Windows\debug',
'C:\Windows\diagnostics',

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    相关资源
    最近更新 更多