Add Asterisks

This topic contains 2 replies, has 2 voices, and was last updated by  Dennis 2 weeks, 1 day ago.

  • Author
    Posts
  • #83521

    Dennis
    Participant

    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.

  • #83524

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

    Dennis
    Participant

    That worked perfectly. Thank you very much.

You must be logged in to reply to this topic.