0

I have a problem of renaming files in a complex tree structure of folders

Example folder structure:

C:\Folder1\Sub_Folder_1\file_1.extension

C:\Folder2\Sub_Folder_2\file_2.extension

C:\Folder2\file_3.extension

I want the files to be named as follows

Say file_1.extension from the first line is being considered with the following necessary metadata

file_1.extension properties:

Date Created: 17-07-2017

Full File Path: C:\Folder1\Sub_Folder_1\file_1.extension

Original File Name: file_1.extension

The full file name after renaming:

Template : Date Created_Full File Path_Original File Name

Hence : 17-07-17_c-folder1-subfolder1_file1.extension

I was able to successfully achieve this using Bulk Rename Utility

But I am not able to automate this process for any file that I create henceforth and have to end up running the BRU program multiple times.

Is there any way to automate BRU or even try and run a batch file to do the same?

1
  • 1
    Consider a Powershell script re-running every 1-5 minutes via Task Scheduluer. Get-Item will let you review the file properties then Rename-Item to get your template based name.
    – Clayton
    Jul 17, 2017 at 13:17

1 Answer 1

0

Here is a Powershell script which automatically renames all files in a given folder as long as the script is open and running

### SET FOLDER TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Users\nixda\Desktop\test"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

### SET ACTION AFTER A FILE IS CREATED
$action = {
    $item   = Get-Item -path $Event.SourceEventArgs.FullPath

    if (-not $item.PSIsContainer) {
        $date     = Get-Date $item.CreationTime -format yy-MM-dd
        $folder   = $item.DirectoryName -replace '\\', '-' -replace ':'
        $newName  = "$($date)_$($folder)_$($item.Name)"

        ### IF PATH ALREADY EXISTS, INCREMENT NUMBER UNTIL WE HAVE A FREE NAME
        $fullPath = "$($item.Directory)/$newName"
        while (Test-Path -Path $fullPath){
            $i++
            $newName = "$($date)_$($folder)_$($item.BaseName) ($i).$($item.Extension)"
            $fullPath = "$($item.Directory)/$newName"
        }

        Rename-Item $item -newName $newName
    } 
}    

### SET WHICH FILE EVENTS SHOULD BE WATCHED 
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}
5
  • Im currently examining why it stops working after 2-4 file renamings
    – nixda
    Jul 17, 2017 at 15:43
  • any heads up on that?
    – user171096
    Jul 21, 2017 at 14:46
  • @user171096 Silly me. It works, but I need to handle the case when the final file name already exists. Let me see...
    – nixda
    Jul 21, 2017 at 16:02
  • Also, how do I get the power shell script to run in the background? Something that needs to be set up only once but runs on system reboot...
    – user171096
    Jul 21, 2017 at 16:06
  • @user171096 Edited, should work now. Please test it. And for your second question, I recommend this method
    – nixda
    Jul 21, 2017 at 16:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .