Author Posts

November 2, 2017 at 8:10 pm

Hi

Let's say I have a thousand files within C:\Test 1\
File (1).txt
File (2).txt
...
File (999).txt
File (1000).txt

I want to read, sort and export those to another textfile. I've accomplished the above through these two commands:

Set-Location -Path C:\"Test 1"
Get-ChildItem *.txt | Sort-Object {$_.BaseName -replace "\D+" -as [Int]} | Select-Object -ExpandProperty Name > !files.txt

All filenames are now within !files.txt, neatly sorted. Ultimatively I want it so that each entry within !files.txt is headed by an asterisk *

So like this (within !files.txt):
*File (1).txt
*File (2).txt
...
*File (999).txt
*File (1000).txt

Is there a way to do this? Thanks.

November 2, 2017 at 9:39 pm

Get-ChildItem *.txt | Sort-Object {$_.BaseName -replace "\D+" -as [Int]} | Select-Object -ExpandProperty Name | 
ForEach-Object {Write-Output "*$_" | Out-File '!files.txt' -Append -NoClobber}

November 2, 2017 at 10:22 pm

That worked perfectly. Thank you very much.