Solved add folder name to beggining of all files in all subfolders

December 16, 2011 at 14:40:41
Specs: Windows Vista
i want to add folder name to beggining of all files in current directory and all sub directories

example i have folder called "music album" it has 2 sub folders called "disc 1 " and "disc2"

with 20 files in each one ...

i would like to rename all the files prefixing thier file name with "music album" being name of top folder ..

being wishful it would be nice if i could also add "disc1 " if in subfolder "disc1" and "disc2" etc

i dont want the full path used only the portion from the directory batch file run in

i am not going to offer any of my efforts as an ammeture it would only provide you with a smile and add to my embarrasement

i will however offer sincere Thanks that anyone even at least looked at my problem

my attempts so far have been using a batch file run through command prompt

regards and hoping ....


See More: add folder name to beggining of all files in all subfolders

Report •

✔ Best Answer
January 2, 2012 at 19:47:50
Returning as promised. The following is based on the test setup details provided by you except that you did not state which disk/partition you had installed the files on. I have assumed (why do I do that?) that it is the default, in my case C:\

The first script is the one I have used to set up the files. It should be copied/pasted into the default under the Command Prompt. Note that directories will be removed when this script is run, if there are directories in Downloads\test\ which should not be removed please take action to protect them.

Script 1.

@echo off
cls
setlocal enabledelayedexpansion

if exist Downloads\test\disc1\ rd /q /s Downloads\test\
if exist Downloads\test\disc2\ rd /q /s Downloads\test\

for /l %%1 in (1,1,2) do (
    md Downloads\Test\Disc%%1\
)

pushd Downloads\Test\Disc1\
call :crefiles
popd
pushd Downloads\Test\Disc2\
call :crefiles
popd

dir Downloads\test\disc1\
dir Downloads\test\disc2\
exit /b

:crefiles
for /l %%1 in (1,1,20) do (
    if %%1 lss 10 (set fileno=0%%1)
    if %%1 gtr 9 (set fileno=%%1)   

    echo ryryryry>"track!fileno!.mp3"
    )


The second script is the one which does the renaming. It should be copied/pasted in Downloads\test\ and run from the GUI, running it in the Command Prompt will generate many errors.

Script 2.

@echo off
cls
setlocal enabledelayedexpansion

pushd %cd%

for /f "tokens=*" %%A in ('dir /a-d /s /b') do (
    set filename=%%A

    for /f "tokens=1-3* delims=\" %%1 in ("!filename!") do (
    set albumin=%%2&set file=%%4
    set file=!file:\= !
    ren "!filename!" "!albumin! !file!"
  )
)

exit /b


Please come back & tell us if your problem is resolved.



#1
December 17, 2011 at 03:15:44
ok these are the bits of code i got from searching maybe someone can see how to get them to work together to achieve

%~pI - expands %I to a path only

example
C:\Temp>batchparams.bat c:\windows\notepad.exe
%~1 = c:\windows\notepad.exe
%~f1 = c:\WINDOWS\NOTEPAD.EXE
%~d1 = c:
%~p1 = \WINDOWS\
%~n1 = NOTEPAD
%~x1 = .EXE

to rename recursive

setlocal enabledelayedexpansion

_______________________________________________________________

for %%j in (*) do (set filename=%%j rename !filename! "%MyDir%"!filename!)
________________________________________________________________

or

________________________________________________________________

for /r "< DIR >" %%x in (*.c) do ren "%%x" *.cpp


for /r %%x in (%1) do ren "%%x" %2
________________________________________________________________
this example currently renames all files *.c to *.cpp


this line i not sure how it would help but might

set WORKING_DIRECTORY=%cd%

..............................................................................................................

this might not help at all .......


Report •

#2
December 29, 2011 at 23:22:55
Please test this script - note that it will rename files so it should be run in a testing environment until proved. It has not been tested under Win Vista so the outcome is unclear.

Good luck.

@echo off
cls
setlocal enabledelayedexpansion

echo.&echo.&echo.&echo.&echo.
echo             Enter path and album name
echo             e.g. D:\album name
echo.
set /p album=            
if "%album%" equ "" echo Album path and name not entered - job terminated&&exit /b

if not exist "%album%\" (echo Album entered [%album%] does not exist - job terminated&&exit /b)
cls
for /f "tokens=*" %%A in ('dir /a-d /s /b "%album%"\') do (
    set filename=%%A

    for /f "tokens=1-4 delims=\" %%1 in ("!filename!") do (
    set albumin=%%2&set disc=%%3&set file=%%4
    ren "!filename!" "!albumin! !disc! !file!"
  )
)




Please come back & tell us if your problem is resolved.


Report •

#3
December 30, 2011 at 03:55:16
Wow looks really good ...

but !!

before i test the code need to ask a question

do i run the batch file from the directory of the album

or does it not matter where it is run from .....

---------------------------------------------------------------------------------------------------------

secondly is it possible for it to be modified so it takes the album name from the folder that it is run in

reason i ask is sometimes paths can be quite long and would avoid typo's and would also offer a little more security in the fact that it would prevent using wrongly
avoiding need to ask my first question ..

and if not to much to ask could you put a few rem statements in explaining what each line does. i ask this so i can learn what each part does and avoid having to keep coming back asking you to do it for me ..

also it might help others reading post to understand and modify for thier needs

-----------------------------------------------------------------------

but THANK YOU very much for so far

am not sure if i set as best answer yet as your reply could be better

regards

.....


Report •

Related Solutions

#4
December 30, 2011 at 14:13:48
If you run the script from any of the directories under the album name it too will be renamed. The script as posted may run from any location, I have only run it from the root of C:

The following script will run in the GUI if copied to "The Album Name" folder but as stated the script filename will also be renamed.

I didn't think these small scripts warranted lots of REMming, simply entering the command followed by /? at the command prompt displays fairly extensive information about the purpose and switches.

Again not tested in Windows Vista , caveat emptor!

@echo off
cls
setlocal enabledelayedexpansion

pushd %cd%

for /f "tokens=*" %%A in ('dir /a-d /s /b') do (
    set filename=%%A

    for /f "tokens=1-4 delims=\" %%1 in ("!filename!") do (
    set albumin=%%2&set disc=%%3&set file=%%4
    ren "!filename!" "!albumin! !disc! !file!"
  )
)


Please come back & tell us if your problem is resolved.


Report •

#5
December 31, 2011 at 02:43:32
THANK YOU

ok we basically there TOP WORK

i start with a test setup as follows

downloads\test\
downloads\test\disc1\
downloads\test\disc2\
downloads\test\disc3\
downloads\test\RenameFileTest.bat
downloads\test\disc1\track1.mp3
downloads\test\disc2\track1.mp3
downloads\test\disc3\track1.mp3


i run your code as "RenameFileTest.bat" placed in downloads\test

this produces renamed files thus

downloads\test\
downloads\test\disc1\
downloads\test\disc2\
downloads\test\disc3\
downloads\test\downloads test RenameFileTest.bat
downloads\test\disc1\downloads test disc1
downloads\test\disc2\downloads test disc2
downloads\test\disc2\downloads test disc3

the file name in the run directory is kept but the files
in the disc1 disc2 and disc3 folder are renamed but the
original filename is not added and nor is the file extension retained. also it is adding the directory name above (downloads) which is not needed.

from original test set the end result i am after is

downloads\test\
downloads\test\test disc1\
downloads\test\test disc2\
downloads\test\test disc3\
downloads\test\test RenameFileTest.bat
downloads\test\test disc1\test disc1 track1.mp3
downloads\test\test disc2\test disc2 track1.mp3
downloads\test\test disc3\test disc3 track1.mp3

basically you code achieves this but needs to retain the original filename prefixing it.

and the first folder needs to be the one the file is run in not the one above.

the only addition (or request) is to rename the subfolders with the parent folder name

test\disc1\ = test\test disc1\

many thanks for time and effort you have put in so far

it is greatly appreciated


Report •

#6
December 31, 2011 at 13:16:34
Yep, we're mucking each other around.

In the first script I posted you will note that the user is required to enter the path in the format D:\Album Name. In your first post you state "example i have folder called "music album" it has 2 sub folders called "disc 1 " and "disc2"

with 20 files in each one ...

i would like to rename all the files prefixing thier file name with "music album" being name of top folder .. As you said that Music Album is the name of the "top folder" I made the wrong assumption that the full path to the files could be D:\Music Album\diskn\filename.ext

In my script the second For loop parses the full path splitting it into 4 tokens from which 3 are used in the renaming of files. Now comes the anomaly... The test you have set up has one additional subfolder, here's a comparison:

Your path could be D:\Downloads\test\diskn\filename.ext

Whereas the path set up by me from the info given by you is:
D:\Album Name\diskn\filename.ext

See the difference? Using the backslash as a delimiter your full path produces 5 tokens, mine just 4.

More work required, will get back to you after the New Year hols.

Happy New year.


Please come back & tell us if your problem is resolved.


Report •

#7
January 2, 2012 at 19:47:50
✔ Best Answer
Returning as promised. The following is based on the test setup details provided by you except that you did not state which disk/partition you had installed the files on. I have assumed (why do I do that?) that it is the default, in my case C:\

The first script is the one I have used to set up the files. It should be copied/pasted into the default under the Command Prompt. Note that directories will be removed when this script is run, if there are directories in Downloads\test\ which should not be removed please take action to protect them.

Script 1.

@echo off
cls
setlocal enabledelayedexpansion

if exist Downloads\test\disc1\ rd /q /s Downloads\test\
if exist Downloads\test\disc2\ rd /q /s Downloads\test\

for /l %%1 in (1,1,2) do (
    md Downloads\Test\Disc%%1\
)

pushd Downloads\Test\Disc1\
call :crefiles
popd
pushd Downloads\Test\Disc2\
call :crefiles
popd

dir Downloads\test\disc1\
dir Downloads\test\disc2\
exit /b

:crefiles
for /l %%1 in (1,1,20) do (
    if %%1 lss 10 (set fileno=0%%1)
    if %%1 gtr 9 (set fileno=%%1)   

    echo ryryryry>"track!fileno!.mp3"
    )


The second script is the one which does the renaming. It should be copied/pasted in Downloads\test\ and run from the GUI, running it in the Command Prompt will generate many errors.

Script 2.

@echo off
cls
setlocal enabledelayedexpansion

pushd %cd%

for /f "tokens=*" %%A in ('dir /a-d /s /b') do (
    set filename=%%A

    for /f "tokens=1-3* delims=\" %%1 in ("!filename!") do (
    set albumin=%%2&set file=%%4
    set file=!file:\= !
    ren "!filename!" "!albumin! !file!"
  )
)

exit /b


Please come back & tell us if your problem is resolved.


Report •

#8
January 5, 2012 at 06:24:07
************** THANK YOU ************

this seems to work perfectly on initial testing

may change first bit of script to make it more flexible on location but works

thank you very much for the sticking with it and forgiving me my early lack of proper explanation of requirements.

would like to say earlier efforts not wasted entirely as has pointed me in direction of learning some more than i thought i knew, hopefully negating some of my need
to come and ask.

i will also add credit to you in compiled file and point to site, as i think i may pass it on to some friends (free of course) who too may find it very useful.

again thank you

reagrds

.....


Report •

Ask Question