Link to home
Start Free TrialLog in
Avatar of wayneray
waynerayFlag for United States of America

asked on

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"
  )
)
 
Avatar of swadhinray
swadhinray
Flag of India image

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 :

https://www.experts-exchange.com/questions/23468926/Need-batch-script-to-move-and-rename-file-with-datestamp.html
Avatar of wayneray

ASKER

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


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 ?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There will be no more than two files in each folder.
I tried the script oBdA and nothing happened. I even got brave and removed the ECHO statement and ran it.
Avatar of oBdA
oBdA

- 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.
I have increased the points to this question.
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.
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

I apoligize oBdA. I had a typo in my Folderroot variable. The script works perfectly. Thanks.
Superbly done!