Renaming files using name of subdirectories

Hewlett-packard / Iq505
August 24, 2009 at 09:05:55
Specs: Windows Vista , Dual Core/4GB
Hello. I have been trying for a few days to get a batch script to work with no luck. I have several subdirectories in a folder called "test box" that is on my desktop. Inside of these subdirectories are several files with various names. For example, Red.txt, 001.txt, memo.avi, etc. I want to rename these files by adding the name of the subdirectory at the end of the name of the file with a hypen seperating the two names. For example ~\Desktop\test box\Status\Red.txt would be renamed to ~\Desktop\test box\Status\Status-Red.txt. Here are just a few of the things I have tried.

::Batch File to Prepend filenames of files in subdirectories with names of subdirectories followed by underscore.
@echo off & setLocal EnableDelayedExpansion

cd /d "C:\Users\I PUT MY USER NAME HERE\Desktop\Test"

for /f "tokens=* delims= " %%a in ('dir/s/b/ad') do (pushd %%a call :sub1
for /f "tokens=* delims= " %%i in ('dir/b/a-d') do (echo ren %%i !FN!_%%i)

goto :eof

:sub1
set FN=!CD!
:loop
for /f "tokens=1* delims=\" %%f in ("!FN!") do (if "%%g" neq "" (set FN=%%g goto :loop)
goto :eof
::End of first Batch file

::Second Batch file effort to name files using subdirectory names
for /D %%j in (*) do (
pushd %%j
for %%k in (*) do (
if not"%%~nk"=="%%j" ren "%%k" "%%j%%~xk"

Please help! What am I doing wrong? What does the script need to look like? The scripts above are edits of scripts by IVO and M2go but they don't seem to work for this tasks. :( Thanks for your kind assistance friends. :)


See More: Renaming files using name of subdirectories

Report •


#1
August 24, 2009 at 22:00:03
See if something like this will work for you, it will rename all files including files in sub directories of the initial directory you specify in the manner that you want.

@echo off
for /f "tokens=*" %%A in ('dir /a:-d /b /s "c:\test1\*.*"') do (
pushd %%~dA%%~pA
for %%b in (.) do (
ren "%%A" "%%~nb-%%~NA%%~XA"
)
popd)


Report •

#2
August 25, 2009 at 09:33:48
Oh my... I was just about to give up and go weep in my abandonment corner and then you gave a reply! Thanks gtaion! I will try your recipe right away and post back.

Report •

#3
August 25, 2009 at 09:51:54
Oh happy happy joy joy! Thank you gtaion! It is working like a charm. :) Now if only there was a reverse process... I think I would be in programming heaven with this. Is there a way to undo the rename process in case a user makes a mistake...? By the way... I can't really understand why your script works and the last one that I tried didn't? They both seem to use similar methods...

Report •

Related Solutions

#4
August 25, 2009 at 10:13:32
Oh Jumping Jalopychinks! I got this script to work as a suffix adding tool by rearranging the script a little bit! I'm soo exited and I just can't hide it! Woo hoo! Here is the new code for all of you interested script munchers. :)

@echo off
for /f "tokens=*" %%A in ('dir /a:-d /b /s "C:\Users\PUT YOUR USER NAME HERE\Desktop\PUT THE NAME OF YOUR FOLDER FOR PROCESSING HERE\*.*"') do (
pushd %%~dA%%~pA
for %%b in (.) do (
ren "%%A" "%%~NA-%%~nb%%~XA"
)
popd)

Thank you for the inspiration gtaion!

Now if only someone would stumble by who knows how to undo the whole renaming process, I will sing Sraccappella on American Idol next week for sure!


Report •

#5
August 25, 2009 at 10:20:16
The easier way to get to a user's desktop would be with enviroment variable's something like

"%userprofile%\desktop" Then no matter who was logged on it would go to there desktop.

Now crazy question but what you want to do know is remove the dir from the name so, dir_name.txt becomes name.txt again?


Report •

#6
August 25, 2009 at 10:34:14
Hi again gtaion. Thanks for the environment value reminder. That is exactly what I am looking for. I want to know if I can undo the rename process to strip off the directory name and the hyphen just like you said in your last post. Is that possible? And is it possible for my altered version of your script as well?

Report •

#7
August 25, 2009 at 17:12:24
This works assuming that your directories dont include a _ character

@echo off
for /f "tokens=1,2* delims=_" %%A in ('dir /a:-d /b /s "%userprofile%\desktop\YOUR FOLDER\*.*"') do (
pushd %%~dA%%~pA
ren "%%A_%%B" "%%B"
popd)


Report •

#8
August 25, 2009 at 19:43:22
Hi again gtaion. I tested the last script that you gave me to munch on and I'm sad to report that it didn't work out this time. The files are still sitting there with the suffix on their names... Wait a minute! Does this only work for the prefix tool you gave me? Let me try again and report back.

Report •

#9
August 25, 2009 at 19:46:09
sorry gtaion... this one is not working out.

Report •

#10
August 26, 2009 at 05:34:09
You know what the problem is, I checked the batch that I gave you before and when it changes the name it adds a "-" between the directory name and original name, but when I wrote the batch to remove it, I have the "delims" looking for a "_", so of course it isn't going to find any files with the "_" character, try changing it so that it will look for all of the file after the "-" by changing the delimiter like so.

for /f "tokens=1,2* delims=-"

Then you will have to change the names of the files that you are looking for also, like this.

ren "%%A-%%B" "%%B"

If you didn't figure it out yet, I'm just using the for statement to break the file name down, it looks for the part of the name before the demlimiter and sets it as token 1 (%%A) and then the rest of the name is read into token 2 (%%B) then all it is doing is looking for the full file name which would be %%A-%%B becuase the "-" got removed becuase it was set as the seperating character and then renaming the full file name to be just the last part of the filename (%%B) if that makes sense.


Report •

#11
August 27, 2009 at 13:52:36
Hi gtaion. Thanks for the explanation. I always have the hardest time understanding the for statement. It creeps me out really. :( But I did try to run your revised version with the correct delimiter by changing the underscore ( _ ) to a hyphen ( - ). But sadly the rename operation still did not work out. :(

Report •

#12
August 27, 2009 at 16:08:36
Im at a loss, does it give you any errors at all? It runs fine for me if I make up some fake files and directories and run the initial naming one and then when I run the above it undoes it.

May i suggest maybe changing the "ren" to "echo" and then running the batch from a command prompt, atleast then you would know if it is pulling the correct filenames and trying to rename it properly, troubleshooting needs to start with the simple stuff:) If you have tons of files and it starts scolling all of the names and you want it to stop, you can stop a batch file by hitting control-c.

I'm posting the code again, I tried it by making some dummy files up on my desktop.

@echo off
for /f "tokens=1,2* delims=-" %%A in ('dir /a:-d /b /s "%userprofile%\desktop\YOUR FOLDER\*.*"') do (
pushd %%~dA%%~pA
echo "%%A-%%B" "%%B"
popd)

I tried to shrink it down to fit, but i hope you realize that this is only 5 lines, the second line should start with "for" and end with "do ("


Report •

#13
August 28, 2009 at 16:33:09
Hello again gtaion. I hope this worrisome batch script isn't annoying you now. When I save the new script and input my folder name, the cmd window simply flashes on the screen and then autoexits and the files are not touched. :(

Report •

#14
August 28, 2009 at 18:33:27
As it should, what I posted I replaced the rename command with the echo command, so really all it's doing is displaying the names, if you open a command prompt (windows logo key-r and cmd) then navigate to where the batch file is stored, i.e. (cd %userprofile%\desktop) and then run the batchfile it should show what file names it's finding and then what name it will change it to. If you just want to try to make it work again replace

echo "%%A-%%B" "%%B"

with

ren "%%A-%%B" "%%B"

But since this is not working, I would suggest trying to run it throught the command line, to see if it is popping up with any error messages, it will make it a lot easier to help you out if I know what the actual problem is.


Report •

#15
August 30, 2009 at 04:55:29
Well... here's another sad report. Sorry... The computer seems to be experiencing puberty or something... I verified that I was following your instructions to the letter. I created the batch file again and put echo instead of ren in the correct place, I deleted the cls and exit lines from the bottom of the batch file in order to prevent it from closing so I could see any errors if they occured. But the CMD window came and left so quickly that I could barely blink before it was gone. I tried it several times but it disappeared way too fast... Then I tried changing echo back to ren just to test it again... no luck.. the files are still unchanged. I am now at the point of just copying and pasting all that you post here to make sure I am not making typing errors, just to be on the safe side... but still this hasn't worked. Maybe it's not possible for some reason? I am super thankful for all of the help you have already given so if this part fails I will still be very thankful for all the rest of the help. :)

Report •

#16
August 30, 2009 at 06:33:46
Okay do this for me real quick, open the run command box (windows logo key - R) Copy and paste the following in to the run prompt, change the path to where ever you have your files. It should open a command prompt window and show you some filenames.

Just copy and past this, it should all go on one line when it pasted into the run box.

cmd /k for /f "tokens=1,2* delims=-" %A in ('dir /a:-d /b /s "%userprofile%\desktop\YOUR FOLDER\*.*"') do pushd %~dA%~pA echo "%A-%B" "%B" popd


Report •

#17
August 30, 2009 at 08:23:47
Or what if we set your folders path as a variable, that way the only thing you have to change is what the YourFolder variable is set as. So just copy and paste this and see if it works, the only thing you have to change is the YourFolder variable, other then that it is not necassary to add CLS or Exit or anything else.
So when it is saved in a .bat file it should look exactly like it does below.

@echo off

set YourFolder=%userprofile%\desktop\YOUR FOLDER

for /f "tokens=1,* delims=-" %%A in ('dir /a:-d /b /s "%YourFolder%\*.*"') do (
pushd %%~dpA
ren "%%A-%%B" "%%B"
popd)


Report •

#18
August 30, 2009 at 14:48:32
In response to post 16, the command prompt window reported
"The filename, directory name, or volume label syntax is incorrect.

In regards to post 17, the command prompt window opened when I ran the new script and it has been open ever since. Almost as though this Vista 64 Bit machine is mocking me... Am I doing something wrong? Just when I thought I was understanding the basics. :(


Report •

#19
August 30, 2009 at 15:12:02
Okay, the error that you are getting in response 16 is telling me that it's not finding what it is looking for. One of Two things.
One: You don't have any files that follow the name scheme of name-name.nam

Two: Your directories have the - character in them. i.e.
%userprofile%\desktop\your-folder\whatever\name-name.nam. I don't know why I didn't think about this before. If this is the case it would explain why it doesn't work for you. Becuase it is seeing the first - and splitting the name from there.


Report •

#20
August 30, 2009 at 16:10:12
Oh your patience has been colossal !!!!! How can I ever say thank you enough! Your last answer was right on! I had simply copied my original folder for testing purposes and of course Vista added a lovely ( - Copy) to the folder name which I did not realize was the cause of the consternation. I think I will go fall off a chair now and eat dandelions as punishment for all that I have put you through.

Report •

#21
August 30, 2009 at 17:11:15
So does that mean things are working well for you then? I hope so. I'm not sure if this is the best way to do it, but if your folders names have an - in them I was thinking that this would work for you.

@echo off

set YourFolder=%userprofile%\desktop\YOUR FOLDER

for /f "tokens=*" %%A in ('dir /a:-d /b /s "%YourFolder%\*.*"') do (
pushd %%~dpA
for /f "tokens=1,2* delims=-" %%e in ('dir /b "%%~nA.*"') do (
if exist %%e-%%f (ren "%%A" "%%f") else (
ren "%%A" "%%g")
)
popd)


What this is doing is going and grabbing the full path for each filename, then running each filename through another for command breaking it down by -'s, then it checks to see if the filename only had one - in it (if this is true the directory it is in didn't have a - in it's name) if it had more the one - it assumes that the directory had a - in the name and only grabs the very last part of the filename.

This may be a better way to do bussiness since it does some name checking.


Report •

#22
August 30, 2009 at 19:05:15
hmmm lets make something great even better shall we? hehehe. I love it! ...but sadly this new form does not work if there is a hyphen in the file name. The command window simply opens on the screen and freezes...

Report •

#23
August 30, 2009 at 19:34:56
So to recap for those that like to just scroll to the bottom without reading all of the conversation above. To rename all files including those located in sub-directories so that there new name includes the name of there current directory i.e.
CurrentDirName-ExistingFileName.Txt we used the following in a batch file

@echo off

set YourFolder=%userprofile%\desktop\YOUR FOLDER

for /f "tokens=*" %%A in ('dir /a:-d /b /s "%YourFolder%\*.*"') do (
pushd %%~dpA
for %%b in (.) do ren "%%A" "%%~nb-%%~nxA"
popd)

And then to undo the naming we used a batch file that contained the following:

@echo off

set YourFolder=%userprofile%\desktop\YOUR FOLDER

for /f "tokens=1,* delims=-" %%A in ('dir /a:-d /b /s "%YourFolder%\*.*"') do (
pushd %%~dpA
ren "%%A-%%B" "%%B"
popd)

But to add to it, if the current directory had a - in the name I suggested using the following, but it did not work for you on your system although it works fine on mine, but you are right if the filename contains a - it screws up the name

@echo off

set YourFolder=%userprofile%\desktop\YOUR FOLDER

for /f "tokens=*" %%A in ('dir /a:-d /b /s "%YourFolder%\*.*"') do (
pushd %%~dpA
for /f "tokens=1,2* delims=-" %%e in ('dir /b "%%~nA.*"') do (
if exist %%e-%%f (ren "%%A" "%%f") else (ren "%%A" "%%g")
)
popd)


Report •

#24
August 31, 2009 at 06:13:17
Right on! And thanks a million for the renamer and the undoer!

Report •


Ask Question