Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 07, 2016, 04:24:42 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: IDEA: Folder File Count and Move  (Read 1751 times)

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 118
    • View Profile
    • Donate to Member
IDEA: Folder File Count and Move
« on: October 11, 2015, 12:05:09 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.

ConstanceJill

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 87
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #1 on: October 13, 2015, 07:27:47 AM »
Hello there .o/

Can be done with a batch script (.bat or .cmd file).
Here's a quick and dirty one :
@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: 118
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #2 on: October 13, 2015, 03:21:05 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

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,474
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #3 on: October 13, 2015, 04:33:03 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).

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,285
    • View Profile
    • Miles Ahead Software
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #4 on: October 13, 2015, 04:52:22 PM »
These may be close depending how the rule triggering is implemented:
http://alternativeto...er/?platform=windows

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

ConstanceJill

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 87
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #5 on: October 14, 2015, 01:17:37 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, 01:34:35 AM by ConstanceJill »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 4,474
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #6 on: October 14, 2015, 01:40:56 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)).

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 118
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #7 on: October 14, 2015, 12:34:11 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. :)