Lelee14
Lelee14

Reputation: 15

Copy File Name to New File

I have a file that contains the date it was created in the file name:

2456Backup20150615.txt

I would like to be able to grab that file name and create a new blank text file with the same name and add the extension .pgp.trg:

2456Backup20150615.txt.pgp.trg

Is there anyway that that can be done in PowerShell?

Upvotes: 0

Views: 182

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175085

Use Get-ChildItem and Where-Object to find the relevant files, pipe them to ForEach-Object and create a new file with New-Item:

Get-ChildItem -Filter *.txt |Where-Object {$_.Name -match "\d{4}Backup\d{8}.txt"} |Foreach-Object { 
    New-Item "$($_.Name).pgp.trg" -ItemType File
}

Upvotes: 4

Related Questions