Need a Batch File to Rename Files in Subfolders to Subfolders Name
Posted on 2011-03-14
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?
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"