Rename folder to the first file inside of it?

Bulk Rename Utility How-To's

Rename folder to the first file inside of it?

Postby moonie211 » Mon Oct 28, 2019 4:56 am

Hi
This is probably a weird question, but I was wondering if there is a way to rename multiple folders to the first file inside each one?

For example, if the first file in a folder was named "Apple.jpg" the folder would be named "Apple"
moonie211
 
Posts: 1
Joined: Mon Oct 28, 2019 4:48 am

Re: Rename folder to the first file inside of it?

Postby bru » Tue Feb 11, 2020 7:57 pm

There might be a javascript way of doing this?
Here's a batch that could do it.. Note that it lacks any FolderName-spec, so it would process ALL SubFolders.
FolderNames are processed beginning with the deepest SubFolder upwards, so it cant try to rename folders whose path is already renamed.
If many folders in 1SubFolder had the same 1stFilename within, only 1 folder gets the name.

@echo off
:: cd /d "C:\YourFolderPath\"
cd /d "C:\Users\BRYCE FAIN\Desktop\Top"
SetLocal EnableDelayedExpansion
FOR /f "delims=" %%A IN ('dir/b/s/ad ^|sort /reverse') DO (
set do=1
For /f "delims=" %%B IN ('dir/b/a-d "%%A\" 2^>nul') DO (
If "!do!"=="1" @echo Ren "%%A\" "%%~nB" 2>nul
set do=2))
pause>nul

Run it as-is with a valid C:\YourFolderPath\ to simply display how it would rename.
The first dir-command could be enhanced like dir/b/s/ad "*xyz*" for only FolderNames containing xyz.
If it displays your desired results, remove @echo to let it perform the rename.
bru
 
Posts: 62
Joined: Wed Jan 31, 2018 7:35 pm


Return to How-To