Solved Batch file to rename files to its folder name

April 15, 2013 at 09:40:23
Specs: Windows 7
Please Help....

I have several hundred folders each containing one single pdf file named "f0.pdf". The folders are actually named with the unique name that I want all the pdf file to be named. I want to rename all the pdf files to their respective folder name so then I can copy them all to one folder without having duplicates and with a unique name


See More: Batch file to rename files to its folder name

Report •

✔ Best Answer
April 15, 2013 at 19:24:00
Ok, try this one: (safety on, using "echo" only)
@echo off & setlocal
:: you know, "whatever" for the path in foll. line
pushd \z\w\000
for /f %%a in ('dir /b /s f0.pdf') do (
call :x %%~pa
)
popd
goto :eof

:x
set x=%1
:: have to get rid of the last backslash
set x=%x:~0,-1%
for %%b in (%x%) do echo move %1\f0.pdf %%~nb.pdf
pause
::==== end batch
See if the echo'd output looks like what you want...



#1
April 15, 2013 at 10:31:46
@echo off & setlocal
md allpdf
for /f "tokens=*" %%a in ('dir /b /s f0.pdf') do (
for /f %%b in ('dir /b /a:d "%%~pa"') do (
copy %%a allpdf\"%%b.pdf"
)
)
::=== end
not sure, not tested.

Report •

#2
April 15, 2013 at 12:02:55
Ok, thanks, I will give it a try, however... Do I have to run the batch file from within a particular parent folder

Report •

#3
April 15, 2013 at 12:25:51
ok, i tried it from within multiple folders. If there was a fdf in that folder it created a folder called allpdf which had a file called allpdf.pdf. I must have been unclear so I will try to be more specific:

I have folder called "000"

within that folder are multiple folders called
"012", "013", "014" etc

within each of those folders are many folders called
"003840", "003841" etc

each of these folders has 1 pdf file called "f0.pdf"

I need to rename all of those files to their immediate parent name...ie "003841.pdf"

I hope that made more sense....OH and thanks GREATLY for your help!!!!


Report •

Related Solutions

#4
April 15, 2013 at 18:29:47
ok, i tried it from within multiple folders. If there was a fdf in that folder it created a folder called allpdf which had a file called allpdf.pdf. I must have been unclear so I will try to be more specific:
I have folder called "000"

within that folder are multiple folders called
"012", "013", "014" etc

within each of those folders are many folders called
"003840", "003841" etc

each of these folders has 1 pdf file called "f0.pdf"

I need to rename all of those files to their immediate parent name...ie "003841.pdf"

I hope that made more sense....OH and thanks GREATLY for your help!!!!


Report •

#5
April 15, 2013 at 19:24:00
✔ Best Answer
Ok, try this one: (safety on, using "echo" only)
@echo off & setlocal
:: you know, "whatever" for the path in foll. line
pushd \z\w\000
for /f %%a in ('dir /b /s f0.pdf') do (
call :x %%~pa
)
popd
goto :eof

:x
set x=%1
:: have to get rid of the last backslash
set x=%x:~0,-1%
for %%b in (%x%) do echo move %1\f0.pdf %%~nb.pdf
pause
::==== end batch
See if the echo'd output looks like what you want...


Report •

#6
April 15, 2013 at 21:04:34
nbrane,

OK, it runs but doesnt do anything....i think!

I want to save all the file exactly where they are just change name to the folder name.pdf


Report •

#7
April 15, 2013 at 21:37:25
Oh, I thought you wanted to rename them all into one directory.
"I want to rename all the pdf files to their respective folder name so then I can copy them all to one folder without having duplicates and with a unique name"
I was just trying to jump one step ahead(using the copy). Just change this line:
for %%b in (%x%) do echo copy %1\f0.pdf \wherever\%%~nb.pdf

to:

for %%b in (%x%) do echo move %1\f0.pdf %%~nb.pdf

(I'll fix it in post #5, and add a pause, because I'm still not sure of myself here...)
Note that "echo" will not execute the maneuver, but only "anticipate" the manuever for you to evaluate as to correctness.


Report •

#8
April 15, 2013 at 21:55:06
My bad, guess that would work too - I have no clue about batch files so didn't understand. Same with the "whatever" and "wherever" comments and the note about removing the last backslash - sorry for my lack of knowledge here!

Report •

#9
April 15, 2013 at 23:25:52
Well, np. Just want to get your situation resolved without the application of "extreme prejudice" Ha ha! :-) So, if the stuff (echo output) looks right, then we're go.

Report •

#10
April 16, 2013 at 06:40:58
Thanks nbrane it worked just the way I asked!!! However, I need one more modification as I failed to notice one little thing. All of the folders had six leading "0"s in the name....for example one folder is named 0000003840 so the corresponding pdf file got renamed 0000003840. All of the pdf got moved into one folder. Is there now a way to rename all of the files minus the leading "0"s. I REALLY appreciate your help!!!!

Report •

#11
April 16, 2013 at 08:52:21
If it's always 6, then this might work (run from the primary directory)
@echo off & setlocal
for %%a in (*.pdf) do call :zero %%a
goto :eof
:zero
set x= %1
set x=%x: 000000=%
echo ren %1 %x%
::==== end

Report •

#12
April 16, 2013 at 12:32:29
nbrane, THANKS FOR YOUR HELP!!!!! all good!

Report •

Ask Question