//////////////////////////////////////////////////////////////////////////////// // // Task: DIRCOUNT.TSK // (Show and log dirs, files, and space usage by subdirectory) // Author: Avanti Technology, Inc. // http://www.avanti-tech.com // Version: 1.1 - Add support for file sizes and logging // 1.0 - Initial Release // // Description: // ============ // Walks a directory tree (specified as command line option or coded // default, if not specified. // // Objective: // ========== // Check volume usage by directory. // // Usage: // ====== // Script can be manually executed using the TaskMaster TMRUN command // at the TMConsole (Shell) Screen: // // Example: TMRUN [vol:path\]DIRCOUNT.TSK // // Note: [vol:path\] is not required if the task resides either // in SYS:SYSTEM or the TaskMaster NLM load directory. // // Script can be scheduled for automatic execution using Client interface or // using the TaskMaster TMSCHEDULE command at the TMConsole (Shell) Screen: // // Examples: TMSCHEDULE ADD DIRCOUNT.TSK 01 02:00 // (Executes the 1st of each month at 2:00am) // // TMSCHEDULE ADD DIRCOUNT.TSK YNNNNNN 02:00 // (Executes every Sunday [SMTWTFS] at 2:00am) // // Note: Scheduled tasks must reside either in SYS:SYSTEM or the // TaskMaster NLM load directory for security reasons. // // // Variables; // ========= // %0 : Vol:Path to traverse // %1 : Total number of directories // %2 : Total number of files // %3 : Total MB of files // %4 : Total KB (modulus) of files (for sub MB value) // %5 : Number of subdirectories in this directory // %6 : Number of files in this directory // %7 : MB of files in this directory // %8 : KB (modulus) of files (for sub MB value) in this directory // %9 : Temporary variable used for multiple purposes // // Compatibility: // ============== // This task has been tested on the following platforms without demonstrating // any compatibility issues or any other reported/confirmed conflicts: // TaskMaster v3.28 (or later) // NetWare v4.1x / v5.x / v6.x // // Warning: // ======== // AS WITH ANY NEW SOFTWARE PROGRAM, BATCH SCRIPT, OR AUTOMATED PROCESSING // PROCEDURE, CAUTION SHOULD BE EXERCISED AND DUE DILIGENCE OBSERVED DURING // INITIAL IMPLEMENTATION. WHERE POSSIBLE, TESTING SHOULD BE PERFORMED ON // NON-PRODUCTION SYSTEMS PRIOR TO FULL IMPLEMENTATION. // //////////////////////////////////////////////////////////////////////////////// // Open log file DIRCOUNT.LOG in the same directory as where the task resides OPEN WRITE %TASK_PATH%\DIRCOUNT.LOG TRUNCATE IF ERRORLEVEL ECHO. ECHO ERROR: Unable to open log file (%TASK_PATH%\DIRCOUNT.LOG)! ECHO Aborting... ECHO. ENDIF // Check if a directory is specified on the command line. // If valid, use it else define a default directory to check IF "%0"=="" OR NOT EXIST "%0" THEN DEFINE %0 SYS:\ // Change the Current Working Directory (CWD) to the desired starting point CD %0 // define variable %1 for use in counting total number of directories DEFINE %1 00000 // define variable %2 for use in counting total number of files DEFINE %2 00000 // define variable %3 for use in counting total size of files in MB DEFINE %3 000000 // define variable %4 for use in counting total size of files in KB (modulus) DEFINE %4 0000000000 // display header ECHO_WRITE Dirs Files Size(MB) Directory ECHO_WRITE ----- ----- -------- ------------------------ // For directory counting, use a WHILE/LOOP WHILE // Define variable %5 as the total number of subdirectories in directory DEFINE %5 00000 // For dir counting, use a WHILE/LOOP (BREAK if %5 is NULL - no more dirs) WHILE // Define variable %5 as the next matching entry in the subdirectory search. // If this is the first %DIR_SUB_%, we must provide a search pattern. IF %5==00000 DEFINE %9 %DIR_SUB_*.*% ELSE DEFINE %9 %DIR_SUB_% ENDIF // Abort the WHILE/LOOP is %9 if NULL (i.e., no more subdirs) IF "%9"=="" THEN BREAK // Increment %5 for each subdirectory found within this directory DEFINE %5 %5+=1 // LOOP back up to the WHILE check for the next matching file LOOP // Define variable %6 as the total number of files in directory DEFINE %6 00000 // Define variable %7 as the total size (MB portion) of files in this subdir DEFINE %7 00000 // Define variable %8 as the total size (KB portion) of files in this subdir DEFINE %8 0000000000 // For file counting, use a WHILE/LOOP (BREAK if %5 is NULL - no more files) WHILE // Define variable %6 as the next matching entry in the file search. // If this is the first %DIR_FILE_%, we must provide a search pattern. IF %6==00000 DEFINE %9 %DIR_FILE_*.*% ELSE DEFINE %9 %DIR_FILE_% ENDIF // Abort the WHILE/LOOP is %9 if NULL (i.e., no more files) IF "%9"=="" THEN BREAK // Increment %6 for each file found within this directory DEFINE %6 %6+=1 // Define %9 as %CWD% and %DIR_FILE_% for file info retrieval DEFINE %9 %CWD%\%9 // Define %9 as the size of the current file DEFINE %9 %FILE_SIZE_%9% // Increment file size info (MB) for this subdirectory & total CALC %7 %7 + (%9 / 1048576) CALC %3 %3 + (%9 / 1048576) // Increment file size info (KB) for this subdirectory CALC %8 %8 + (%9 - ((%9 / 1048576) * 1048576)) // Check if KB to MB wrap increment required IF "%8">"0001048576" DEFINE %7 %7+=1 DEFINE %8 %8-=1048576 ENDIF // Increment file size info (KB) total CALC %4 %4 + (%9 - ((%9 / 1048576) * 1048576)) // Check if KB to MB wrap increment required IF "%4">"0001048576" DEFINE %3 %3+=1 DEFINE %4 %4-=1048576 ENDIF // LOOP back up to the WHILE check for the next matching file LOOP // Calculate decimal 100 KB value for display CALC %8 (%8/102400) // Reformat variable contents for display REFORMAT %5 PACK RIGHT 5 REFORMAT %6 PACK RIGHT 5 REFORMAT %7 PACK RIGHT 6 REFORMAT %8 PACK RIGHT 1 //DEFINE %7 000000+=%7 //DEFINE %8 0+=%8 IF "%TM_VERSION%">="4" DEFINE %9 %FILE_NAME_LONG_%CWD%% IF "%9"=="" THEN DEFINE %9 %CWD% ELSE DEFINE %9 %CWD% ENDIF // display the directory information ECHO_WRITE %5 %6 %7.%8 %9 // Restore zero filled data formats for comparisons REPLACE %5 " 0" REPLACE %6 " 0" // Increment %1 by the dir count for this directory DEFINE %1 %1+=%5 // Increment %2 by the file count for this directory DEFINE %2 %2+=%6 // Define variable %9 as the next matching entry in the directory tree // If this is the first directory (total dir count == current dir count), // we must provide a starting point for the %DIR_TREE_% System Environment // Variable to begin traversing IF %1==%5 DEFINE %9 %DIR_TREE_%CWD% ELSE DEFINE %9 %DIR_TREE_% ENDIF // Abort the WHILE/LOOP is %9 if NULL (i.e., no more levels) IF "%9"=="" THEN BREAK // LOOP back up to the WHILE check for the next directory tree level LOOP // Reformat the total dir count to strip leading zeroes DEFINE %1 0+=%1 // Reformat the total file count to strip leading zeroes DEFINE %2 0+=%2 // Reformat the total size MB value to strip leading zeroes DEFINE %3 0+=%3 // Calculate &reformat decimal 100 KB value for display CALC %4 %4/102400 DEFINE %4 0+=%4 ECHO_WRITE. ECHO_WRITE %CWD% ECHO_WRITE Dir(s): %1 ECHO_WRITE File(s): %2 ECHO_WRITE Size MB: %3.%4 CLOSE