Need a Batch File to Rename Files in Subfolders to Subfolders Name

I am trying to create a batch file that will rename all files in a group of subfolders to the subfolder name. For example, Folder c:\test has folders c:\test\sub1, c:\test\sub2, c:\test\sub3 and so forth. I want the files in sub 1 to be named "sub1" and the files in sub2 to be named "sub2". If there is more than one file, then a number is appended to the file name.

Each subfolder may have two or more files and the folder have spaces in the folder name. I tried using a batch file created by another expert but I am having trouble understanding the code in order to adjust the code. The batch file below does rename files in the directory but I need to change file name in a group of subfolders in the target directory. Is this possible?

@echo off
set Target=C:\test
for /f "Tokens=*" %%A in ('dir /b /s /ad "%Target%"') do (
  for /f "Tokens=*" %%B in ('dir /a-d /b "%%A" 2^>NUL') do (
     ren "%%A\%%B" "%%~nA %%~nxB"
  )
)
 
waynerayAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
oBdAConnect With a Mentor Commented:
Try the script below. It's currently in test mode and will only display the "rename" commands it would normally run, but not do anything for real.
Remove the capitalized ECHO in line 7 to run it for real.
@echo off
setlocal enabledelayedexpansion
set FolderRoot=C:\Test
for /d %%a in ("%FolderRoot%\*.*") do (
  set i=
  for %%f in ("%%a\*.*") do (
    ECHO ren "%%f" "%%~nxa!i!%%~xf"
    set /a i += 1
  )
)

Open in new window

0
 
swadhinrayCommented:
You can do this with macro expert.
For more details on macro expert go to their home page and download it :

http://www.macro-expert.com

Also check a similar question on move and renaming file :

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_23468926.html
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
waynerayAuthor Commented:
Thanks for your suggestions swadhinray. However, I want a batch, vbs or macro that will simply change the name of a file to the folder name for several files (16 to 20 files exisiting in 16 to 20 subfolders). I have vba macros for most of my tasks but I thought a batch file or vbs file would be helpful in this case. There are good batch files that rename files but none of them rename files outside the root folder. Here is another example.

c:\Test\folder 1\file 1.txt
c:\Test\folder 2\file 2.txt
c:\Test\folder 3\file 3.txt

Change to this.

c:\Test\folder 1\folder 1.txt
c:\Test\folder 2\folder 2.txt
c:\Test\folder 3\folder 3.txt


0
 
swadhinrayCommented:
Got it ....

But how many files will be there under one sub folder like :

If folder name is "TEST" and sub folder name is "folder 1"  and then how many files will be there under this sub folder ?
0
 
waynerayAuthor Commented:
There will be no more than two files in each folder.
0
 
waynerayAuthor Commented:
I tried the script oBdA and nothing happened. I even got brave and removed the ECHO statement and ran it.
0
 
oBdACommented:
- You have to adjust the "FolderRoot" variable to point to a folder that contains a (test) structure like the one you described.
- Run the script from a command prompt, not with double-click from Explorer.
0
 
waynerayAuthor Commented:
I have increased the points to this question.
0
 
waynerayAuthor Commented:
I am running from a command prompt and I did change the variable "FolderRoot" to match the location of the batch file I am running and where the subfolders are. The files that I need to change the names to are Microsoft Word documents. Some are docx and some or doc files.
0
 
oBdACommented:
Just in case: do not change anything else except setting the "FolderRoot" variable to the correct value.
Use notepad.exe to paste from the clipboard and save; not all editors handle copied code correctly.
Make sure you have the complete script copied and pasted, including the last closing bracket on line 10.
I just recopied the code from this site, created two folders sub1 and sub2 under c:\test with two test files in each, and ran the script.
It generated this output:
ren "C:\Test\sub1\moretest.docx" "sub1.docx"
ren "C:\Test\sub1\test.docx" "sub11.docx"
ren "C:\Test\sub2\andevenmoretest.docx" "sub2.docx"
ren "C:\Test\sub2\evenmoretest.docx" "sub21.docx"

Open in new window

0
 
waynerayAuthor Commented:
I apoligize oBdA. I had a typo in my Folderroot variable. The script works perfectly. Thanks.
0
 
waynerayAuthor Commented:
Superbly done!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.