Rename Files and Directories (Add Prefix)

Rename multiple files in a folder, add a prefix (Windows)

Option 1: Using Windows PowerShell

Open the windows menu.
Type: "PowerShell" and open the 'Windows PowerShell' command window.

Goto folder with desired files: e.g. cd "C:\house chores"
Notice: address must incorporate quotes "" if there are spaces involved.

You can use 'dir' to see all the files in the folder. Using '|' will pipeline the output of 'dir' for the command that follows.

Notes: 'dir' is an alias of 'Get-ChildItem'. See: wiki: cmdlets.
One can provide further functionality. e.g. 'dir -recurse' outputs all the files, folders and sub-folders.

What if I only want a range of files?

Instead of 'dir |' I can use:

dir | where-object -filterscript {($_.Name -ge 'DSC_20') -and ($_.Name -le 'DSC_31')} |

For batch-renaming with the directory name as a prefix:

dir | Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}

Option 2: Using Command Prompt

In the folder press shift+right-click : select 'open command-window here'

for %a in (*.*) do ren "%a" "prefix - %a"

If there are a lot of files, it might be good to add an '@echo off' command before this and an 'echo on' command at the end.

How to rename with prefix/suffix?

In Bash and zsh you can do this with Brace Expansion. This simply expands a list of items in braces. For example:

# echo {vanilla,chocolate,strawberry}-ice-cream
vanilla-ice-cream chocolate-ice-cream strawberry-ice-cream

So you can do your rename as follows:

mv {,new.}original.filename

as this expands to:

mv original.filename new.original.filename

How to rename all files in a directory adding prefix of current unix date

If I understand your question, then you could use the $(date +%s) command substitution syntax to get the command output (and I suggest quotes) like

for i in *; do mv "$i" "$(date +%s)_$i"; done

Renaming multiple files in a folder by adding Prefix using python

You can use glob to find all the html files:

from glob import glob
import os
pre = "xyz_"
[os.rename(f, "{}{}".format(pre, f)) for f in glob("*.html")]

html files starting with a . should be ignored as glob treats filenames beginning with a dot (.) as special cases..

def glob(pathname):
"""Return a list of paths matching a pathname pattern.

The pattern may contain simple shell-style wildcards a la
fnmatch. However, unlike fnmatch, filenames starting with a
dot are special cases that are not matched by '*' and '?'
patterns.

"""
return list(iglob(pathname))

Append a prefix to all files recursively with rename

By default, rename modifies the entire path. The filename can be considered alone by passing the -d flag. Also note that the * glob will match all files in the current directory, not just the ones you pass to stdin. So to prepend 00_ to the filenames, use:

find . -name "*.pdf" | rename -d 's/^/00_/'

You can also add the -n flag to do a dry-run where the renames are printed and not executed.

Rename multiple files in a folder, add a line counts as prefix (Powershell)

What you are doing is almost fine, the error comes from trying to concatenate an int with a string, PowerShell attempts type conversion of all elements to the type of the leftmost object in the operation:

The operation that PowerShell performs is determined by the Microsoft .NET type of the leftmost object in the operation. PowerShell tries to convert all the objects in the operation to the .NET type of the first object. If it succeeds in converting the objects, it performs the operation appropriate to the .NET type of the first object. If it fails to convert any of the objects, the operation fails.

Since the leftmost object in this case (the .Length property) is of the type int PowerShell attempts to convert the rest to int and it fails, for example:

PS /> 1 + 'A'
InvalidArgument: Cannot convert value "A" to type "System.Int32". Error: "Input string was not in a correct format."

This would be easily fixed by type casting the returned value to sring:

-NewName { [string](Get-Content $_).Length + "_" + $_.BaseName + $_.Extension }

Or for example using string formatting or the -f format operator:

-NewName { '{0}_{1}{2}' -f (Get-Content $_).Length, $_.BaseName, $_.Extension }

As for "the bonus", with string formatting see How do I control the number of integral digits?

In this case you can use {0:0000}, as an example:

(0..1000).ForEach({'{0:0000}' -f $_})

On the other hand, if you have many lengthy files, [System.IO.File]::ReadAllLines(...) is likely to be faster than Get-Content:

-NewName { '{0:0000}_{1}' -f [System.IO.File]::ReadAllLines($_).Length, $_.Name }
# OR
$io = [System.IO.File]
-NewName { '{0:0000}_{1}' -f $io::ReadAllLines($_).Length, $_.Name }


Related Topics



Leave a reply



Submit