Solved

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

Posted on 2011-03-14
13
1,324 Views
Last Modified: 2012-05-11
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"
  )
)
 
0
Comment
Question by:wayneray
  • 7
  • 3
  • 3
13 Comments
 
LVL 2

Expert Comment

by:swadhinray
ID: 35135636
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
 
LVL 2

Expert Comment

by:swadhinray
ID: 35135654
0
 

Author Comment

by:wayneray
ID: 35137252
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Expert Comment

by:swadhinray
ID: 35138066
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
 
LVL 84

Accepted Solution

by:
oBdA earned 350 total points
ID: 35138966
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
 

Author Comment

by:wayneray
ID: 35141174
There will be no more than two files in each folder.
0
 

Author Comment

by:wayneray
ID: 35142672
I tried the script oBdA and nothing happened. I even got brave and removed the ECHO statement and ran it.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 35142685
- 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
 

Author Comment

by:wayneray
ID: 35142731
I have increased the points to this question.
0
 

Author Comment

by:wayneray
ID: 35142760
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
 
LVL 84

Expert Comment

by:oBdA
ID: 35142854
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
 

Author Comment

by:wayneray
ID: 35143243
I apoligize oBdA. I had a typo in my Folderroot variable. The script works perfectly. Thanks.
0
 

Author Closing Comment

by:wayneray
ID: 35143245
Superbly done!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
push same scheduled task across multiple servers? 16 200
Backup solution 14 79
hosts.ini- can it be made to block all websites? 14 81
avoiding the dashes in the sqlcmd output... 1 587
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question