Alter folder name if it contains a file with specific name?

Bulk Rename Utility How-To's

Alter folder name if it contains a file with specific name?

Postby patchworkfr » Fri Jul 07, 2023 7:29 am

Hello, I've been using this great program for a while now and it's been immensely helpful with regular renaming tasks, but I have one a little more complicated than I usually do and wondered if it's possible to do with BRU?

I have a whole load of sub-folders and I want to rename any folder that has a file inside it with a specific name.
Basically I ripped a whole load of old CDs to back-up, then as I listened through them to see which I wanted to keep or not, I renamed the cover.jpg to denote this. Now I've realised I'd rather have that notation in the folder name, so I'd like a bulk rename format something like this:

if directory has file inside called [keep].jpg, then rename directory [existing foldername]_[keep]

If this is possible with BRU it will certainly save the time doing it manually, fingers crossed! :P

Thank you.
patchworkfr
 
Posts: 2
Joined: Fri Jul 07, 2023 7:18 am

Rename folder if contains a filename

Postby Luuk » Fri Jul 07, 2023 11:03 am

When the bru is renaming a folder, it wont look at the filenames, but could use a batch like..

@echo off
SetLocal EnableDelayedExpansion
For /f "delims=" %%A IN ('dir /b/s/ad "C:\Path\To\Your\MainFolder"') DO (
Set folder=%%A
If NOT !folder:~-7!==_[keep] If exist "%%A\[keep].jpg" echo ren "%%A" "[%%~nA]_[keep]")
echo done
pause>nul

As long as this batch uses echo, it will only list a preview for how the folders can be renamed.
It lists folders containing "_[keep].jpg", but not if the folders already have _[keep] at the end.
So just edit the red-part, and maybe the new-name format if you like to change the brackets?

When decided on the new-name format, can then remove echo to conduct folder renaming.
Could also edit this batch to rename your files back into cover.jpg with something like...

@echo off
SetLocal EnableDelayedExpansion
For /f "delims=" %%A IN ('dir /b/s/ad "C:\Path\To\Your\MainFolder"') DO (
Set folder=%%A
If NOT !folder:~-7!==_[keep] If exist "%%A\[keep].jpg" (
echo ren "%%A\[keep].jpg" cover.jpg
echo ren "%%A" "[%%~nA]_[keep]"))
echo done
pause>nul

This one is exactly the same, except renaming the file first, before renaming the directory.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: Alter folder name if it contains a file with specific name?

Postby patchworkfr » Fri Jul 07, 2023 12:57 pm

That seems a little bit over my head, so I'm going to have to take some time read that and see how I do it - I'm only familiar with the GUI interface of the program nothing further than that, but I really appreciate your help, thank you. :)
patchworkfr
 
Posts: 2
Joined: Fri Jul 07, 2023 7:18 am

Rename folder if contains a filename

Postby Luuk » Fri Jul 07, 2023 11:32 pm

You can paste it into a text-file, and then save it with either the .cmd or .bat extension.
So then, after editing your C:\Path, and clicking onto the batch, presents something like...
ren "C:\Path\To\Your\MainFolder\FolderName1" "[FolderName1]_[keep]"
ren "C:\Path\To\Your\MainFolder\FolderName2" "[FolderName2]_[keep]"
ren "C:\Path\To\Your\MainFolder\FolderName3" "[FolderName3]_[keep]"

Its only presents folder-names without _[keep] at the end, and if they contain "[keep].jpg".
After verifying the correct new-name format? then its safe to remove all of the red echo's.
So then, clicking onto the batch, would conduct those renames (instead of echoing).

If preferring to review your new-names inside of a text-file, instead of the command-line...
Can just add >"C:\Path\To\SomeTextFile.txt" to the very end of any lines with a red echo.
Then after any experiments, can just open SomeTextFile.txt to preview everything first.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm


Return to How-To