Skip to main content
Added new information
Source Link
Reddy Lutonadio
  • 19.1k
  • 4
  • 20
  • 40

Try the following:

gci | Where { $_.Name -Match '\d{8}'} | ForEach {
        if ($_.Name -Match '^(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace  '^(\d{8})_(.+)$','$2_$1' ) }
        elseif ($_.Name -Match '^(.+_)(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace '^(.+_)(\d{8})_(.+)$', '$1$3_$2' ) }
    }

The code checks where the 8 digits are found in the filename (beginning or other) and rename the files accordingly. No need to check if the 8 digits are at the end.

Try the following:

gci | Where { $_.Name -Match '\d{8}'} | ForEach {
        if ($_.Name -Match '^(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace  '^(\d{8})_(.+)$','$2_$1' ) }
        elseif ($_.Name -Match '^(.+_)(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace '^(.+_)(\d{8})_(.+)$', '$1$3_$2' ) }
    }

Try the following:

gci | Where { $_.Name -Match '\d{8}'} | ForEach {
        if ($_.Name -Match '^(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace  '^(\d{8})_(.+)$','$2_$1' ) }
        elseif ($_.Name -Match '^(.+_)(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace '^(.+_)(\d{8})_(.+)$', '$1$3_$2' ) }
    }

The code checks where the 8 digits are found in the filename (beginning or other) and rename the files accordingly. No need to check if the 8 digits are at the end.

Source Link
Reddy Lutonadio
  • 19.1k
  • 4
  • 20
  • 40

Try the following:

gci | Where { $_.Name -Match '\d{8}'} | ForEach {
        if ($_.Name -Match '^(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace  '^(\d{8})_(.+)$','$2_$1' ) }
        elseif ($_.Name -Match '^(.+_)(\d{8})_(.+)$') { 
            Rename-Item $_ ( $_.Name -Replace '^(.+_)(\d{8})_(.+)$', '$1$3_$2' ) }
    }