Page 1 of 1

Renaming folder name same as one of the file inside.

PostPosted: Thu Aug 11, 2022 7:20 pm
by Zekrye
Hello, I hope this has not been answered yet because I cant find the one that really fit to my problem.

I have lots of jumbled folder name but the file inside at least have .txt and that txt is important. so is it possible to make the parent folder name same as the .txt file?

I.E. /jumbledmess1337/correctname.txt to /correctname/correctname.txt

Cheers.

Re: Renaming folder name same as one of the file inside.

PostPosted: Thu Aug 11, 2022 9:50 pm
by Luuk
When the bru is renaming any file/folder, it will never look at another file/folder.
If its me, I would probably just write a script to look inside the folders, before renaming them.

Rename folder to its only settled filename.txt

PostPosted: Fri Aug 12, 2022 1:32 am
by Luuk
This command to look inside of all sub-folders, and find any folders with having only one .txt-file inside...
For /f "delims=" %A IN ('dir/b/s/ad^|Sort/r') DO @Dir/b "%A\*.txt" 2>nul|Find /c /v ""|Findstr "^1$" 1>nul && For /f "delims=" %B IN ('dir/b "%A\*.txt"') DO @echo "%A" ----- "%~nB"

It does present a 'preview' to the screen like.... "C:\Path\To\Your\Folder-Name" ----- "New-Folder-Name" (from .txt-file)
If satisfied with "New-Folder-Names"?, first remove ----- and then change @echo ===> @ren to conduct the folder renames.
Extra spaces around ----- wont matter because Im always using extra anyways, and it begins renaming at the deepest folders first.

Must remember there is no way to "undo", unless saving the output from the first command!!

Re: Renaming folder name same as one of the file inside.

PostPosted: Mon Aug 15, 2022 4:23 am
by Zekrye
Thank you for the solution, I dont think Im that savvy with scripting but i'll try and see if it work.

Rename folder to its only settled filename.txt

PostPosted: Mon Aug 15, 2022 6:42 am
by Luuk
If not familiar with the command-line, first just type a command like... cd /d "C:\Path\To\Your\Main-Folder"
Then just type cd again, to verify that you are at the correct Main-Folder.

Then, to paste the long command after copying it... Right-click the top-left icon, then Edit, then Paste.
It does find all sub-folders with only one .txt-file (there can be other file-types, but only one .txt-file).
Then it just presents them to the screen like.... "C:\Path\To\FolderNowName" ----- "FolderNewName".

If the list is very long, and you like to make a preview-file, or just save a copy of the original names...
Just add > "C:\Users\YourUserName\Desktop\My-folder-preview.txt" to the very end of the command.
Then just make sure the preview-file is settled at your desktop, before conducting the renames.

If ready to edit the command for renaming, remember it only conducts folder-names with exactly 1 .txt-file inside.
Any folders with no .txt-files or with 2-or-more .txt-files, will not be shown in the preview, and will not be renamed.