Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Moving many files up one directory level  (Read 34273 times)

0 Members and 1 Guest are viewing this topic.

cameronyoung

    Topic Starter


    Rookie

    Moving many files up one directory level
    « on: January 11, 2010, 06:24:46 PM »
    Hi,

    I have about 400 folders with the following directory depth:
    Level1\Level2\Level3\*.files

    I need to move all of the *.files up one directory so that they are:
    Level1\Level2\*.files

    It would also be extremely helpful if I could limit the *.files to specific types of files, namely *.MP3 and then if I could delete the "Level3" directories and any remaining junk files they contain.

    I'm sure this solution is possible in with a BAT file?

    Thank you.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Moving many files up one directory level
    « Reply #1 on: January 11, 2010, 07:00:05 PM »
    I would not recommend a bat file.
    Do you have a backup device?
    Do you have lots of free space on your disk?
    Rather that move, you should copy.

    But if you insist. You are gong to skip over one on folder, the top one.
    Level1\Level2\Level3\*.files

    The Level1 folder will not be moved.

    We shall move the contents of the next folder down to a new, higher location. Later you can rename the folders, if you wish.


    Make a new folder. call it Level2New.
    This is going to be the target folder. Make it at the highest level.

    In windows explore open the  the Level2 fonder.
    Now look up on the tool bar. See the 'Edit' tab?
    Click on Edit,  Select all.
    Then Click on Move to folder.
    Select the Level2New as the target.
     If you do it right the contents of Level2 and everything below will be in Level2New.
    Level1 will still have its contents.
    Practice on some dummy folders first.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Moving many files up one directory level
    « Reply #2 on: January 11, 2010, 07:21:32 PM »
    the folders aren't being moved... the files are.



    Code: [Select]
    @echo off
    for /f %%P in ('dir /b /od') do copy %%P\*.* .
    REM for /f %%P in (dir /b /od') do rmdir /s /q %%P

    Run this batch file from within the "level2" folder. what it does is copy all files from the folders within level2 into the level2 folder itself. (as I understand your issue, you have many folders within this level2 folder, and wish to copy the files from each of these folders to the parent folder.

    Note that I am using the "copy" command, at this point. the loop that removes the directories is commented out. I recommend making a complete backup, if at all possible, before deleting them. Or, you could change the command to move. a Move is faster then a copy and delete on the same drive. Also, you can easily change the filemask by changing the %%P\*.* to %%P\*.mp3, for example.

    also, remember that if any of these folders has filenames that are the same as files in any other folder, you should rename them. the copy command as written will prompt you to overwrite if any names conflict.

    I was trying to dereference Null Pointers before it was cool.

    BillRichardson



      Intermediate

      Thanked: 15
      Re: Moving many files up one directory level
      « Reply #3 on: January 11, 2010, 07:28:18 PM »
      Code: [Select]
      @echo off
      cd "C:\Documents and Settings\cameronyoung\Level1\Level2\Level3\"

      copy *.mp3  "C:\Documents and Settings\cameronyoung\Level1\Level2\"

      delete *.*
       
      Bill Richardson

      cameronyoung

        Topic Starter


        Rookie

        Re: Moving many files up one directory level
        « Reply #4 on: January 11, 2010, 07:41:06 PM »
        Hi, I don't think I explained myself as clearly as I could have. I really need something that will loop through a number of folders, a better explanation of my 400 odd folders would be like this:

        Level1\1Level2\Level3\*.files
        Level1\2Level2\Level3\*.files
        Level1\3Level2\Level3\*.files
        Level1\4Level2\Level3\*.files
        Level1\5Level2\Level3\*.files
        Level1\6Level2\Level3\*.files

        What I want to do is to remove all of the "Level 3" folders and move the contents (well, just .MP3 files if possible) back to the "Level 2" folders.  It's easy to do it for one folder, but I've got about 400 folders at "Level 2" which each contain an extra, unwanted folder.

        Even if the script or whatever doesn't delete the "Level 3" folders in the end, that's not a big deal, as I have a program that could remove them.. though it would be preferable.

        Thanks for your help.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Moving many files up one directory level
        « Reply #5 on: January 11, 2010, 07:50:24 PM »
        Ahh, OK, guess I misunderstood :)

        Code: [Select]
        @echo off
        for /f %%P in ('dir /b /od') do copy %%P\level3\*.*  .\%%P\
        REM for /f %%P in (dir /b /od') do rmdir /s /q %%P\level3

        Run this from within your "level1" folder. it will do something similar to the previous batch I provided; this one loops through all the folders ('dir /b /od') and copies the files from within the level3 folder of that folder "out" of the folder into the "level2" folder that it's looping through.

        Again, the remarked out rmdir command will remove the level3 folders, as well. the first loop only copies. and change the "%%P\level3\*.*" to "%%P\level3\*.mp3" to only copy mp3 files. (as I'm sure your aware deleting the folder will result in the loss of other files that are not MP3 from within that folder.
        I was trying to dereference Null Pointers before it was cool.

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Moving many files up one directory level
        « Reply #6 on: January 11, 2010, 08:42:42 PM »
        Now that we have all confused him,
        If his file structure is in good shape ...
        and if explorer is not bite by bugs.

        He can do it in one drop and drag.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Moving many files up one directory level
        « Reply #7 on: January 11, 2010, 09:44:39 PM »
        Now that we have all confused him,
        If his file structure is in good shape ...
        and if explorer is not bite by bugs.

        He can do it in one drop and drag.


        No. he can't. you might want to read a bit closer.

        He wants to move the files in all the DIFFERENT level3 folders beneath the level 2 folders into the respective level 2 folders. THIS CANNOT be done quickly with explorer.
        I was trying to dereference Null Pointers before it was cool.

        cameronyoung

          Topic Starter


          Rookie

          Re: Moving many files up one directory level
          « Reply #8 on: January 12, 2010, 12:10:39 AM »
          Ahh, OK, guess I misunderstood :)

          Code: [Select]
          @echo off
          for /f %%P in ('dir /b /od') do copy %%P\level3\*.*  .\%%P\
          REM for /f %%P in (dir /b /od') do rmdir /s /q %%P\level3

          Run this from within your "level1" folder. it will do something similar to the previous batch I provided; this one loops through all the folders ('dir /b /od') and copies the files from within the level3 folder of that folder "out" of the folder into the "level2" folder that it's looping through.

          Again, the remarked out rmdir command will remove the level3 folders, as well. the first loop only copies. and change the "%%P\level3\*.*" to "%%P\level3\*.mp3" to only copy mp3 files. (as I'm sure your aware deleting the folder will result in the loss of other files that are not MP3 from within that folder.

          Hey, I am pretty sure this will work, except I may not have given you all the required info.

          "Level 1" is always called "music" but the "Level 2" and "Level 3" directories always have different names. In that BAT file you gave me I noticed that you you have a couple of commands which point specifically to folders called "Level 3": is there a way to just point to directories at this level in the tree rather than folders actually named "Level 3"?

          Thanks so much!

          Salmon Trout

          • Guest
          Re: Moving many files up one directory level
          « Reply #9 on: January 12, 2010, 12:16:29 AM »
          Code: [Select]
          @echo off
          cd "C:\Documents and Settings\cameronyoung\Level1\Level2\Level3\"

          copy *.mp3  "C:\Documents and Settings\cameronyoung\Level1\Level2\"

          delete *.*
           


           ::)

          cameronyoung

            Topic Starter


            Rookie

            Re: Moving many files up one directory level
            « Reply #10 on: January 12, 2010, 12:18:14 AM »
            ::)
            Sorry you haven't read my post properly. BC_Programmer could you please check my reply? Thanks heaps.

            Salmon Trout

            • Guest
            Re: Moving many files up one directory level
            « Reply #11 on: January 12, 2010, 12:33:28 AM »
            Sorry you haven't read my post properly. BC_Programmer could you please check my reply? Thanks heaps.

            I have, somebody else hasn't.

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Moving many files up one directory level
            « Reply #12 on: January 12, 2010, 01:57:14 AM »
            Original code:

            Code: [Select]
            @echo off
            for /f %%P in ('dir /b /od') do (
                pushd %%P
                for /f %%I in ('dir /b /od') do (
                    copy "%%P\%%I\*.*"  ".\%%P\"
                    REM rmdir /s /q "%%P\%%I"
                )
                popd

            )


            new code:

            Code: [Select]
            SETLOCAL ENABLEDELAYEDEXPANSION
            for /f "usebackq delims=" %%P in (`dir /b /ad`) do (
                pushd %%P
                for /f "usebackq delims=" %%I in (`dir /b /ad`) do (
                     copy "%%I\*.*"  .\
                     REM rd /s /q "%%I"
                )
                popd

            )

            changed the dir command to what I originally intended; I had /od, but /o and -o are for sorting. changed it to /ad so they would both only enumerate directories.

            Given the new requirements, added a nested loop within the original, to account for "level3" folders having different names. In this case, it iterates on ALL folders within each folder; Again, I have the rd command remmed out here; unrem it after you test it for it to delete the folders as you wanted. It worked for my tests (it moved all data in third level folders to their respective second level folders) however, with multiple folders we have the problem of duplicate filenames in these folders; copy will prompt to overwrite in this scenario.
            I was trying to dereference Null Pointers before it was cool.

            cameronyoung

              Topic Starter


              Rookie

              Re: Moving many files up one directory level
              « Reply #13 on: January 28, 2010, 08:19:43 PM »
              Hey BC_Programmer,

              I added the below code to a .BAT file but it didn't work.

              Code: [Select]
              SETLOCAL ENABLEDELAYEDEXPANSION
              for /f "usebackq delims=" %%P in (`dir /b /ad`) do (
                  pushd %%P
                  for /f "usebackq delims=" %%I in (`dir /b /ad`) do (
                       copy "%%I\*.*"  .\
                       REM rd /s /q "%%I"
                  )
                  popd

              )

              When I added the code as is, it didn't move any files or delete anything.  When I removed the REM it just deleted everything except the parent directories.

              Am I doing something wrong?  I ran the .BAT file from "S:\MUSIC\test.bat" and these are the exact files I had in the directory:

              S:\MUSIC\dalek vs faust\2004 - derbe respect, alder\dalek vs faust v0\01 jhevevevj.mp3
              S:\MUSIC\dalek vs faust\2004 - derbe respect, alder\dalek vs faust v0\02 83fjh3v3v.mp3
              S:\MUSIC\dalek vs faust\2004 - derbe respect, alder\dalek vs faust v0\03 jeje vv urvfrv.mp3
              S:\MUSIC\dalek vs faust\2004 - derbe respect, alder\dalek vs faust v0\folder.jpg
              S:\MUSIC\mazzy star\1991 - she hangs brightly\she hangs brightly\01 whh whh whaaamp3
              S:\MUSIC\mazzy star\1991 - she hangs brightly\she hangs brightly\02 bleeef dikee aa.mp3
              S:\MUSIC\mazzy star\1991 - she hangs brightly\she hangs brightly\03 !.mp3
              S:\MUSIC\mazzy star\1991 - she hangs brightly\she hangs brightly\playlist.m3u

              I had hoped that it would produce the following:

              S:\MUSIC\dalek vs faust\2004 - derbe respect, alder\01 jhevevevj.mp3
              S:\MUSIC\dalek vs faust\2004 - derbe respect, alder\02 83fjh3v3v.mp3
              S:\MUSIC\dalek vs faust\2004 - derbe respect, alder\03 jeje vv urvfrv.mp3
              S:\MUSIC\mazzy star\1991 - she hangs brightly\01 whh whh whaaamp3
              S:\MUSIC\mazzy star\1991 - she hangs brightly\02 bleeef dikee aa.mp3
              S:\MUSIC\mazzy star\1991 - she hangs brightly\03 !.mp3

              Ideally I would have liked it to move only the .MP3 files and then for it to remove the directories the MP3s were moved from and any non-MP3 files they contain.

              It needs to be something generic though, as there are about 2000 more "dalek vs faust" and "mazzy star" level directories with the exact same structure.

              Please let me know if this isn't clear.  Thank you.

              Salmon Trout

              • Guest
              Re: Moving many files up one directory level
              « Reply #14 on: January 29, 2010, 12:16:28 AM »
              Sorry to jump in, but shouldn't this

              copy "%%I\*.*"  .\

              be this

              copy "%%I\*.*"  ..\

              ?

              (i.e. two dots refer to the next folder up)