9

I have a directory structure as below:

Folder
  > SubFolder1
    > FileName1.abc
    > Filename2.abc
    > .............

  > SubFolder2
    > FileName11.abc
    > Filename12.abc
    > ..............

  > ..........

etc. I want to rename the files inside the subfolders as:

SubFolder1_Filename1.abc
SubFolder1_Filename2.abc
SubFolder2_Filename11.abc
SubFolder2_Filename12.abc

i.e. add the folder name at the beginning of the file name with the delimiter "_". The directory structure should remain unchanged. Note: Beginning of file name is same. e.g. in above case File*.

I made below Script


for /r "PATH" %%G in (.) do (
  pushd %%G
  for %%* in (.) do  set MyDir=%%~n* 
  FOR %%v IN (File*.*) DO REN %%v  "%MyDir%_%%v" 
  popd
  ) 

Problem with the above script is that it is taking only one Subfolder name and placing it to the beginning of file name irrespective of the folder.

4
  • 1
    Are you restricted to doing this with cmd.exe? This would be a LOT easier (trivial, actually) with a Unix shell. Dec 7, 2012 at 6:57
  • Yes I do want to do it in cmd only as I am on Windows. I know, by installing bash tools i can do it more easily in unix. But I was just curious to get it done in cmd. and want to use the built in features of windows effectively. Moreover I don't have permission to install any third party tool on the machine I am working on.
    – shekhar
    Dec 7, 2012 at 7:09
  • @NicoleHamilton - it is actually quite trivial in Windows batch as well.
    – dbenham
    Dec 7, 2012 at 13:24
  • 3
    @dbenham Your idea of trivial and my idea of trivial are quite different. Dec 7, 2012 at 15:34

5 Answers 5

7

You can do this in a more user friendly way using ReNamer, with a single renaming rule:

  1. Insert ":File_FolderName:_" as Prefix (skip extension)

You can also save it as a Preset and use it for command line renaming.

enter image description here

1
  • legend. nice program Jun 13, 2017 at 20:20
5

To rename only files in the immediate child folders

@echo off
pushd "Folder"
for /d %%D in (*) do (
  for %%F in ("%%~D\*") do (
    for %%P in ("%%F\..") do (
      ren "%%F" "%%~nxP_%%~nxF"
    )
  )
)
popd

To recursively rename all files in child folders

@echo off
pushd "Folder"
for /d %%D in (*) do (
  pushd "%%D"
  for /r %%F in (*) do (
    for %%P in ("%%F\..") do (
      ren "%%F" "%%~nxP_%%~nxF"
    )
  )
  popd
)
popd

Make sure you only run either script once! You don't want to put multiple prefixes in front of the files :-)

Additional code could be added to make it safe to run multiple times.

1

If you want to rename files inside subfolder only this is the solution.

for %%f in (.) do set "A=%%~dpnxf\"
for /r "%A%" %%f in (.) do call :func "%%~f"
goto :EOF
:func
set "B=%~1"
for %%g in ("%B%") do set "C=%%~dpnxg"
for %%g in ("%C%") do set "D=%%~nxg"
cd  %C%
set "k=%C%\"
if NOT %A%==%k% FOR %%v IN (*.*) DO REN "%%v" "%D%_%%v" 
goto :EOF
1

You could do it easily by using Windows Powershell. That's a two-line script to rename all files in subfolders the way the file name gets a subfolder name prefix. Consider this simple structure in Drive D:

D:\folder1\Sub1

         Sub1 - AAAA.txt
          Sub1 - BBBB.txt
           Sub1 - CCC.txt

D:\folder1\Sub2

          0 AAAAA.txt
          0 CCCC.txt

Here is the script:

PS C:\Users\User> cd D:\folder1

PS D:\folder1> get-childitem -recurse | Rename-Item -NewName {$.Directory.Name + " - " + $.Name}

By running the script all files will be renamed with directory name prefix.

1
  • Rename-Item : The input to the script block for parameter 'NewName' failed.The term '$.Directory.Name' is not recognized as the name of a cmdlet, function, script file, or operable program.
    – Arnab
    Jul 25, 2020 at 14:11
0

I use Bulk Rename Utility (TGRMN Software) which has a lot of options for renaming ; including appending folder name to file name as a prefix or a sufix , including choosing what separator you want between them, and down to what level of subfolder you want to append

Bulk Rename Utility

5
  • 1
    Could you compare this to the earlier answers? Mar 19, 2021 at 23:03
  • The first answer with 5 votes is also a third party based solution, so why not
    – fjohn
    Mar 21, 2021 at 9:47
  • How is your suggested solution better than the other answers? Mar 22, 2021 at 0:06
  • I did not specified anywhere that it is better. It is a similar solution
    – fjohn
    Mar 22, 2021 at 23:20
  • Can you differentiate your solution from the others? Help the reader? Mar 23, 2021 at 2:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .