How to rename using name of folder?

Hewlett-packard / Iq505
January 6, 2009 at 05:44:27
Specs: Windows Vista , Dual Core/4GB
Hello. How do I use a batch file to rename all files placed in a folder with the name of the folder. For example... If I have a folder named C:\Turtles, How do I rename the jpg file in this folder that is currently named C:\Turtles\paper.jpg to C:\Turtles\Turtles.jpg? I'm sorry if I am putting this question in the wrong forum... I haven't quite figured out how to navigate around the forums yet... :(

See More: How to rename using name of folder?

Report •

January 6, 2009 at 06:24:01
The Forum you accessed is the right one, but...

From your post it is not clear what exactly you wish to achieve. File names must be unique inside the same folder, so you can rename as you posted only if the folder contains ONE file. In that case how many folders do you want to access and what is their structure?

A batch is a program and needs to be driven by exact rules.

Report •

January 6, 2009 at 07:01:36
Thanks for the fast reply. I was trying to break the project into parts so it would be easier to do but that probably just made things more confusing eh. Well... here goes...
I have almost 700 unique text files that are currently in one directory. All of the files are named things like...


and so on.

The Main Directory is called C:\Turtles.

The first thing I need to do is create subdirectories in the Turtles folder for each of these files that will be named according to the file name so the Cromera.txt file should go into a folder named


and the Crucean.txt file should go into a folder named


and so on...

After this I will manually put a picture into these subfolders and then I will need a second batch file to rename the image based on the name of the subdirectory it is in, so if I put an image named lotus.jpg into the Cromera folder and an image named tortoise.jpg into the Crucean folder, they would be renamed to Cromera.jpg and Crucean.jpg, respectively. I hope I have not made things more confusing with this explanation. :( Your help is greatly appreciated.

Report •

January 6, 2009 at 07:45:32
for /f "tokens=*" %%a in ('dir /b /a-d *.txt') do mkdir "%%~na" & copy "%%a" "%%~na"

Report •

Related Solutions

January 6, 2009 at 08:21:11
...sorry but I'm pretty new at batch scripting... what does the script you gave me do and/or what do I need to substitute in it for it to work with my files... I know that I need to put it in the form of ...

@echo off
@for /f "tokens=*" %%a in ('dir /b /a-d *.txt') do mkdir "%%~na" & copy "%%a" "%%~na"

But I am sort of lost with what to do after this... please help if possible. thanks again.

Report •

January 6, 2009 at 08:23:25
First batch to create subfolders

:: MDSF.BAT Usage: mdsf
@echo off
pushd \Turtles
for %%j in (*) do (
md "%%~nj"
move "%%j" "%%~nj"
:: [End_Of_Batch]

After you have added the images to each subfolder

@echo off
:: MYREN.BAT Usage: myren
pushd \Turtles
for /D %%j in (*) do (
pushd %%j
for %%k in (*) do (
if not "%%~nk"=="%%j" ren "%%k" "%%j%%~xk"
:: [End_Of_Batch]

You can run the second script more than one time if you want to add the images in subsequent sessions.

Report •

January 6, 2009 at 08:25:47
2nd batch: (Run in c:\turtles\)

for /f "tokens=*" %%a in ('dir /b /ad') do (
for /f "tokens=*" %%b in ('dir /b /a-d "%%~a\*.jpg"2^>nul') do ren "%%~a\%%b" "%%~a.jpg")

Report •

January 6, 2009 at 08:33:41
Ok. I will try this right away and report back in a few moments.

Report •

January 6, 2009 at 08:59:16
Ok I have mixed results... First I want to say thanks a bunch for helping me on this. ...but, the second half of both of your solutions is not working out for me... I have tried running the second solution to rename the images from inside of the turtles folder... but no success... Am I doing something wrong?

Report •

January 6, 2009 at 09:03:40
lol, IVO also has some code by split of minutes. feel free to use either code, it does the same thing with different method.

about the first bat, use notepad to create the .bat file. example 1.bat.
then put these lines inside 1.bat, save and exit.

@echo off
for /f "tokens=*" %%a in ('dir /b /a-d *.txt') do mkdir "%%~na" & copy "%%a" "%%~na"

run the batch in c:\turtles\
the same step apply to the second batch

Report •

January 6, 2009 at 09:20:36
Hi again Reno. I have created a batch file using your code and a separate batch file using IVO's code and I copied these batch files into the Turtles folder.

First I ran your batch file from inside of the Turtles folder. Then I went to look inside of the subfolders that were there to see if the images had been renamed... but there they sat defiantly glaring back at me in protest of the operation... no change.

Then I tried running IVO's batch file and peeked cautiosly into the subfolders again... and sure enough the images in those subdirectories were still glaring back with no changes to their name... Please share any more advice on what steps I can take next. Thanks again.

Report •

January 6, 2009 at 09:31:12
on ivo code, if you copy&paste like me, change this line:
if not"%%~nk"=="%%j" ren "%%k" "%%j%%~xk"

add a space between not and "

if not "%%~nk"=="%%j" ren "%%k" "%%j%%~xk"

on my code, i dont see where goes wrong. except it force to rename even if the file name is the same as folder name and produce error on screen. extension is hardcoded, so check if the file inside the folder is ".jpg" extension?

Report •

January 6, 2009 at 09:34:25
About my second script... I edited that in minutes after posting as I found minor bugs (I tested it quickly by a command line version). So please go back to my post and create again from scratch the script as the code you copied may be faulting. The version you find in post #5 should be right.

Report •

January 6, 2009 at 09:38:15
Thanks reno, I did not notice that.

Report •

January 6, 2009 at 09:51:14
yippie!!!!!!!! Thank you thank you thank you! I wish for lots of hugs and cookies for both of you in the new year.

Reno you were exactly right. Your script only works with jpgs and I forgot that I had put bmp in my test folder. :( And your sharp vision caught the slight space issue in IVO's script. Thank you.

IVO your scripts are very universal and worked with both jpg and bmp pictures after I inserted the space. I also notice that your first codes had different results. IVO your code moved the files into the subdirectories while your codes copies them into the new directories, Reno. I am going to keep both of them for different tasks in the future. I might even start to build a little batch library. lol.

I am simply overjoyed with this since you have both saved me countless hours of work. Someday I hope to understand what the %% signs actually mean.... lol. Thanks again.

Report •

January 6, 2009 at 09:57:56
np =)
i am still trying to grab the process of "pushd/popd" idea and the for loop in (*) combination.
it works like charm. thanks to ivo code, i am learning new stuff.

Report •

Ask Question