Add folder name to beginning of filename












6















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.










share|improve this question




















  • 1





    Are you restricted to doing this with cmd.exe? This would be a LOT easier (trivial, actually) with a Unix shell.

    – Nicole Hamilton
    Dec 7 '12 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 '12 at 7:09













  • @NicoleHamilton - it is actually quite trivial in Windows batch as well.

    – dbenham
    Dec 7 '12 at 13:24








  • 3





    @dbenham Your idea of trivial and my idea of trivial are quite different.

    – Nicole Hamilton
    Dec 7 '12 at 15:34
















6















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.










share|improve this question




















  • 1





    Are you restricted to doing this with cmd.exe? This would be a LOT easier (trivial, actually) with a Unix shell.

    – Nicole Hamilton
    Dec 7 '12 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 '12 at 7:09













  • @NicoleHamilton - it is actually quite trivial in Windows batch as well.

    – dbenham
    Dec 7 '12 at 13:24








  • 3





    @dbenham Your idea of trivial and my idea of trivial are quite different.

    – Nicole Hamilton
    Dec 7 '12 at 15:34














6












6








6


5






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.










share|improve this question
















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.







windows command-line batch rename batch-rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 30 '15 at 19:41









DavidPostill

106k26228263




106k26228263










asked Dec 7 '12 at 6:19









shekharshekhar

701312




701312








  • 1





    Are you restricted to doing this with cmd.exe? This would be a LOT easier (trivial, actually) with a Unix shell.

    – Nicole Hamilton
    Dec 7 '12 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 '12 at 7:09













  • @NicoleHamilton - it is actually quite trivial in Windows batch as well.

    – dbenham
    Dec 7 '12 at 13:24








  • 3





    @dbenham Your idea of trivial and my idea of trivial are quite different.

    – Nicole Hamilton
    Dec 7 '12 at 15:34














  • 1





    Are you restricted to doing this with cmd.exe? This would be a LOT easier (trivial, actually) with a Unix shell.

    – Nicole Hamilton
    Dec 7 '12 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 '12 at 7:09













  • @NicoleHamilton - it is actually quite trivial in Windows batch as well.

    – dbenham
    Dec 7 '12 at 13:24








  • 3





    @dbenham Your idea of trivial and my idea of trivial are quite different.

    – Nicole Hamilton
    Dec 7 '12 at 15:34








1




1





Are you restricted to doing this with cmd.exe? This would be a LOT easier (trivial, actually) with a Unix shell.

– Nicole Hamilton
Dec 7 '12 at 6:57





Are you restricted to doing this with cmd.exe? This would be a LOT easier (trivial, actually) with a Unix shell.

– Nicole Hamilton
Dec 7 '12 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 '12 at 7:09







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 '12 at 7:09















@NicoleHamilton - it is actually quite trivial in Windows batch as well.

– dbenham
Dec 7 '12 at 13:24







@NicoleHamilton - it is actually quite trivial in Windows batch as well.

– dbenham
Dec 7 '12 at 13:24






3




3





@dbenham Your idea of trivial and my idea of trivial are quite different.

– Nicole Hamilton
Dec 7 '12 at 15:34





@dbenham Your idea of trivial and my idea of trivial are quite different.

– Nicole Hamilton
Dec 7 '12 at 15:34










4 Answers
4






active

oldest

votes


















5














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






share|improve this answer
























  • legend. nice program

    – user1438082
    Jun 13 '17 at 20:20



















2














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.






share|improve this answer

































    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





    share|improve this answer































      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:folder1Sub1



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


      D:folder1Sub2



                0 AAAAA.txt
      0 CCCC.txt


      Here is the script:



      PS C:UsersUser> 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.






      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "3"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f516082%2fadd-folder-name-to-beginning-of-filename%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        5














        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






        share|improve this answer
























        • legend. nice program

          – user1438082
          Jun 13 '17 at 20:20
















        5














        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






        share|improve this answer
























        • legend. nice program

          – user1438082
          Jun 13 '17 at 20:20














        5












        5








        5







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 4 '14 at 20:40









        dezlovdezlov

        39029




        39029













        • legend. nice program

          – user1438082
          Jun 13 '17 at 20:20



















        • legend. nice program

          – user1438082
          Jun 13 '17 at 20:20

















        legend. nice program

        – user1438082
        Jun 13 '17 at 20:20





        legend. nice program

        – user1438082
        Jun 13 '17 at 20:20













        2














        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.






        share|improve this answer






























          2














          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.






          share|improve this answer




























            2












            2








            2







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 7 '15 at 2:22

























            answered Dec 7 '12 at 13:22









            dbenhamdbenham

            7,84142030




            7,84142030























                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





                share|improve this answer




























                  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





                  share|improve this answer


























                    1












                    1








                    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





                    share|improve this answer













                    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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 24 '12 at 9:45









                    shekharshekhar

                    701312




                    701312























                        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:folder1Sub1



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


                        D:folder1Sub2



                                  0 AAAAA.txt
                        0 CCCC.txt


                        Here is the script:



                        PS C:UsersUser> 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.






                        share|improve this answer




























                          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:folder1Sub1



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


                          D:folder1Sub2



                                    0 AAAAA.txt
                          0 CCCC.txt


                          Here is the script:



                          PS C:UsersUser> 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.






                          share|improve this answer


























                            1












                            1








                            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:folder1Sub1



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


                            D:folder1Sub2



                                      0 AAAAA.txt
                            0 CCCC.txt


                            Here is the script:



                            PS C:UsersUser> 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.






                            share|improve this answer













                            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:folder1Sub1



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


                            D:folder1Sub2



                                      0 AAAAA.txt
                            0 CCCC.txt


                            Here is the script:



                            PS C:UsersUser> 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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 14 '18 at 8:33









                            RichardRichard

                            111




                            111






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Super User!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f516082%2fadd-folder-name-to-beginning-of-filename%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Plaza Victoria

                                Puebla de Zaragoza

                                Musa