Home > OS >  How to display file list with inverted comma using command prompt
How to display file list with inverted comma using command prompt

Time:12-25

I want to display file list covered with inverted comma with a comma using Command Prompt. Suppose my folder has few files like

abc.txt
hello.mp3
world.mp4

I want to run a script on command prompt which can store file name in a text file like this

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

I tried some option with dir command but didn't work.

CodePudding user response:

[edit - thanks to Compo for pointing out the missing "trailing comma".]

the following code will generate a list of file names wrapped in single quotes [also known as "apostrophe"].

what it does ...

  • grabs all the file names in the target dir
  • uses the -f string format operator to build the desired string
  • sends the string to the $QuotedFileNameList var
  • displays that collection on screen

you can save the list to a file with Set-Content. [grin]

the code ...

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

$QuotedFileNameList

output for me, today ...

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

CodePudding user response:

Above works on Windows Powershell. Here's a new method if you have Powershell

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

or splatting

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

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

output

'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',
  • Related