We have some email archiving that is dumping all the emails into a directory. Because of some performance reasons with the server, I want to setup an automated task that will run a script once a day and if there is more than 3,000 (or whatever number) of files in the main directory, create a new directory with the date and move all the main directory files into it. I'm sure someone has already written something similar, so if anyone could point me at it that would be great. Batch file or Powershell would both be fine.

  • what performance issues are you having with directories over 3k? Wouldn't you want to solve the performance problem, or is this homework?
    – Jim B
    Jun 8, 2010 at 20:35
  • The performance problem has to do with the way the mail server does its indexing in an xml file. As there are more files the time to read and write to the index file increases. So separating mail into directories fixes that.
    – user11956
    Jun 9, 2010 at 15:33

2 Answers 2


Written and tested. Copy the following code into a *.bat file. You'll want to modify the directory where the emails exist at the start of the code. The variable cBig has already been set to 3000 but you can change this if you'd like. At the bottom, move *.txt will have to be changed to reflect the extension of the emails you're moving. Once you've tested it and are happy you can remove the pause commands... they just help see what's going on. Good luck!

echo off

REM **navigate to the directory

REM **store count of files to file count.txt (/a-d removes folders from count)
dir /b /a-d | find /v /c "::" > count.txt

REM **read count back in to variable (easiest way I knew how to do this)
set /p myvar=<count.txt

REM **set your upper limit (in your case 3000)
set cBig=3000

REM **quick display of the number of files
echo %myvar%


REM **is the number of files larger than our upper limit? If so goto BIG
if '%myvar%' gtr '%cBig%' goto BIG

REM **do nothing

REM **create new directory with date and move all files
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
MD %FDate%
move *.txt ./%FDate%

  • I think you need to use dir /b /a-d instead of dir /b to prevent counting directories
    – ggonsalv
    Jun 9, 2010 at 11:34
  • Thanks, still have to try it out, but looks very good
    – user11956
    Jun 9, 2010 at 15:30

Untested .CMD script.

REM @echo off
setlocal enableextensions enabledelayedexpansion

  rem Print all filenames (excl. folders) in current directory into temporary text-file
  dir /B /A-D  1>%TMPTXT%

  rem Count number of files (lines) in text-file
  set FILECNT=0
  for /F %%i in (%TMPTXT%) do (
    set /A FILECNT=!FILECNT!+1
  echo Number of files in folder: !FILECNT!

  rem Is number of files greater than expected?
  if /I !FILECNT! GTR 2999  call :MoveFiles

  del %TMPTXT%
  goto :EOF

  rem Construct a folder-name based on date (remember date changes at midnight)
  rem Since the date value is locale specific, you might want to fiddle with string-replacing.
  mkdir "%SUBFLDR%"
  if /I !ERRORLEVEL! NEQ 0 (
    echo Failed to create sub-folder '%SUBFLDR%'.
    goto :EOF

  rem Move only those files found in text-file to the new folder.
  for /F %%f in (%TMPTXT%) do (
    move  "%%f"  "%SUBFLDR%\."
    if /I !ERRORLEVEL! NEQ 0  echo Failed to move file '%%f'
  goto :EOF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.