Skip navigation

JSI Tip 9460. How do I implement a shared folder quota system?


I have scripted Quota.bat to implement a shared folder quota system on a 'server'.

For each 'server' that you implement, Quota.bat will detect if the shared folder(s) on the 'server' have exceeded your limit(s), and if so, for each shared folder whose limit is exceeded, Quota.bat will prevent additional writes to the share, and send an email to as many 'share managers' as you desire.

Quota.bat uses the following freeware:
. RMTShare.exe
. Du.exe
. BLAT.exe

To implement Quota.bat on a 'server':

1. Define three (3) files in the same folder as Quota.bat:

   Quota_wait.txt     contains a single entry, the number of seconds to wait between each quota checking cycle.
                      If you type 60, Quota.bat would wait 1 minute.
                      If you type 300, Quota.bat would wait 5 minutes.

   Quota_msgtxt.txt   contains the fixed text of a message that will be send to each 'share manager'
                      when a quota is exceeded. It can be as simple as:
                      Quota.bat detected that the subject limit has been exceeded. 
                      since the message subject contains the computer name, local share folder name, 
                      quota limit that you set, and the size detected.

   Quota_limit        each line contains the configuration parameters for a share on this 'server':
                      Folder Path,Limit,RMTSHARE command,email addresses
                      where:
                             Folder Path      is the local path to the shared folder, like C:\Users.

                             Limit            is the maximum number of megabytes allowed.

                             RmtSHARE command is the RMTShare command that changes the share
                                              permissions to Read. If C:\Folder\Accounting is shared as
                                              \\ComputerName\ShareName and the domain groups 
                                              "Accountants" and "Accounts Payable" in the JSIINC.COM domain
                                              had Full Control or Change permission, use:
                                              rmtshare \\ComputerName\ShareName /GRANT "JSIINC\Accountants":R /GRANT "JSIINC\Accounts Payables":R

                            email addresses   is the comma separated list of email addresses that should be
                                              notified when the quota is exceeded, like:
                                              [email protected],[email protected],[email protected]

2. Start Quota.bat. I use a Scheduled Task that is configured to run Quota.bat when the computer starts.
NOTE: When a quota is exceeded, after correcting the situation, delete the Folder Path line from the Quota_Exceeded.txt file that is created in the same folder as Quota.bat, and reset the share permissions to allow writing.

Quota.bat contains:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set where=%~DP0
set wait="%where%Quota_wait.txt"
set limit="%where%Quota_limit.txt"
set msgtxt="%where%Quota_msgtxt.txt"
set exceeded="%where%Quota_Exceeded.txt"
@echo.Quota_Exceeded.txt>%exceeded%
if not exist %wait% @echo %wait% does not exist&endlocal&goto :EOF
if not exist %limit% @echo %limit% does not exist&endlocal&goto :EOF
if not exist %msgtxt% @echo %msgtxt% does not exist&endlocal&goto :EOF
for /f "Tokens=*" %%a in ('type %wait%') do (
 if "%%a" NEQ "" set /a seconds=%%a + 1
)
:loop
@ping -n %seconds% 127.0.0.1>nul
for /f "Tokens=1-3* Delims=," %%a in ('type %limit%') do (
 set Folder=%%a
 set /a max=%%b
 set rmtcmd=%%c
 set blat=blat %msgtxt% -to %%d
 for /f "Tokens=*" %%x in ('du -q !Folder!^|find "Size:"') do (
  set size=%%x
  set size=!size:* =!
  set size=!size: bytes=!
  set size=!size: =!
  set size=!size:,=!
  call :quotaX !size!##
 )
)
goto Loop
:quotaX
set lx=%1
set /a cnt=0
:quotaXs
set /a cnt=%cnt% + 1
call set wrk=%%lx:~%cnt%^,1%%
if "%wrk%" NEQ "#" goto quotaXs
set /a cnt=%cnt% - 6
call set /a sz=%%lx:~0,%cnt%%%
if %sz% LSS !max! goto :EOF
set ex=%ComputerName%!Folder!!max!
set OK=Y
for /f "Tokens=*" %%i in ('@echo %ex%^|findstr /l /i /G:%exceeded%') do (
 set OK=N
)
if "%OK%" EQU "N" goto :EOF
@echo %ex%>>%exceeded%
!rmtcmd!
set subject=!blat! -s "Quota %ComputerName% !Folder! exceeded !max! - !sz!."
%subject%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish