0

I have 1800 main folders, around 1000 or so of those have a subfolder in them with the same name (always the same name).

These subfolders have the files which are not picked up by the program I use because it is not in the main folder. I have looked into changing the directory for the program but alas to no avail.

So I need to move all those subfolders into the main folder, any idea how I can do this?

For example:

folder1 with all the files

folder2 with a subfolder also called folder2 which has all the files

folder3 with all the files

+1797

Etc.

7
  • So you have main Folder and inside main folder you have Subfolder and inside Subfolder you have another Subfolder with the same name as it's parent. Both Subfolders can't be in main folder unless you rename one of them because there can't be 2 folders with the same name in the same folder. Commented Sep 29, 2021 at 0:22
  • Not exactly, I have folder A that has 1800 folders, these 1800 folders can be called folder B, this folder Bs either have files in them or another folder (a subfolder that has the same name as folder B) in it. This sub folder is folder C and these folders have all the files in them instead of them just being in their respective folder B I want to get rid of the extra folder by moving all the files up on level Commented Sep 29, 2021 at 0:29
  • So you have Folder A (main) | Folder B (empty) | Folder C (Files) and you want to get rid of folder B? Commented Sep 29, 2021 at 0:34
  • Oh, I think I got it now. You want to move files form folder C to B and delete C? Commented Sep 29, 2021 at 0:36
  • Either way works, get rid of B and move C up a level or move all from C to B Commented Sep 29, 2021 at 0:40

2 Answers 2

0

You can create a batch script that does the job for you. You copy the code below, put it in notepad. Save the file with the name you want but with a *.bat extension. Then you drag the main folder (not the main sub-folders) to the batch file.

Make a test on some copy first to see if the result is as expected...

@echo off

IF /i exist "%~1" (set "MainFolder=%~1") else (exit)

pushd "%MainFolder%"
For /f "delims=" %%a in ('dir /ad /b 2^>nul') do Call :MSubfolders "%%~a"

exit

:MSubfolders
set "MSubfolder=%~1"
for /f "delims=" %%a in ('dir /ad /b /s "%MSubfolder%\*" ^|sort /r 2^> nul') do (
                                                                                 for /f "delims=" %%b in ('dir /b /a-d "%%a\*" 2^>nul') do IF /i not exist "%MSubfolder%\%%b" (move "%%a\%%b" "%MSubfolder%\") else (call :Rename "%%a" "%%b")
                                                                                 rd "%%a\"
                                                                                )
goto :EOF

:Rename
for /l %%a in (1,1,100) do (
                            if /i not exist "%MSubfolder%\%~n2(%%a)%~x2" move "%~1\%~2" "%MSubfolder%\%~n2(%%a)%~x2"
                            goto :EOF
                           )
goto :EOF

enter image description here

1
  • Ok I'll give it a try thanks! Commented Sep 30, 2021 at 2:25
0

Untested, but this PowerShell should be close.

Assunig:

  • Any sub-folder of a namin folder should have its files moved to the main folder and then the subfolder deleted.
  • Command is run from the folder containing the main folders (path easily added)

Verbose:

Get-ChildItem -Directory | Where-Object { ( $SubFolder = Get-ChildItem $_.FullName -Directory ) } -PipelineVariable MainFolder | ForEach-Object{
    # Move files to MainFolder
    Get-ChildItem $SubFolder -File | Move-Item -Destination $MainFolder.FullName -WhatIf
    # Delete empty SubFolder
    Remove-Item $SubFolder -WhatIf
}

Aliased:

gci -ad | ? { ( $SubFolder = gci $_.FullName -ad ) } -pv MainFolder | %{
    gci $SubFolder -af | move -Destination $MainFolder.FullName -WhatIf
    del $SubFolder -WhatIf
}

The -WhatIf parameter allows you to test the command without actual moving/deletion. Remove after testing to perform the actions.

You must log in to answer this question.

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