topbanner_forum
  *

avatar image

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

Login with username, password and session length
  • Sunday December 15, 2024, 7:15 pm
  • Proudly celebrating 15+ 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 6356 times)

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 180
    • View Profile
    • Donate to Member
IDEA: Folder File Count and Move
« on: October 11, 2015, 12: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: 243
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #1 on: October 13, 2015, 07:27 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: 180
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #2 on: October 13, 2015, 03: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

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

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #4 on: October 13, 2015, 04:52 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: 243
    • View Profile
    • Donate to Member
Re: IDEA: Folder File Count and Move
« Reply #5 on: October 14, 2015, 01: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, 01:34 AM by ConstanceJill »

4wd

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

magician62

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