?
Solved

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

Posted on 2011-03-14
13
Medium Priority
?
1,366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 85

Accepted Solution

by:
oBdA earned 1400 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 85

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 85

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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