Move Folders with More than Two Files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
NewYears1978
Posts: 8
Joined: 29 Nov 2015 21:42

Move Folders with More than Two Files

#1 Post by NewYears1978 » 09 Dec 2017 23:12

Hi all!

I have a folder that has about 1,000 sub folders in it. Some of them have only 2 files, the others have more than 2 files.

I want to move all the ones with only 2 files, to the parent directory (or to a specific directory if that is easier).

Thanks in advance - assuming this is even possible :)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Move Folders with More than Two Files

#2 Post by aGerman » 10 Dec 2017 08:18

NewYears1978 wrote:
09 Dec 2017 23:12
to the parent directory
Parent directory of what? Of the folder with the 1000 sub directories?

Code: Select all

@echo off &setlocal
for /d %%i in (*) do (
  for /f %%j in ('dir /a-d /b "%%~i\"^|find /c /v ""') do (
    if %%j==2 move "%%~i" "%cd%\..\"
  )
)
... saved in the folder with the sub directories you want to check.

Steffen

Hackoo
Posts: 103
Joined: 15 Apr 2014 17:59

Re: Move Folders with More than Two Files

#3 Post by Hackoo » 10 Dec 2017 09:13

You can set a new folder on your desktop to move your subfolders on it as example :
If the output is correct on your side , you can remove the echo before the move command

Code: Select all

@echo off
Set "DestinationFolder=%userprofile%\desktop\NewFolder"
If not exist "%DestinationFolder%\" MD "%DestinationFolder%\"
Setlocal EnableDelayedExpansion
@for /D  %%D in (*) DO (
	pushd "%%D"
	@for /f %%a in ('dir /a-d/b/-o/-p/s^|find /v /c "" 2^>nul') do set /a "Count=%%a"
		If "!Count!" EQU "2" (
			Set "Folder=%%D"
			echo "!Folder!" has 2 files inside
			echo move "!Folder!" "%DestinationFolder%\"
		)
	popd
	)
)
pause & exit

NewYears1978
Posts: 8
Joined: 29 Nov 2015 21:42

Re: Move Folders with More than Two Files

#4 Post by NewYears1978 » 10 Dec 2017 10:16

aGerman wrote:
10 Dec 2017 08:18
NewYears1978 wrote:
09 Dec 2017 23:12
to the parent directory
Parent directory of what? Of the folder with the 1000 sub directories?

Code: Select all

@echo off &setlocal
for /d %%i in (*) do (
  for /f %%j in ('dir /a-d /b "%%~i\"^|find /c /v ""') do (
    if %%j==2 move "%%~i" "%cd%\..\"
  )
)
... saved in the folder with the sub directories you want to check.

Steffen
Yes the parent of the folder that the subs are in (so up one directory)

Thanks to you both I will try it out after church :)


Both methods worked, thank you both very much!

Post Reply