Delgado
Delgado

Reputation: 55

Renaming multiple files at once that contains different chars

New to scripting and I've been looking into some other topics and tried different scripts but without success.

I want to add "_discard" to multiple files in different subdirectories in "projects" folder

The original file names are exactly the following:

first-file_us-tower_10.jpg
first-file_us-tower_tr_10.jpg
first-file_us-bridge_10.jpg
first-file_us-bridge_gps_10.jpg
first-file_us-bridge_tr_10.jpg
first-file_us-river_bbnf_10.jpg

What I want is the following:

first-file_us-tower_10_discard.jpg
first-file_us-tower_tr_10_discard.jpg
first-file_us-bridge_10_discard.jpg
first-file_us-bridge_gps_10_discard.jpg
first-file_us-bridge_tr_10_discard.jpg
first-file_us-river_bbnf_10_discard.jpg

This is what I'm attempting to use in a ps1 script but can't complete it.

$dir = "C:\pics\projects" 
$file = "*_us*.jpg"
$copy = "*_us*_discard.jpg" 

get-childitem -Path "$dir" | where-object { $_.Name -like "$file" } | %{rename-item -path $dir$_ -newname $d"_"$_}

Unfortunately without success. if this can be achieved in a Batch script is also welcome but I am not familiar with Batch.

Any help would be appreciated

Upvotes: 0

Views: 47

Answers (2)

user6811411
user6811411

Reputation:

As also files in subfolders should be renamed and
you surely want to exclude files already having the _discard attached

$dir    = "C:\pics\projects" 
$file   = "*_us*.jpg"
$append = "_discard"
Get-ChildItem "$dir\$file" -Recurse -File |
  Where-Object BaseName -notmatch $append |
    Rename-Item -NewName {"{0}{1}{2}" -f $_.BaseName,$append,$_.Extension} -WhatIf

If the output looks OK, remove the trailing -WhatIf

Upvotes: 0

Paal Braathen
Paal Braathen

Reputation: 208

Have you read the docs? There is an example doing almost exactly what you are trying to do. Example 4:

Get-ChildItem *.txt | Rename-Item -NewName { $_.name -Replace '\.txt$','.log' }

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/rename-item?view=powershell-6

PS C:\> (Get-ChildItem "C:\pics\projects").FullName
C:\pics\projects\first-file_us-bridge_10.jpg
C:\pics\projects\first-file_us-bridge_gps_10.jpg
C:\pics\projects\first-file_us-bridge_tr_10.jpg
C:\pics\projects\first-file_us-river_bbnf_10.jpg
C:\pics\projects\first-file_us-tower_10.jpg
C:\pics\projects\first-file_us-tower_tr_10.jpg

PS C:\> Get-ChildItem "C:\pics\projects" | Rename-Item -NewName {$_.Name -Replace "\.jpg$","_discard.jpg"}

PS C:\> (Get-ChildItem "C:\pics\projects").FullName
C:\pics\projects\first-file_us-bridge_10_discard.jpg
C:\pics\projects\first-file_us-bridge_gps_10_discard.jpg
C:\pics\projects\first-file_us-bridge_tr_10_discard.jpg
C:\pics\projects\first-file_us-river_bbnf_10_discard.jpg
C:\pics\projects\first-file_us-tower_10_discard.jpg
C:\pics\projects\first-file_us-tower_tr_10_discard.jpg

I removed the Where-Object since it matches all the filenames in your example.

Upvotes: 1

Related Questions