Rename Multiple Directories Matching Pattern

Rename multiple directories matching pattern


find . -depth -name bar -type d -execdir mv {} baz \;

-execdir changes directory to the parent before executing the command, so the mv here will be local to each parent directory.

Rename Folders matching pattern using foreach loop

Write-Host is correctly outputting the folder name $homeFolder

You should use the same variable in your rename-item command instead of $_

Rename-Item $homeFolder -NewName $homeFolder.Name.Replace('My ', '')

However, this will only work if your current working directory is the same as the location of the folders you want to rename. Otherwise, use

Rename-Item $homeFolder.FullName -NewName $homeFolder.Name.Replace('My ', '')

Rename multiple folders with pattern in name using Batch

This should do it:

@echo off
setlocal enabledelayedexpansion

for /d %%a in (*) do (
set "p=%%a"
set "fp=!p:~0,8!" & set "tp=!p:~10!"
echo ren %%a !fp!M2!tp!
)

Remove the echo once you verify the output is what you want to do the actual rename.

Renaming all files within subdirectories

I suggest to use find, like this:

find . -type f -name '*hw4*' -execdir mv {} hw4.pl \;

How to rename multiple folders using a progressive number?

The batch file code for this folder renaming task:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Counter=1"
for /F "delims=" %%I in ('dir "D:\Programmi Installati\log_*" /AD /B /ON 2^>nul') do ren "D:\Programmi Installati\%%I" "log_!Counter!" & set /A Counter+=1
endlocal
pause

The command FOR runs with cmd.exe /C (more precise %ComSpec% /C) in a separate command process in background the command line:

dir "D:\Programmi Installati\log_*" /AD /B 2>nul

DIR outputs to handle STDOUT of this background command process

  • just the names of all directories because of option /AD (attribute directory)
  • in bare format because of option /B without folder path
  • matching the wildcard pattern log_*
  • in specified directory D:\Programmi Installati
  • ordered by name because of option /ON.

DIR would output an error message to handle STDERR if it can't find any directory entry matching these criteria. This error message is redirected to device NUL to suppress it.

Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.

FOR captures all lines output to handle STDOUT of started command process and processes those lines after started cmd.exe terminated itself. It is very important for this folder renaming task that FOR runs on a list of folder names captured before doing the folder renames as otherwise the directory entries would change while FOR is accessing them. For that reason for /D can't be used in this case because of for /D would process the list of log_* directory entries while this list changes on each successful folder rename. The result would be directories not renamed or renamed multiple times or even an endless running loop depending on file system (NTFS or a FAT file system like FAT32 or ExFAT).

FOR with option /F ignores empty lines which do not occur here. FOR ignores also lines starting with a semicolon because of end of line option eol=; is the default. But all lines output by DIR start with log_ and for that reason the default end of line definition can be kept for this task.

FOR with option /F splits up a line by default to substrings using normal space and horizontal tab as delimiters and assigns just first space/tab separated string to specified loop variable. This line splitting behavior is not wanted here because loop variable I should hold directory name with all spaces. Therefore delims= is used to define an empty list of delimiters to disable the line splitting behavior.

The directory name assigned to loop variable I is referenced with %%I and full path not output by DIR as source name of command REN. The new folder name is log_ with the counter variable value referenced using delayed environment variable expansion.

The counter variable is incremented by one using a simple arithmetic expression after renaming the directory independent on directory renaming was successful of failed for various reasons.

The command PAUSE at end is added to see the error message output by command REN if renaming a directory failed. There is nothing output except the prompt by PAUSE on all directories could be renamed successfully.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • pause /?
  • ren /?
  • set /?
  • setlocal /?

See also single line with multiple commands using Windows batch file for an explanation of operator &.

PS: I recommend the usage of the shareware file manager Total Commander which has a built-in multi-rename tool for renaming files and folders for people with no coding experience. Download, install and start Total Commander, navigate to the folder containing all these folders, press Ctrl+A to select the folders, press Ctrl+M to open multi-rename tool window and the rest is self-explaining. If you need nevertheless help, press key F1 to open the help page for multi-rename tool.

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

Rename Multiple folder in a directory with python

The src and dst aren't the absolute path, so it's trying to rename a file from the directory of the python script.

You should be able to fix this just by replacing os.rename(src, dst) with os.rename(os.path.join(path, src), os.path.join(path, dst)) to specify absolute path.



Related Topics



Leave a reply



Submit