Page 1 of 1

Move Folders with More than Two Files

Posted: 09 Dec 2017 23:12
by NewYears1978
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 :)

Re: Move Folders with More than Two Files

Posted: 10 Dec 2017 08:18
by aGerman
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

Re: Move Folders with More than Two Files

Posted: 10 Dec 2017 09:13
by Hackoo
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

Re: Move Folders with More than Two Files

Posted: 10 Dec 2017 10:16
by NewYears1978
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!