PDA

View Full Version : General directory scanner



ErosOlmi
21-03-2007, 13:04
Generic script illustrating how to loop inside directories to search for files.
A general purpose DoTheJob function can me personalized.
In this case function is just printing file name, date and number of days since last write access to file and today.

Tip: execute the script once and than execute again. This will give you the idea of what is caching on hard disks usage.
On my system it can takes something like 1 minute the first time and 10 seconds from second time on.

_________________________________________________


'-------------------------------------------------------------------------
'Load needed thinBasic modules
'-------------------------------------------------------------------------
USES "FILE" '---File functionalities
USES "LL" '---Linked list functionalities
uses "DT" '---Date functionalities
uses "Console" '---Console functions


'-------------------------------------------------------------------------
'Start Main Program
'-------------------------------------------------------------------------
dim T1, T2 as double '---Timing counters

dim SourceDirs as string '---This will contains source directory listing
'---It can be loaded from a buffer or from a file

T1 = timer
'---Just put some directories here separated by $CRLF
'---This list can be loaded from a file using FILE_LOAD function
SourceDirs = _
"C:\" & $crlf & _
"C:\WINDOWS\" & $crlf & _
"C:\WINDOWS\SYSTEM32\"


'---Now parse source list and load an array
dim vSource() as string
dim SourceTotal as long value parse(SourceDirs, vSource, $crlf)

global TotalNumberOfFiles as long
global TotalSize as quad


'---OK, now loops through sources and do the job
dim Count as long
for Count = 1 to SourceTotal
if dir_exists(vSource(Count)) then
console_writeline "--->> Checking dir: " & vSource(Count)
DoTheJob(vSource(Count))
end if
next

T2 = timer

console_writeline string$(79, "-")
console_writeline "Job done in " & format$(T2 - T1, "#0.0000") & " secs"
console_writeline "Total files checked: " & TotalNumberOfFiles
console_writeline "Total size in bytes: " & TotalSize
console_waitkey


'-------------------------------------------------------------------------
'DoTheJob function
'-------------------------------------------------------------------------
' Add here the needed logic on files
' If you need to delete files older than xxx days just add a line like:
'
' IF nDays > xxx then File_Kill(FileName)
'
'-------------------------------------------------------------------------
FUNCTION DoTheJob(SourceDir as string) as long
LOCAL Count AS LONG
LOCAL FileName AS STRING
LOCAL pList AS LONG
LOCAL nItems AS LONG
LOCAL fSize AS QUAD
local fDate as string
local nDays as long

'---Create a list with the files found in source directory
pList = DIR_LIST(SourceDir, "*.*", %FILE_NORMAL OR %FILE_HIDDEN OR %FILE_SYSTEM OR %FILE_ADDPATH)
'---Count them
nItems = LL_Count(pList)

IF nItems > 0 THEN
FOR Count = 1 TO nItems

'---Get the full file name
FileName = LL_GetItem(pList, Count)

'---Get some file info
fSize = File_size(FileName)
fDate = File_GetDate(FileName, %DATE_TIME_LAST_FILE_WRITE)
nDays = DT_DateDiff(fDate, date$, %DT_DIFF_IN_DAYS)

'---Her file name and some data is printed
'---but any logic can be performed.
console_writeLine FileName & " FileSize: " & fSize & " " & fDate & " " & nDays

'---Collect some info ...
TotalNumberOfFiles += 1
TotalSize += fSize

NEXT
END IF

END FUNCTION

kryton9
22-03-2007, 07:57
Thanks Eros these are so handy and powerful.

You should make a master compressed version of Eros's super duper scripts!!