2

Story: I have multiple folders with 1000+ files in each that are named similar to each other but are slightly different but they relate to the same content. For example, in one folder I have files named quite simply "Jobs to do.doc" and in another folder "Jobs to do (UK) (Europe).doc" etc.

This is on Windows 10, not Linux.

Question: Is there a script to compare each folder's content and rename them based on minimum similarity? So the end result would be to remove all the jargon and have each file in each folder (multiple) the same as one another but STILL remain in the retrospective folder? *Basically compare multiple folder content to one folders contents and rename them so each file in each folder is named the same?

Example:

D:/Folder1/Name_Of_File1.jpeg
D:/Folder2/Name_Of_File1 (Europe).jpeg
D:/Folder3/Name_of_File1_(Random).jpeg

D:/folder1/another_file.doc
D:/Folder2/another_file_(date_month_year).txt
D:/Folder3/another_file(UK).XML

I have used different file extensions in the above example in hope someone can write a script to ignore file extensions.

I hope this make sense. So either a script to remove the content in brackets and keep the files integrity or rename ALL files across all folders based on minimum similarity.

The problem is its 1000+ files in each folder so want to run it as an automated job.

Thanks in advance.

Joe Clough
  • 25
  • 4
  • Could you show us what you have tried so far? – 303 May 13 '17 at 17:41
  • 2
    Define "similarity" – Mathias R. Jessen May 13 '17 at 17:49
  • I think I have made it more confusing than it needs to be but I've run out of cafeen. I mean compare contents of one folder to another and rename so both folder contents have the same name but keep their file extension. When I said minimum similarity I meant the above, rename one folders content based on another folder content and if its 80% similar, rename it. so the select folders I choose , all the content of the folders will all be named the same. – Joe Clough May 13 '17 at 18:07
  • I dont have anything to work with as I dont know if what I want is possible and I am not overly sure what I am saying makes sense. rename Folder 2 and 3's content that match with folder 1s content. is that clearer? my brain hurts – Joe Clough May 13 '17 at 18:12
  • 2
    You should post a list of input files in both folders, and the list of the desired output based on such an input; don't forget to also include some names that will _not_ be renamed. I think this sould be easier that clearly define the rules of what you call "minimum similarity". – Aacini May 13 '17 at 20:06

1 Answers1

0

If the stuff you want to get rid of is always in brackets then you could write a regex like (.*?)([\s|_|]*\(.*\))

Try something like this

$folder = Get-ChildItem 'C:\TestFolder'
$regex = '(.*?)([\s|_|]*\(.*\))'
foreach ($file in $folder){
    if ($file.BaseName -match $regex){
        Rename-Item -Path $file.FullName -NewName "$($matches[1])$($file.extension)" -Verbose #-WhatIf
    }
}

Regarding consistency you could run a precheck using same regex

#change each filename if it matches regex and store only it's new basename
$folder1 = get-childitem 'D:\T1' | foreach {if ($_.BaseName -match $regex){$matches[1]}else{$_.BaseName}} 
$folder2 = get-childitem 'D:\T2' | foreach {if ($_.BaseName -match $regex){$matches[1]}else{$_.BaseName}}
#compare basenames in two folders - if all are the same nothing will be returned 
Compare-Object $folder1 $folder2

Maybe you could build with that idea.

Adam Mnich
  • 461
  • 3
  • 12
  • I very much appreciate a solution, this is always welcome. The only reason for the above request to compare and rename folders is for consistency, that way I know that ALL folders contents (that I specify) have the same name except for the file extension. Your powershell script gets me half way there by removing anything in brackets but I get this error: What if: Performing the operation "Rename File" on target "Item: X:\Test\TestFile01.txt: X:\Test\TestFile01.txt". – Joe Clough May 13 '17 at 20:23
  • TestFile01.txt shouldn't give a match with this regex. Odd. I have no error with this filename. – Adam Mnich May 13 '17 at 20:41
  • sorry correction. I missed off one bit. Error: What if: Performing the operation "Rename File" on target "Item: C:\Test\TestFile01 (Europe).zip Destination: C:\Test\TestFile01.zip". I left out the region in parenthesis which is the main reason I want to run this script it to remove anything in parenthesis including the parenthesis – Joe Clough May 13 '17 at 20:48
  • Remove the -WhatIf if you wan't the files to be renamed. It's not an error. – Adam Mnich May 13 '17 at 20:53
  • OK that could do it, Im having trouble because I have spaces in my folders names but working with that. youre definatly on the right lines and this please me greatly as ive had enough with thinking haha. so compare folder 1 with folder 2 and folder 3 folder4 and 5 (if neede) and rename to suit with Folder1 – Joe Clough May 13 '17 at 21:01
  • Your script work to remove anything in ( ) including the parentheses and this will do tremendously as an interim. I am still looking for a full solution of comparing files in other folders and renaming based on an 80% similarity. If anyone is wondering, the content in each file is different so cannot just do a replace, the File name is similar but slightly different hence the reason to want to rename to the same in each folder for consistency. – Joe Clough May 13 '17 at 21:17