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
Move Folders with More than Two Files
Moderator: DosItHelp
Re: Move Folders with More than Two Files
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%\..\"
)
)
Steffen
Re: Move Folders with More than Two Files
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
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
-
- Posts: 8
- Joined: 29 Nov 2015 21:42
Re: Move Folders with More than Two Files
Yes the parent of the folder that the subs are in (so up one directory)aGerman wrote: ↑10 Dec 2017 08:18Parent directory of what? Of the folder with the 1000 sub directories?... saved in the folder with the sub directories you want to check.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%\..\" ) )
Steffen
Thanks to you both I will try it out after church
Both methods worked, thank you both very much!