DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: IDEA: Folder File Count and Move  (Read 8,364 times)

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
IDEA: Folder File Count and Move
« on: October 11, 2015, 05:05 PM »
There seem to be a number of utilities out there for size of folders, but nothing obvious for File count.

Concept
From source folder move all sub folders with less than or greater than x files (not likely both) to definable Destination folder.

Givens
1. For ease destination folder would be at same or higher level than source folder on same drive, with a name of same or less length (should prevent path length issues and lengthy move process)
2. There will be only folders in the source folder.
3. If a sub folder contains a further folder it may be classified as 'one' item for the purpose of the count.

I have something like 20000 folders all with small files of varying numbers, the intent is to weed out those with few files for further review/categorisation.
Why an I Magician62? Because Magician1 thru 61 were gone. :)

ConstanceJill

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 248
Re: IDEA: Folder File Count and Move
« Reply #1 on: October 13, 2015, 12:27 PM »
Hello there .o/

Can be done with a batch script (.bat or .cmd file).
Here's a quick and dirty one :
Code
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM This script counts the number of files in each of the folders right below SrcFolder (including sub-folders)
REM If that number is higher than the set threshold (see variable FileCountLimit), then it moves that folder to TargetFolder
REM WARNING : both TargetFolder and SrcFolder paths MUST include the final "\" character or the script will fail.
REM This was only tested under Windows 7
SET TargetFolder=E:\Moo\stuff\
SET SrcFolder=C:\foo\bar\whatever\
SET FileCountLimit=400
for /f "delims=*" %%d in ('dir /a:d /b "%SrcFolder%"') do (
	SET Folder=%%d
	ECHO Counting files in folder "%SrcFolder%!Folder!"...
	SET FileCount=0
	for /f "delims=*" %%f in ('dir /a:-d /b /s "%SrcFolder%!Folder!"') DO (
		SET /A FileCount+=1
	)
	ECHO Folder contains !FileCount! files.
	IF !FileCount! GEQ %FileCountLimit% (
			ECHO File count is higher or equal to the limit ^(%FileCountLimit%^), DO ACTION HERE
			MOVE /Y "%SrcFolder%!Folder!" "%TargetFolder%"
		) ELSE (
			ECHO File count is inferior to the limit ^(%FileCountLimit%^), no action.
	)
)
pause

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
Re: IDEA: Folder File Count and Move
« Reply #2 on: October 13, 2015, 08:21 PM »
I had a few issues at first which seems related to characters in the folder name. I have separated those for the moment and will investigate later.

But for the moment it seems to be working, only 14000 folders to go on this pass :)
Many thanks
Why an I Magician62? Because Magician1 thru 61 were gone. :)

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: IDEA: Folder File Count and Move
« Reply #3 on: October 13, 2015, 09:33 PM »
I had a few issues at first which seems related to characters in the folder name. I have separated those for the moment and will investigate later.

You could probably get it to process those by adding a change codepage command (chcp) at the start of the command file to change the character set it uses by default, (normally 850).
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • Coding Snacks
Re: IDEA: Folder File Count and Move
« Reply #4 on: October 13, 2015, 09:52 PM »
These may be close depending how the rule triggering is implemented:
http://alternativeto.net/software/file-juggler/?platform=windows

But I suspect there is a rule based file manager out there that does it.

ConstanceJill

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 248
Re: IDEA: Folder File Count and Move
« Reply #5 on: October 14, 2015, 06:17 AM »
Yeah, I've noticed the command line can have issues with unicode character names when using the default encoding, so indeed, using a CHCP command at the beginning to change it can help.

It would be "CHCP 65001" for UTF-8, you'll also need to change the display font to Lucida Console (or that least according to some other threads) if you want to have those names displayed correctly (if/when applicable).

Edit : well that's weird, I seem to remember having this trick work a while ago on my computer, but today it doesn't want to display Japanese characters correctly in the console :/
« Last Edit: October 14, 2015, 06:34 AM by ConstanceJill »

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: IDEA: Folder File Count and Move
« Reply #6 on: October 14, 2015, 06:40 AM »
I'd give chcp 28591 a go, it's worked for me with various accented characters, (corresponds to ISO 8859-1 Latin 1; Western European (ISO)).
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
Re: IDEA: Folder File Count and Move
« Reply #7 on: October 14, 2015, 05:34 PM »
I think some of the issues were with folders beginning with " ", which obviously shouldn't be there in the first place. I still need to investigate, but it may just be as easy to do a quick rename and deal with them in later processing as they are less than 0.5% of the folders processed. :)
Why an I Magician62? Because Magician1 thru 61 were gone. :)

Original address: https://www.donationcoder.com/forum/index.php?topic=41757.0