Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Parsing Help - How to output a list of folder names into a text file

I have a simple script that reads the folder names and outputs them into a text file. I realized I got a lot more output then I wanted, so I used the select-item cmdlet to select just the name property from the hashtable. The problem is that there is still all the white space that the data I omitted would have normally filled, not helping my problem since the white space will destroy my script.

I have tried some [regex] commands to strip out the whitespace with (/S+) but I don't really know it that well I was using some code trying to tweak from an example someone helped me with. The topic name is the same as the title here and it is on this site too. Anyone that can help me I would appreciate it!

Basically, I cant figure out how to output the names of folders into a simple text file with ZERO whitespace (1 line per folder name).


$accFolder = Read-Host "Enter the account folder container....: "

$dataArray = Get-ChildItem "D:\influxcyst\$accFolder" | select-object name

$dataArray
$dataArray | Out-File $HOME\desktop\$accFolder.txt

$newArray = get-content $HOME\desktop\$accFolder.txt

#[regex]$regex = "\s(\S+)\s"
#[regex]::matches($newArray,$regex) | foreach-object {$_.groups[1].value}
like image 220
Matthew Avatar asked Jun 27 '11 19:06

Matthew


2 Answers

Try this one:

Get-ChildItem C:\Source\Path | ForEach-Object { $_.Name } > C:\Output\File.txt

Related resources:

  • ForEach-Object Cmdlet
like image 62
Enrico Campidoglio Avatar answered Oct 26 '22 18:10

Enrico Campidoglio


You can get just the names with the -Name switch:

$accFolder = Read-Host "Enter the account folder container....: "
Get-ChildItem -Name "D:\influxcyst\$accFolder" | Out-File $HOME\desktop\$accFolder.txt
like image 22
Shay Levy Avatar answered Oct 26 '22 16:10

Shay Levy