我想使用命令提示符顯示用逗號覆寫的檔案串列。假設我的檔案夾中有幾個檔案,例如
abc.txt
hello.mp3
world.mp4
我想在命令提示符下運行一個腳本,它可以將檔案名存盤在這樣的文本檔案中
'abc.txt',
'hello.mp3',
'world.mp4',
我用dir命令嘗試了一些選項,但沒有用。
uj5u.com熱心網友回復:
[編輯 - 感謝 Compo 指出缺少的“尾隨逗號”。]
以下代碼將生成用單引號 [也稱為“撇號”] 括起來的檔案名串列。
它能做什么 ...
- 獲取目標目錄中的所有檔案名
- 使用
-f字串格式運算子來構建所需的字串 - 將字串發送到
$QuotedFileNameListvar - 在螢屏上顯示該集合
您可以將串列保存到檔案中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',
uj5u.com熱心網友回復:
以上適用于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',
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/393507.html
