Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Regex rename multiple files

$
0
0

Hi,  

I have a direcotry with about 20 .sql files which i need to rename. File pattern is lile:

"TestRun_Database_name_DB_Update_Build_19.3.1.sql"
"TestRun_Database_name_DB_Update_Build_19.3.1.sql"
"TestRun_Database_name_DB_Update_Build_19.3.1.sql"


What i need is to rename all files to just:

"Database_name.sql"

I have a regex pattern to match: TestRun_  -> "^(?i)[a-z]{7}_" ,
and regex pattern to macth : _DB_Update_Build_19.3.1 - > "_(?i)[a-z]{2}_[a-z]{6}_(?i)[a-z]{5}_\d{1,3}\.\d{1,3}\.\d{1,3}"
I am trying to remove those two parts from file name with script bellow.




    $prefix = "^(?i)[a-z]{7}_"
    $suffix = "_(?i)[a-z]{2}_[a-z]{6}_(?i)[a-z]{5}_\d{1,3}\.\d{1,3}\.\d{1,3}"
    $files = Get-ChildItem -path "D:\dbfiles" -filter *.sql
    
         foreach ($file in $files) {
      
           {
              rename-item -newname  { $_.name -replace $prefix, " " -and  $_.name -replace $suffix, " " }
           }
    }


In ISE console script passes with no errors, but files are not renamed.
Can anyone help me a little here.
Thanks,


Viewing all articles
Browse latest Browse all 6937

Trending Articles