Solved

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

Posted on 2011-03-14
13
1,299 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
 
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 83

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 83

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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now