Windows script rename files

auberginehill / Rename-Files.ps1

Rename-Files.ps1
#>
# Find all wmv-files with a string «oldstring» and replace «oldstring» with «newstring» in the filename
Get-ChildItem * .wmv — Filter » *oldstring* » | ForEach
# Change the file extension of all .jpeg files to .jpg
# Credit: Ohad Schneider: «How can I bulk rename files in PowerShell?»
Get-ChildItem * .jpeg | Rename-Item — NewName
# Rename all PDF-files in a folder to lowercase
Get-ChildItem * .pdf | Rename-Item — NewName
# Replace the space character(s) with an underscore in all the filenames found in the current folder and the successive subfolders
Get-ChildItem — Recurse — Force — Filter » * * » | Rename-Item — NewName
# Rename files with an increasing number
Get-ChildItem * .jpg | ForEach-Object — Begin < $count = 1 >— Process
# Rename files with an increasing number
Get-ChildItem * .jpg | ForEach-Object — Begin < $count = 1 >— Process
# Inserting a prefix into filenames
$source_path = » C:\Temp «
$filter = » *.pdf «
$new_prefix = » ACertainNewPrefix «
# Retrieve files with the $source_path and $filter parameters, for example all PDF-files from C:\Temp
# $filter = «» parameter value would retrieve all available instances, i.e. no filtering
$files = Get-ChildItem — Path $source_path — Filter $filter
# Process each file and add the $new_prefix to the filenames
ForEach ( $file in $files ) <
$old_file_name = $file .Name
$new_full_name = » $ ( $file .DirectoryName ) » + » \ » + » $ ( $new_prefix ) » + » $ ( $old_file_name ) «
# Rename the file (perhaps first with the -WhatIf parameter?)
# Rename-Item $file.FullName -NewName $new_full_name -WhatIf
Rename-Item $file .FullName — NewName $new_full_name
> # ForEach $file
# Source: Don Jones: «Appending text to the beginning of a variable file name» https://powershell.org/forums/topic/appending-text-to-the-beginning-of-a-variable-file-name/
Renaming Multiple Files in Windows Explorer (known as File Explorer in Windows 10)
(1) Start by selecting a bunch of files: one can hold down for example.
(a) the Ctrl key to select multiple files with multiple clicks or
(b) the Shift key to select a range of files.
(2) After the files are selected, use a rename command:
(a) the button on the Home menu,
(b) the command on the context menu or
(c) the F2 key
Result: All the files remain selected, but the first one in the group gets its name highlighted.
(3) Type a new name for the (highlighted) file.
(4) Hit Enter or click somewhere else in the window.
Result: All the selected files are renamed using the name that was just typed in
and are appended with a number in parentheses to differentiate them.
# Source: http://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/
#>
# Source: help Rename-Item -Full
# Source: Jeffrey Snover: «Renaming Files»: https://blogs.msdn.microsoft.com/powershell/2007/03/06/renaming-files/
# Source: John Savill: «Use Windows PowerShell to Rename Files»: http://windowsitpro.com/powershell/use-windows-powershell-rename-files
# Source: Ed Wilson: «Use PowerShell to Rename Files in Bulk»: https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/22/use-powershell-to-rename-files-in-bulk/
# Source: Ohad Schneider: «How can I bulk rename files in PowerShell?»: http://stackoverflow.com/questions/13382638/how-can-i-bulk-rename-files-in-powershell/36241702#36241702
# Source: http://tweaks.com/windows/49459/batch-file-rename-with-windows-powershell/
# Source: http://www.howtogeek.com/111859/how-to-batch-rename-files-in-windows-4-ways-to-rename-multiple-files/

This comment has been minimized.

Copy link Quote reply

auberginehill commented Feb 23, 2017 •

Changelog

  • 20180716: typo
  • 20190124: added a «Inserting a prefix into filenames» -section

This comment has been minimized.

Copy link Quote reply

jonathan987 commented Sep 6, 2017 •

Very useful code but I use Batch Rename Files Tool because is easier to used. You can found hier BatchRenameFiles.org that allows you to quickly rename all the files in a specified directory.

This comment has been minimized.

Copy link Quote reply

csharpforevermore commented Feb 27, 2020

Nice example code. I prefer this script rather than being constrained with some BatchRenameFiles tool. Funny when I see someone trying to market their own terrible products by spamming boards like this.

Nice work with the script. Thank you for providing it!

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

windows batch script for renaming files

I have a lot of files in a windows folder. These file names contain a lot of underscores as well. E.g.:

I want to rename these files such that the last underscore and everything following it is removed and the files are renamed as follows:

Note: The number of underscores varies. Some filenames may have just a single underscore, whereas other filenames may have up to 8 underscores. I am looking for a Windows batch script which could do the trick and not PowerShell commands.

This is the code that I am using currently to rename the files considering that the file names have only one underscore in it:

xi» GOTO :EOF This is the code that I am using currently to rename the files considering that the file names have only one underscore in it. – UserA Jun 22 ’17 at 7:13

2 Answers 2

You would need to change the setting of sourcedir to suit your circumstances.

The required REN commands are merely ECHO ed for testing purposes. After you’ve verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

Perform a directory scan of all filenames matching the mask. WIth each name found, using delayed expansion, assign the name to filename and then replace each _ with Space _

Use a simple for to assign newname to the original filename with the _string removed (replaced by nothing) and add back the extension using %%

Windows Script to Rename and move files

I have been tasked with creating an archive process using either a Windows Batch or PowerShell script. I have seen a few examples here on StackExchange but nothing that does exactly what I need and I am running into some issues.

Here is the background: I have 3 folders

Our main system puts xml files into the Incoming directory and I have to create a script that will run every 5 mins and do the following .

  1. Iterate through all the xml files in the Incoming folder
  2. Rename them to OriginalFilename.ready_to_archive
  3. Copy all ready_to_archive_files into the Archive directory
  4. Once in the archive directory rename them back to OriginalFilename.xml
  5. Copy all ready_to_archive_files into the Outgoing directory
  6. Once in the Outgoing directory rename them back to OriginalFilename.xml
  7. Delete all ready_to_archive_files from the Incoming directory

Of course if any stage fails then it should not go to the next one since we do not want to delete files that have not been archived properly.

I have had a look at Folder iteration with Move-Item etc but I run into so many issues. This is really not my main working field so any help would be appreciated.

Here is the PowerShell script I created:

And here is a screenshot of the execution: (Ok I do not have enough rep points to add a screenshot so here is a txt dump of the output) .

The only thing that actually works in this script is that the .xml file in folder ‘one’ is properly renamed to .ready_to_archive’ but nothing else happens.

Script to rename files

I have about 2200 different files in a few different folders, and I need to rename about about 1/3 of them which are in their own subfolder. Those 700 are also in various folders as well.

For example, there might be The top-most folder is Employees, which has a few files in it, then the folder 2002 has a few, 2003 has more files, 2004 etc.

I just need to attach the word «Agreement» before the existing name of each file. So instead of it just being «Joe Schmoe.doc» It would be «Agreement Joe Schmoe.doc» instead.

I’ve tried googling such scripts, and I can find stuff similar to what I want but it all looks completely foreign to me so I can’t understand how I’d modify it to suit my needs.

Oh, and this is for windows server ’03.

2 Answers 2

I need about 2 minutes to write such script for *NIX systems (may be less), but for Windows it is a long song . ))

I’ve write simple VBS script for WSH, try it (save to .vbs, change Path value (on the first line of the script) and execute). I recommend to test script on small amount of data for the first time just to be sure if it works correctly.

I used to do bulk renaming with batch scripts under Windows. I know it’s a snap on *nix (find . -maxdepth N -type f -name «$pattern» | sed -e ‘p’ -e «s/$str1/$str2/g» | xargs -n2 mv). Buf after some struggle in vain, I found out, to achieve that effect using batch scripts is almost impossible. So I turned to javascript.

With this script, you can add prefix to file names by ‘rename.js «s/^/Agreement /» -r *.doc’. A caret(^) means to match the beginning. The ‘-r’ options means ‘recursively’, i.e. including sub-folders. You can specify a max depth with the ‘-d N’ option. If neither ‘-r’ or ‘-d N’ is given, the script does not recurse.

If you know the *nix ‘find’ utility, you would notice that ‘find’ will match the full path (not just the file name part) to specified regular expression. This behavior can be achieved by supplying the ‘-f’ option. By default, this script will match the file name part with the given regular expression.

Use This Script to Rename Multiple Files at Once in Windows

You’ll save yourself boatloads of time

Do you need to rename several files at once in Windows? It can be quite the task to do it manually, but Windows supports scripts that you can run to automate the renaming process, saving you loads of time.

As an example, consider a case like in the example image above where you have a folder of hundreds of images each named Copy of and then a word or two, like Copy of Black Tea.jpg.

Instead of manually renaming each file to delete “Copy of” or to change those words to something else, you could run a script to do all the renaming for you.

Software programs and cameras often append a specific set of characters to exported files, so this script comes in handy in those circumstances.

How to Make the Renaming Script

A script is essentially a carefully crafted set of commands to tell the computer exactly what to do. Here’s the “find and replace” script we’re dealing with:

Set objFso = CreateObject(“Scripting.FileSystemObject”)

Set Folder = objFSO.GetFolder(“ENTER\PATH\HERE”)

For Each File In Folder.Files

sNewFile = File.Name

sNewFile = Replace(sNewFile,”ORIGINAL”,”REPLACEMENT”)

if (sNewFile<>File.Name) then

File.Move(File.ParentFolder+”\”+sNewFile)

end if

To use this script requires a text editor. Notepad, built-in to Windows, will do just fine.

Step 1: Open Notepad. You can do this by searching for Notepad in the Start menu or by executing the notepad command in the Run dialog box (WIN+R).

Step 2: Copy the script exactly as it’s shown above, and paste it into Notepad.

Step 3: Edit the file rename script to make it apply to your unique situation.

To do that, you need to change the text called ENTER\PATH\HERE to the exact folder where your soon-to-be-renamed files are located.

For example, maybe you want to rename a group of files in a folder on your desktop, in which case your folder path might look like this: C:\Users\Matt\Desktop\Converted MP3s\.

To make the script always apply to the folder it’s currently located in, just change the path to .\. That is, a period and then a backslash, without a space. Using the script this way lets you drop it into any folder and have it automatically apply to only that folder.

Also change ORIGINAL to the characters you want to replace, and delete REPLACEMENT so that you can enter the text that should replace the original characters. In other words, you can read this line of the script as “replace THIS with THIS.

Note: Make sure you keep the quotes in every instance you see them. They need to remain in the folder path and the replace section.

Step 4: Go to File > Save As and name the file anything you like, but be sure to change the “Save as type” option to All Files (*) and append .vbs to the end of the file name.

Step 5: You can now close out of Notepad and execute the VBS file to apply the script.

That’s it! To edit the VBS file to change the location of the files to rename, or to adjust what to replace in the files, just right-click the VBS file like you see above, but instead of opening it, choose Edit.

How to Bulk Rename Files in Windows 10

If you’re using Windows 10, there’s a built-in renaming feature that’s easy to use and might be exactly what you’re after. This method is unique compared to the script above because it works even if the files have completely different filenames.

In other words, these two methods have completely different use cases. Let’s say you have 100 files that each have the word house in them along with other random characters. You want to keep all the characters untouched but make the word house into home. The script is great for that.

However, if the 100 files are all named random characters and you want them to be really similar like housepics, you can use the Windows 10 renaming function to rename the first to housepics (1), the second to housepics (2), the third to housepics (3), and so on.

Here’s how to do this in Windows 10:

Step 1: Highlight the files you want to rename.

Step 2: Press the F2 key or right-click one of the selected files and choose Rename.

Step 3: Type the filename you want to use and then press Enter.

Instantly, every selected file will use the exact same filename. If each file is using a different file extension, they’ll all be named identically, but if they have the same file extension, a number will be appended to the end since two or more files can’t use the same filename in the same folder.

Founder of Help Desk Geek and managing editor. He began blogging in 2007 and quit his job in 2010 to blog full-time. He has over 15 years of industry experience in IT and holds several technical certifications. Read Aseem’s Full Bio

Читайте также:  Что делать если неправильно установил windows
Оцените статью