Zultran
Zultran

Reputation: 13

Generate file names list using cmd / powershell and adding coma after each file name?

I am trying to generate all file names in a folder which I am able to do using cd command accessing my folder location and then using dir /b > files.txt. This works just fine but I also would like to be able to add a comma after each file name and I'm not sure how to do that

Upvotes: 1

Views: 158

Answers (3)

user6811411
user6811411

Reputation:

In powershell ouput on one line:

(gci).Name -join(',')

Several lines

gci|%{$_.Name+","}

Cmd line several lines

for %A in (*) do @Echo %~nx,

On one line

Set "result="
for %A in (*) do @if not defined result (@set result=%A) else (call Set "result=%result%,%A")
Echo %result%

In a batch file change %A to %%A

Upvotes: 1

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174555

In PowerShell 3.0+ you could use Get-ChildItem -Name with the -join operator:

$FileList = (Get-ChildItem -Name) -join ','

In PowerShell 2.0 the same can be achieved with:

$FileNames = Get-ChildItem |%{$_.Name}
$FileList = [string]::Join(',',$FileNames)

Upvotes: 0

Ranadip Dutta
Ranadip Dutta

Reputation: 9133

This should help you out :

$names=ls | select Name
foreach($name in $names)
{
$result+=$name.Name + ","
}
$result.remove($result.length -1)| out-file Files.txt

Upvotes: 0

Related Questions