View Full Version : question about visual basic
Magyar Khan
01-08-2003, 16:29
i want to make a stats tool which allows a player to read all his logfiles at once and analyses it for wins losses and so on...
anyone knows teh commands needed to scan all subfolders and its contents for logfiles?
Lord Gnome
01-08-2003, 23:34
Hi Magyar Khan, I'm not a programming guru by any means but I can do a bit of stuff in VB that might be able to help.
I'm kinda guessing exactly what you need to do, if you can be more specific about exactly what functions you need or task to carry out I'll know better if I can assist. I'll have a go based on what you said anyway.
It sounds like you want to search for files with a specific file name/file extension in a given location, unfortunatly VB has no intrinsic FileExists function http://www.totalwar.org/forum/non-cgi/emoticons/frown.gif. However you can hack it by either using a open statement and trapping the resulting error http://www.totalwar.org/forum/non-cgi/emoticons/eek.gif or a testing whether a dir() command will execute for the given file. These work but are a bit ugly, and if you are searching a big file space it is sloooow (um, where are the logs kept? if you know the folderpath you can limit your search there) It's okay if you just looking in a relatively small file space though (still ugly though.)
Better is to use the windows api to do it - it's much faster and more effiecent to code. I've got a couple of windows api's that do recursive File/Folder searches (nicked off the web at some point in the past) which I could post here if you want or mail you as text files. The important bits are the FindFirstFile and FindNextFile api functions in kernel32.dll, (very easy to use in case your not familiar with api stuff). I can probably dig out a couple of (old but gold) web-links for VB code as well if you want.
Wellington
01-09-2003, 02:57
Magyar Khan,
I can't puzzle out exactly what your criteria for a "logfile" is. The following code is a recursive routine that provides an array of all files starting from a root (define the root at your own risk, as per CPU processing power - I've defined a small root folder "Historical Battles" in MTW). When you have the list of files you can then open them and check them for contents (alternately you can limit to list of files that pertain to 'wildcard' file names.
Anyway, here's the example code which can be chopped for VB5/6/Script or whatever ....
' *******************************************************
' *** START OF EXAMPLE CODE
' *******************************************************
Dim FilePathArray(), FilePathArrayIndex, RootPath
RootPath = "C:\Program Files\Total War\Medieval - Total War\Battle\batinit\Historical Battles"
Call BuildAllFilePaths(RootPath)
Call ShowResults(FilePathArray)
Sub BuildAllFilePaths(rootfolder) ' start the ball rolling
Dim fso, onefolder
FilePathArrayIndex = 0
Redim FilePathArray(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set onefolder = fso.GetFolder(rootfolder)
Call ShowSubFolderPaths(onefolder)
End Sub
Sub ShowSubFolderPaths(onefolder) ' check all folders in a subfolder
Dim folder
For Each folder In onefolder.SubFolders
Call ShowFilesPaths(folder)
Call ShowSubFolderPaths(folder)
Next
End Sub
Sub ShowFilesPaths(folder) ' check all files in a folder/subfolder
Dim file, i, filetype
For Each file in folder.Files
Redim Preserve FilePathArray(FilePathArrayIndex+1)
Set FilePathArray(FilePathArrayIndex) = file
FilePathArrayIndex = FilePathArrayIndex + 1
Next
End Sub
Sub CheckFileContents(thisfile) ' we have a file open it and check whatever you wish
' code, code, code ... blah, blah, blah
End Sub
Sub ShowResults(dataarray)
Dim i, AllFiles
' do what you want to in here, but for "The Org's" modders who want to see
' what is happening (and who may not have VB6) this a VBScript version that
' merely shows the results of the above (recursive) 'file list' routines
For i = 0 to Ubound(dataarray) -1
AllFiles = Allfiles & vbCrLf & dataarray(i)
Next
Wscript.Echo AllFiles
End Sub
' *******************************************************
' *** END OF EXAMPLE CODE
' *******************************************************
If anyone is reading this and does'nt have VB5/6 then just create a new text file, copy the contents of the above code and save the file as "test.vbs". RUnthe sabves vbs file and you should see the output from this routine.
Magyar Khan, if you wish to discuss this (pass ideas or whatever) the E-Mail me.
Welly
Wellington
01-09-2003, 03:16
Whoops ... missed a line (only a potential problem for Magyar Khan who requirs acces to a "file checking routine within a root".
Always a problem when your chopping and changing code for examples (well .. that's MY excuse anyway!http://www.totalwar.org/forum/non-cgi/emoticons/wink.gif
Note the single line "Call CheckFileContents(file)" that I should have included in the above code -
Sub ShowFilesPaths(folder) ' check all files in a folder/subfolder
Dim file, i, filetype
For Each file in folder.Files
Redim Preserve FilePathArray(FilePathArrayIndex+1)
Set FilePathArray(FilePathArrayIndex) = file
FilePathArrayIndex = FilePathArrayIndex + 1
Call CheckFileContents(file)
Next
End Sub
Magyar Khan
01-09-2003, 03:26
well got it working, with simple code
interested?
Wellington
01-09-2003, 04:04
Magyar Khan,
I'm ALWAYS interested - although my demeanour is always relative to my current 'mood'
Are you Ok with the code/processing now?
Magyar Khan
01-09-2003, 05:00
yup very, now i must think about how and what to analyuse effectively
vBulletin® v3.7.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.