Solved Rename folder with first file content name

May 19, 2015 at 17:55:13
Specs: Windows 7
I have a parent folder with folders (images) underneath. Each folder has 1many files
I would like some advice on a batch script that I can run to rename the folders to the first file (image) containing within excluding the extension (.jpg)

e.g.

BEFORE

Parent (folder)

Folder 1
img0001.jpg
img0002.jpg

Folder 2
img0003.jpg
img0004.jpg

AFTER

Parent (folder)
img0001

img0001.jpg
img0002.jpg

img0003
img0003.jpg
img0004.jpg


See More: Rename folder with first file content name

Report •

#1
May 19, 2015 at 21:11:16
✔ Best Answer
Here's one possibility. The output goes to a test batch (TEST.BAT) and to the console screen. Nothing will be altered except the creation of TEST.BAT in the parent folder, for safety reasons. I have not tested it.
::begin batch
@echo off & setlocal enabledelayedexpansion
cd "c:\parent folder"
for /f "tokens=*" %%a in ('dir /b /ad') do (
call :xx "%%a"
)
goto :eof

:xx
for %%b in ("%1\*.*") do (
echo ren %1 %%~nb
>>test.bat echo ren %1 %%~nb&goto :eof
)


Report •

#2
May 19, 2015 at 21:30:54
"the first file"

Much depends on how you define first.

=====================

M2 Get custom script or take private lessons


Report •
Related Solutions


Ask Question