Search code examples
windowsbatch-filebulk

Script to delete directories but keep files


I am after a script to be able to do the following

I have about 2000 directories with files and folders inside them

I want to delete the main folder but keep everything inside the folder

So an exmaple of one folder

Folder A / Folder 2 / Video 1
           Image1
           Image2

So I want it to be able to delete the main first folder, in this case it would be 'Folder A'

Folder 2 / Video 1
Image1
Image2

Hope you can understand what I am wanting to do if not just ask! Cheers

Extra info

From this

C:/folder A/Stuff
C:/folder B/Stuff
C:/folder C/Stuff

To this

C:/stuff
C:/sfuff
C:/sfuff

I just want this bat to be able to delete the folder and move everything from within that folder to the place I ran the .bat, I also want to be able to run the .bat on other drives D:/ E;/ F;/ many locations


Solution

  • If you have this structure in your disk:

    C:\Folder A
    |  | Folder 2
    |  | | Video 1
    |  | | | Image1
    |  | | | Image2
    

    ... and you execute these commands:

    cd "\Folder A"
    move "Folder 2" ..
    

    ... then you get this result:

    C:\Folder A
    |  Folder 2
    |  | Video 1
    |  | | Image1
    |  | | Image2
    

    That is, it move "Folder 2" one level up, to the same level of "Folder A". If you want to do the same thing with all folders inside "Folder A" and then delete it, this Batch file do that:

    @echo off
    cd "\Folder A"
    for /F "delims=" %%a in ('dir /B /AD') do (
       move "%%a" ..
    )
    cd ..
    rd "\Folder A"
    

    Is this what you want?

    EDIT: New version added as answer to new comments

    @echo off
    rem Eliminate all folders present at the same level of the Batch file
    rem and move their contents one level up
    for /F "delims=" %%a in ('dir /B /AD') do (
       cd "%%a"
       for /F "delims=" %%b in ('dir /B /AD') do (
          move "%%b" ..
       )
       move *.* ..
       cd ..
       rd "%%a"
    )
    

    You must note that if two or more folders or files in the stuff have the same name, and they were originally placed directly below the top-level folders that are being deleted, then the program will issue an error and the folder or file will not be moved (and the top-level folder will not be deleted).

    Antonio