Better Way to Rename Files Based on Multiple Patterns

Rename multiple files by replacing a particular pattern in the filenames using a shell script

An example to help you get off the ground.

for f in *.jpg; do mv "$f" "$(echo "$f" | sed s/IMG/VACATION/)"; done

In this example, I am assuming that all your image files contain the string IMG and you want to replace IMG with VACATION.

The shell automatically evaluates *.jpg to all the matching files.

The second argument of mv (the new name of the file) is the output of the sed command that replaces IMG with VACATION.

If your filenames include whitespace pay careful attention to the "$f" notation. You need the double-quotes to preserve the whitespace.

Multiple file renaming based on regex pattern + renaming within files based on new file names

This solved it for me:

  • File renaming e.g. as described here: https://superuser.com/questions/16007/how-can-i-mass-rename-files
  • Batch replacement with this VS Code plugin: https://marketplace.visualstudio.com/items?itemName=angelomollame.batch-replacer
  • since I had the said table (old vs. new name) prepared, a simple regex replacement of the table entries did the trick to meet the prerequisites for the plugin, i. e. the old names were replaced by replace "old_file_name" and the new names by with "new_file_name"; then, just copy & paste everything for this plugin as described there and all is replaced

Renaming lots of files in Linux according to a pattern

My favorite solution is my own rename script. The simplest example that maps to your problems are these:

% rename 's/_/-/g' *
% rename 's/(\p{Lower})(\p{Upper})/$1 $2/g' *

Although I really hate whitespace in my filenames, especially vertical whitespace:

 % rename 's/\s//g' *
% rename 's/\v//g' *

et cetera. It’s based on a script by The Larry Wall, but extended with options, as in:

usage: /home/tchrist/scripts/rename [-ifqI0vnml] [-F file] perlexpr [files]
-i ask about clobbering existent files
-f force clobbers without inquiring
-q quietly skip clobbers without inquiring
-I ask about all changes
-0 read null-terminated filenames
-v verbosely says what its doing
-V verbosely says what its doing but with newlines between old and new filenames
-n don't really do it
-m to always rename
-l to always symlink
-F path read filelist to change from magic path(s)

As you see, it can change not just the names of files, but where symbolic links are pointing to using the same pattern. You don’t have to use a s/// pattern, although often one does.

The other tools in that directory are mostly for Unicode work, of which there are some super-useful ones.

Is there a way to rename files in a folder in a pattern using powershell?

The problem with your code is that within the loops to create the new file name, you do nothing with the file to rename itself, and you are overwriting the same variable $newFileName each time.

You could do like below:

$filePath = 'X:\tifs'  # put the path of the folder where the tif files are here
$letters = "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R"

# initialize two counters
$i = $j = 0
Get-ChildItem -Path $filePath -Filter '*.tif' | ForEach-Object {
$newFileName = '{0}{1:00}.tif' -f $letters[$i], $j++
$_ | Rename-Item -NewName $newFileName -WhatIf
if ($j -gt 20) {
$i++ # go to the next letter
$j = 0 # reset the number count
}
# if the letter counter has exceeded the number of letters, break out of the loop
if ($i -gt $letters.Count) { break }
}

The -WhatIf switch is there to test first. In the console window you can see what the Rename-Item cmdlet would do. If you are satisfied with what is shown, remove the Whatif switch to actually start renaming.



Related Topics



Leave a reply



Submit